geo_ip_curb 0.4.0 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/geo_ip_curb.rb +21 -12
  2. metadata +5 -5
data/lib/geo_ip_curb.rb CHANGED
@@ -7,7 +7,7 @@ class GeoIpCurb
7
7
  COUNTRY_API = "ip-country"
8
8
  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/
9
9
  TIMEOUT = 10
10
- ERROR_PREFIX = "api.ipinfodb.com service error"
10
+ ERROR_PREFIX = "API service error"
11
11
 
12
12
  @@api_key = nil
13
13
 
@@ -22,25 +22,34 @@ class GeoIpCurb
22
22
  # Retreive the remote location of a given ip address.
23
23
  #
24
24
  # It takes two optional arguments:
25
- # * +preceision+: can either be +:city+ (default) or +:country+
26
- # * +timezone+: can either be +false+ (default) or +true+
25
+ # * +precision+: can either be +:city+ (default) or +:country+
26
+ # * +timeout+: number of seconds to wait before timing out
27
+ # * +service_url+: set the service URL, in case it changes, or you want to proxy it for additional caching, etc. Add 'APIKEYPLACEHOLDER' and 'IPPLACEHOLDER' in the right places and they will be replaced accordingly.
27
28
  #
28
29
  # ==== Example:
29
- # GeoIpCurb.geolocation('209.85.227.104', {:precision => :city, :timezone => true})
30
+ # GeoIpCurb.geolocation('209.85.227.104', {:precision => :city, :timeout => 5})
30
31
  def self.geolocation(ip, options={})
31
- @precision = options[:precision] || :country
32
- @timeout = options[:timeout] || TIMEOUT
33
-
32
+ @precision = options[:precision] || :country
33
+ @timeout = options[:timeout] || TIMEOUT
34
+ @service_url = options[:service_url] || nil
35
+
34
36
  raise "API key must be set first: GeoIpCurb.api_key = 'YOURKEY'" if self.api_key.nil?
35
37
  raise "Invalid IP address" unless ip.to_s =~ IPV4_REGEXP
36
38
  raise "Invalid precision" unless [:country, :city].include?(@precision)
37
-
38
- uri = "#{SERVICE_URL}/#{@precision==:country ? COUNTRY_API : CITY_API}/?key=#{self.api_key}&ip=#{ip}&format=json"
39
+
40
+ if @service_url then
41
+ @service_url = @service_url.sub("APIKEYPLACEHOLDER", self.api_key)
42
+ @service_url = @service_url.sub("IPPLACEHOLDER", ip)
43
+ uri = @service_url
44
+ else
45
+ uri = "#{SERVICE_URL}/#{@precision==:country ? COUNTRY_API : CITY_API}/?key=#{self.api_key}&ip=#{ip}&format=json"
46
+ end
47
+
39
48
  convert_keys send_request(uri)
40
49
  end
41
50
 
42
51
  private
43
-
52
+
44
53
  def self.send_request(uri)
45
54
  http = Curl::Easy.new(uri)
46
55
  http.timeout = @timeout
@@ -48,10 +57,10 @@ class GeoIpCurb
48
57
  JSON.parse(http.body_str)
49
58
  rescue => e
50
59
  error = {}
51
- error[:error_msg] = "#{ERROR_PREFIX}: \"#{e}\"."
60
+ error[:error_msg] = "#{ERROR_PREFIX}: #{e}."
52
61
  error
53
62
  end
54
-
63
+
55
64
  def self.convert_keys(hash)
56
65
  location = {}
57
66
  location[:ip] = hash["ipAddress"]
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: geo_ip_curb
3
3
  version: !ruby/object:Gem::Version
4
- hash: 15
4
+ hash: 13
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 4
9
- - 0
10
- version: 0.4.0
9
+ - 1
10
+ version: 0.4.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Ryan Conway
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-05-25 00:00:00 +01:00
18
+ date: 2011-06-17 00:00:00 +01:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -107,7 +107,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
107
107
  requirements: []
108
108
 
109
109
  rubyforge_project:
110
- rubygems_version: 1.5.0
110
+ rubygems_version: 1.6.2
111
111
  signing_key:
112
112
  specification_version: 3
113
113
  summary: Retrieve the geolocation of an IP address using the the ipinfodb.com v3 API service using Curb.