geo_ip 0.3.1 → 0.3.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/geo_ip.rb +56 -48
- data/spec/geo_ip_spec.rb +1 -1
- 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 '
|
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
|
-
|
15
|
-
|
16
|
-
|
13
|
+
class << self
|
14
|
+
def api_key
|
15
|
+
@@api_key
|
16
|
+
end
|
17
17
|
|
18
|
-
|
19
|
-
|
20
|
-
|
18
|
+
def api_key= api_key
|
19
|
+
@@api_key = api_key
|
20
|
+
end
|
21
21
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
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
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
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:
|
4
|
+
hash: 23
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 3
|
9
|
-
-
|
10
|
-
version: 0.3.
|
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-
|
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:
|
29
|
+
hash: 7
|
30
30
|
segments:
|
31
31
|
- 1
|
32
32
|
- 4
|
33
|
-
|
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:
|
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: *
|
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.
|
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
|