File: ipgeolocation.rb

package info (click to toggle)
ruby-geocoder 1.8.6-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 900 kB
  • sloc: ruby: 8,419; makefile: 3
file content (59 lines) | stat: -rw-r--r-- 1,162 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
require 'geocoder/results/base'

module Geocoder::Result
  class Ipgeolocation < Base

    def coordinates
      [@data['latitude'].to_f, @data['longitude'].to_f]
    end

    def address(format = :full)
      "#{city}, #{state} #{postal_code}, #{country_name}".sub(/^[ ,]*/, "")
    end

    def state
      @data['state_prov']
    end

    def state_code
      @data['state_prov']
    end

    def country
      @data['country_name']
    end

    def country_code
      @data['country_code2']
    end

    def postal_code
      @data['zipcode']
    end

    def self.response_attributes
      [
          ['ip', ''],
          ['hostname', ''],
          ['continent_code', ''],
          ['continent_name', ''],
          ['country_code2', ''],
          ['country_code3', ''],
          ['country_name', ''],
          ['country_capital',''],
          ['district',''],
          ['state_prov',''],
          ['city', ''],
          ['zipcode', ''],
          ['time_zone', {}],
          ['currency', {}]
      ]
    end

    response_attributes.each do |attr, default|
      define_method attr do
        @data[attr] || default
      end
    end
  end
end