geo_ip 0.3.1 → 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. data/lib/geo_ip.rb +56 -48
  2. data/spec/geo_ip_spec.rb +1 -1
  3. metadata +25 -10
data/lib/geo_ip.rb CHANGED
@@ -4,62 +4,70 @@ COUNTRY_API = "ip_query_country.php"
4
4
  IPV4_REGEXP = /\A(?:25[0-5]|(?:2[0-4]|1\d|[1-9])?\d)(?:\.(?:25[0-5]|(?:2[0-4]|1\d|[1-9])?\d)){3}\z/
5
5
 
6
6
  require 'json'
7
- require 'uri'
8
- require 'net/http'
7
+ require 'rest-client'
9
8
 
10
9
  class GeoIp
11
-
12
10
  @@api_key = nil
11
+ @@timeout = 1
13
12
 
14
- def self.api_key
15
- @@api_key
16
- end
13
+ class << self
14
+ def api_key
15
+ @@api_key
16
+ end
17
17
 
18
- def self.api_key=(api_key)
19
- @@api_key = api_key
20
- end
18
+ def api_key= api_key
19
+ @@api_key = api_key
20
+ end
21
21
 
22
- # Retreive the remote location of a given ip address.
23
- #
24
- # It takes two optional arguments:
25
- # * +preceision+: can either be +:city+ (default) or +:country+
26
- # * +timezone+: can either be +false+ (default) or +true+
27
- #
28
- # ==== Example:
29
- # GeoIp.geolocation('209.85.227.104', {:precision => :city, :timezone => true})
30
- def self.geolocation(ip, options={})
31
- @precision = options[:precision] || :city
32
- @timezone = options[:timezone] || false
33
- raise "API key must be set first: GeoIp.api_key = 'YOURKEY'" if self.api_key.nil?
34
- raise "Invalid IP address" unless ip.to_s =~ IPV4_REGEXP
35
- raise "Invalid precision" unless [:country, :city].include?(@precision)
36
- raise "Invalid timezone" unless [true, false].include?(@timezone)
37
- uri = "#{SERVICE_URL}#{@country ? COUNTRY_API : CITY_API}?key=#{self.api_key}&ip=#{ip}&output=json&timezone=#{@timezone}"
38
- url = URI.parse(uri)
39
- reply = JSON.parse(Net::HTTP.get(url))
40
- location = convert_keys reply
41
- end
22
+ def timeout
23
+ @@timeout
24
+ end
25
+
26
+ def timeout= timeout
27
+ @@timeout = timeout
28
+ end
29
+
30
+ # Retreive the remote location of a given ip address.
31
+ #
32
+ # It takes two optional arguments:
33
+ # * +preceision+: can either be +:city+ (default) or +:country+
34
+ # * +timezone+: can either be +false+ (default) or +true+
35
+ #
36
+ # ==== Example:
37
+ # GeoIp.geolocation('209.85.227.104', {:precision => :city, :timezone => true})
38
+ def geolocation ip, options={}
39
+ @precision = options[:precision] || :city
40
+ @timezone = options[:timezone] || false
41
+ raise "API key must be set first: GeoIp.api_key = 'YOURKEY'" if self.api_key.nil?
42
+ raise "Invalid IP address" unless ip.to_s =~ IPV4_REGEXP
43
+ raise "Invalid precision" unless [:country, :city].include?(@precision)
44
+ raise "Invalid timezone" unless [true, false].include?(@timezone)
45
+ url = "#{SERVICE_URL}#{@country ? COUNTRY_API : CITY_API}?key=#{self.api_key}&ip=#{ip}&output=json&timezone=#{@timezone}"
46
+ reply = JSON.parse RestClient::Request.execute(:method => :get, :url => url, :timeout => self.timeout)
47
+ location = convert_keys reply
48
+ end
42
49
 
43
- private
44
- def self.convert_keys(hash)
45
- location = {}
46
- location[:ip] = hash["Ip"]
47
- location[:status] = hash["Status"]
48
- location[:country_code] = hash["CountryCode"]
49
- location[:country_name] = hash["CountryName"]
50
- if @precision == :city
51
- location[:region_code] = hash["RegionCode"]
52
- location[:region_name] = hash["RegionName"]
53
- location[:city] = hash["City"]
54
- location[:zip_postal_code] = hash["ZipPostalCode"]
55
- location[:latitude] = hash["Latitude"]
56
- location[:longitude] = hash["Longitude"]
57
- if @timezone
58
- location[:timezone_name] = hash["TimezoneName"]
59
- location[:utc_offset] = hash["Gmtoffset"].to_i
60
- location[:dst?] = hash["Isdst"] ? true : false
50
+ private
51
+ def convert_keys hash
52
+ location = {}
53
+ location[:ip] = hash["Ip"]
54
+ location[:status] = hash["Status"]
55
+ location[:country_code] = hash["CountryCode"]
56
+ location[:country_name] = hash["CountryName"]
57
+ if @precision == :city
58
+ location[:region_code] = hash["RegionCode"]
59
+ location[:region_name] = hash["RegionName"]
60
+ location[:city] = hash["City"]
61
+ location[:zip_postal_code] = hash["ZipPostalCode"]
62
+ location[:latitude] = hash["Latitude"]
63
+ location[:longitude] = hash["Longitude"]
64
+ if @timezone
65
+ location[:timezone_name] = hash["TimezoneName"]
66
+ location[:utc_offset] = hash["Gmtoffset"].to_i
67
+ location[:dst?] = hash["Isdst"] ? true : false
68
+ end
61
69
  end
70
+ location
62
71
  end
63
- location
64
72
  end
65
73
  end
data/spec/geo_ip_spec.rb CHANGED
@@ -83,8 +83,8 @@ describe "GeoIp" do
83
83
  it "should return the correct timezone information for a public ip address" do
84
84
  geolocation = GeoIp.geolocation(IP_GOOGLE_US, {:timezone => true})
85
85
  geolocation[:timezone_name].should == 'America/Los_Angeles'
86
- geolocation[:utc_offset].should == -28800
87
86
  geolocation[:dst?].should_not be_nil # true if dst?, false if not dst?
87
+ geolocation[:utc_offset].should == (geolocation[:dst?] ? -25200 : -28800)
88
88
  end
89
89
 
90
90
  it "should not return the timezone information when explicitly not requesting it" do
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: geo_ip
3
3
  version: !ruby/object:Gem::Version
4
- hash: 17
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 3
9
- - 1
10
- version: 0.3.1
9
+ - 2
10
+ version: 0.3.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Jeroen Jacobs
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-03-26 00:00:00 +01:00
18
+ date: 2011-05-20 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -26,18 +26,33 @@ dependencies:
26
26
  requirements:
27
27
  - - ~>
28
28
  - !ruby/object:Gem::Version
29
- hash: 11
29
+ hash: 7
30
30
  segments:
31
31
  - 1
32
32
  - 4
33
- - 6
34
- version: 1.4.6
33
+ version: "1.4"
35
34
  type: :runtime
36
35
  version_requirements: *id001
37
36
  - !ruby/object:Gem::Dependency
38
- name: rspec
37
+ name: rest-client
39
38
  prerelease: false
40
39
  requirement: &id002 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ~>
43
+ - !ruby/object:Gem::Version
44
+ hash: 13
45
+ segments:
46
+ - 1
47
+ - 6
48
+ - 1
49
+ version: 1.6.1
50
+ type: :runtime
51
+ version_requirements: *id002
52
+ - !ruby/object:Gem::Dependency
53
+ name: rspec
54
+ prerelease: false
55
+ requirement: &id003 !ruby/object:Gem::Requirement
41
56
  none: false
42
57
  requirements:
43
58
  - - ~>
@@ -48,7 +63,7 @@ dependencies:
48
63
  - 5
49
64
  version: "2.5"
50
65
  type: :development
51
- version_requirements: *id002
66
+ version_requirements: *id003
52
67
  description: A call to the ipinfodb.com will be done to retreive the geolocation based on the IP address. No need to include a database file in the application.
53
68
  email: gems@jeroenj.be
54
69
  executables: []
@@ -95,7 +110,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
95
110
  requirements: []
96
111
 
97
112
  rubyforge_project:
98
- rubygems_version: 1.5.0
113
+ rubygems_version: 1.4.2
99
114
  signing_key:
100
115
  specification_version: 3
101
116
  summary: Retreive the geolocation of an IP address based on the ipinfodb.com webservice