geokit 1.8.1 → 1.8.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,15 @@
1
- ---
2
- SHA512:
3
- data.tar.gz: 09020bb1e0b4fd79479277c02fa59d3ce9b443b679df55822ac06a50cb3848dfd1e093d43e23a0f03736eead51e2126adaf62104b3dc3563799ab912bf837a55
4
- metadata.gz: ad583bd4f48c42e5cc2880b7950e3f564ffc16f449efe359364892f23300cbe38803fd7435140329feaefacc85415b92428789e44ec5e7f8101e8f377013d14b
5
- SHA1:
6
- data.tar.gz: 657c7dbe8d563f7fc3ac2f3382d748bb1d551019
7
- metadata.gz: c10facc376715511adf49805a214ad0bd5a38678
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ NzdhNTJlNWE1YTg5MWI5MjMyOTRjMmNhOTQ0YzdmZmU3MzVmNzVkNA==
5
+ data.tar.gz: !binary |-
6
+ OTE1ZTA5NTg4NmU3NDdjZWNkZWM0ZmVhYmE3NjkzYWIzMzMxMGI1ZQ==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ ODdlMDUwNjQxNzk0MDJhODNmZGRlMzYzNDUyZmRiNDYwZDU0ZWYzOGUxNGU4
10
+ MWIxMTc3MDQ3ZTc1NDM5OTI0MzUxMjVmOTNkODliN2M5M2M1ZTM0NDE4ZTRh
11
+ OGIxN2ZlMGYxN2UxNGFkMDU5YzU3ZGUyNDQ1N2JkYjNhNThmZTA=
12
+ data.tar.gz: !binary |-
13
+ YTQyNWVlOTk5OWY4ODkxNjMxNDk0NDhhZGM1MGZiYjE5ODBlZDEzZGI0Zjc4
14
+ ZDBkNzQ3MTJmMWU5M2JjMjc5YzQ4ODM3NzBjYWNkZjEyY2ZhZDAwM2ExOTNk
15
+ NTM4M2M4ZDNhYTZkNDMyYzM5OGZiODJiNDBkNTJkMmU2Y2I1NTc=
@@ -1,3 +1,8 @@
1
+ ## 1.8.2
2
+
3
+ * Fix due to name clash with dependency definitions in geokit 1.8.1
4
+ * Standaride GeoLoc provider string
5
+
1
6
  ## 1.8.1
2
7
 
3
8
  * Change way keys/dependencies defined
@@ -93,20 +93,28 @@ module Geokit
93
93
  # are responsible for implementing. Returns a populated GeoLoc or an
94
94
  # empty one with a failed success code.
95
95
  def self.geocode(address, *args)
96
+ logger.debug "#{provider_name} geocoding. address: #{address}, args #{args}"
96
97
  do_geocode(address, *args) || GeoLoc.new
97
98
  rescue TooManyQueriesError, GeocodeError
98
99
  raise
99
100
  rescue
100
- logger.error "Caught an error during #{self.class} geocoding call: #{$!}"
101
+ logger.error "Caught an error during #{provider_name} geocoding call: #{$!}"
101
102
  GeoLoc.new
102
103
  end
103
104
  # Main method which calls the do_reverse_geocode template method which subclasses
104
105
  # are responsible for implementing. Returns a populated GeoLoc or an
105
106
  # empty one with a failed success code.
106
107
  def self.reverse_geocode(latlng)
108
+ logger.debug "#{provider_name} geocoding. latlng: #{latlng}"
107
109
  do_reverse_geocode(latlng) || GeoLoc.new
108
110
  end
109
111
 
112
+ def self.new_loc
113
+ loc = GeoLoc.new
114
+ loc.provider = Geokit::Inflector.underscore(provider_name)
115
+ loc
116
+ end
117
+
110
118
  # Call the geocoder service using the timeout if configured.
111
119
  def self.call_geocoder_service(url)
112
120
  Timeout::timeout(Geokit::Geocoders::request_timeout) { return self.do_get(url) } if Geokit::Geocoders::request_timeout
@@ -143,11 +151,17 @@ module Geokit
143
151
  Net::HTTP::new(*net_http_args).start { |http| http.request(req) }
144
152
  end
145
153
 
154
+ def self.provider_name
155
+ name.split('::').last.gsub(/Geocoder$/, '')
156
+ end
157
+
146
158
  def self.parse(format, body, *args)
159
+ logger.debug "#{provider_name} geocoding. Result: #{CGI.escape(body)}"
147
160
  case format
148
161
  when :json then parse_json(MultiJson.load(body), *args)
149
162
  when :xml then parse_xml(REXML::Document.new(body), *args)
150
163
  when :yaml then parse_yaml(YAML::load(body), *args)
164
+ when :csv then parse_csv(body.chomp.split(','), *args)
151
165
  end
152
166
  end
153
167
 
@@ -157,6 +171,12 @@ module Geokit
157
171
  end
158
172
  end
159
173
 
174
+ def self.process(format, url, *args)
175
+ res = call_geocoder_service(url)
176
+ return GeoLoc.new if !res.is_a?(Net::HTTPSuccess)
177
+ parse format, res.body, *args
178
+ end
179
+
160
180
  def self.transcode_to_utf8(body)
161
181
  require 'iconv' unless String.method_defined?(:encode)
162
182
  if String.method_defined?(:encode)
@@ -30,6 +30,11 @@ module Geokit
30
30
  /^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})?$/.match(ip)
31
31
  end
32
32
 
33
+ def self.process(format, ip)
34
+ return GeoLoc.new unless valid_ip?(ip)
35
+ super(format, submit_url(ip))
36
+ end
37
+
33
38
  # Checks whether the IP address belongs to a private address range.
34
39
  #
35
40
  # This function is used to reduce the number of useless queries made to
@@ -13,7 +13,6 @@ module Geokit
13
13
  res = call_geocoder_service(url)
14
14
  return GeoLoc.new if !res.is_a?(Net::HTTPSuccess)
15
15
  xml = transcode_to_utf8(res.body)
16
- logger.debug "Bing geocoding. Address: #{address}. Result: #{xml}"
17
16
  parse :xml, xml
18
17
  end
19
18
 
@@ -38,8 +37,7 @@ module Geokit
38
37
 
39
38
  # extracts a single geoloc from a //Location element in the bing results xml
40
39
  def self.extract_location(xml)
41
- loc = GeoLoc.new
42
- loc.provider = 'bing'
40
+ loc = new_loc
43
41
  set_address_components(loc, xml)
44
42
  set_precision(loc, xml)
45
43
  set_bounds(loc, xml)
@@ -19,12 +19,7 @@ module Geokit
19
19
  # Template method which does the geocode lookup.
20
20
  def self.do_geocode(loc)
21
21
  raise ArgumentError('Geocoder.ca requires a GeoLoc argument') unless loc.is_a?(GeoLoc)
22
- url = submit_url(loc)
23
- res = call_geocoder_service(url)
24
- return GeoLoc.new if !res.is_a?(Net::HTTPSuccess)
25
- xml = res.body
26
- logger.debug "Geocoder.ca geocoding. Address: #{loc}. Result: #{xml}"
27
- parse :xml, xml, loc
22
+ process :xml, submit_url(loc), loc
28
23
  end
29
24
 
30
25
  def self.parse_xml(xml, loc)
@@ -7,11 +7,7 @@ module Geokit
7
7
  def self.do_reverse_geocode(latlng)
8
8
  latlng=LatLng.normalize(latlng)
9
9
  url = "http://data.fcc.gov/api/block/find?format=json&latitude=#{Geokit::Inflector::url_escape(latlng.lat.to_s)}&longitude=#{Geokit::Inflector::url_escape(latlng.lng.to_s)}"
10
- res = call_geocoder_service(url)
11
- return GeoLoc.new if !res.is_a?(Net::HTTPSuccess)
12
- json = res.body
13
- logger.debug "FCC reverse-geocoding. LL: #{latlng}. Result: #{json}"
14
- parse :json, json
10
+ process :json, url
15
11
  end
16
12
 
17
13
  # Template method which does the geocode lookup.
@@ -36,8 +32,7 @@ module Geokit
36
32
  raise Geokit::Geocoders::GeocodeError
37
33
  end
38
34
 
39
- loc = GeoLoc.new
40
- loc.provider = 'fcc'
35
+ loc = new_loc
41
36
  loc.success = true
42
37
  loc.precision = 'block'
43
38
  loc.country_code = 'US'
@@ -5,11 +5,11 @@ module Geokit
5
5
  private
6
6
 
7
7
  def self.do_geocode(ip)
8
- return GeoLoc.new unless valid_ip?(ip)
9
- url = "http://freegeoip.net/xml/#{ip}"
10
- res = call_geocoder_service(url)
11
- return GeoLoc.new if !res.is_a?(Net::HTTPSuccess)
12
- parse :xml, res.body
8
+ process :xml, ip
9
+ end
10
+
11
+ def self.submit_url(ip)
12
+ "http://freegeoip.net/xml/#{ip}"
13
13
  end
14
14
 
15
15
  XML_MAPPINGS = {
@@ -22,8 +22,7 @@ module Geokit
22
22
  }
23
23
 
24
24
  def self.parse_xml(xml)
25
- loc = GeoLoc.new
26
- loc.provider = 'freegeoip'
25
+ loc = new_loc
27
26
  set_mappings(loc, xml.elements['Response'], XML_MAPPINGS)
28
27
  loc.success = !!loc.city && !loc.city.empty?
29
28
  loc
@@ -5,11 +5,11 @@ module Geokit
5
5
  private
6
6
 
7
7
  def self.do_geocode(ip)
8
- return GeoLoc.new unless valid_ip?(ip)
9
- url = "http://www.geoplugin.net/xml.gp?ip=#{ip}"
10
- res = call_geocoder_service(url)
11
- return GeoLoc.new if !res.is_a?(Net::HTTPSuccess)
12
- parse :xml, res.body
8
+ process :xml, ip
9
+ end
10
+
11
+ def self.submit_url(ip)
12
+ "http://www.geoplugin.net/xml.gp?ip=#{ip}"
13
13
  end
14
14
 
15
15
  XML_MAPPINGS = {
@@ -21,8 +21,7 @@ module Geokit
21
21
  }
22
22
 
23
23
  def self.parse_xml(xml)
24
- loc = GeoLoc.new
25
- loc.provider = 'geoPlugin'
24
+ loc = new_loc
26
25
  set_mappings(loc, xml.elements['geoPlugin'], XML_MAPPINGS)
27
26
  loc.success = !!loc.city && !loc.city.empty?
28
27
  loc
@@ -9,13 +9,7 @@ module Geokit
9
9
 
10
10
  # Template method which does the geocode lookup.
11
11
  def self.do_geocode(address)
12
- url = submit_url(address)
13
- res = call_geocoder_service(url)
14
- return GeoLoc.new if !res.is_a?(Net::HTTPSuccess)
15
-
16
- xml = res.body
17
- logger.debug "Geonames geocoding. Address: #{address}. Result: #{xml}"
18
- parse :xml, xml
12
+ process :xml, submit_url(address)
19
13
  end
20
14
 
21
15
  def self.submit_url(address)
@@ -42,8 +36,7 @@ module Geokit
42
36
 
43
37
  def self.parse_xml(xml)
44
38
  return GeoLoc.new unless xml.elements['geonames/totalResultsCount'].text.to_i > 0
45
- loc = GeoLoc.new
46
- loc.provider = 'genomes'
39
+ loc = new_loc
47
40
  # only take the first result
48
41
  set_mappings(loc, xml.elements['geonames/code'], XML_MAPPINGS)
49
42
  loc.success = true
@@ -8,11 +8,7 @@ module Geokit
8
8
  def self.do_reverse_geocode(latlng)
9
9
  latlng=LatLng.normalize(latlng)
10
10
  url = submit_url("/maps/api/geocode/json?sensor=false&latlng=#{Geokit::Inflector::url_escape(latlng.ll)}")
11
- res = call_geocoder_service(url)
12
- return GeoLoc.new if !res.is_a?(Net::HTTPSuccess)
13
- json = res.body
14
- logger.debug "Google reverse-geocoding. LL: #{latlng}. Result: #{CGI.escape(json)}"
15
- parse :json, json
11
+ process :json, url
16
12
  end
17
13
 
18
14
  # Template method which does the geocode lookup.
@@ -48,11 +44,7 @@ module Geokit
48
44
 
49
45
  res = call_geocoder_service(url)
50
46
  return GeoLoc.new if !res.is_a?(Net::HTTPSuccess)
51
-
52
- json = res.body
53
- logger.debug "Google geocoding. Address: #{address}. Result: #{CGI.escape(json)}"
54
-
55
- parse :json, json
47
+ parse :json, res.body
56
48
  end
57
49
 
58
50
  # This code comes from Googles Examples
@@ -71,8 +63,8 @@ module Geokit
71
63
 
72
64
  def self.submit_url(query_string)
73
65
  if client_id && cryptographic_key
74
- channel = channel ? "&channel=#{channel}" : ''
75
- urlToSign = query_string + "&client=#{client_id}" + channel
66
+ channel_string = channel ? "&channel=#{channel}" : ''
67
+ urlToSign = query_string + "&client=#{client_id}" + channel_string
76
68
  signature = sign_gmap_bus_api_url(urlToSign, cryptographic_key)
77
69
  "http://maps.googleapis.com" + urlToSign + "&signature=#{signature}"
78
70
  else
@@ -138,8 +130,7 @@ module Geokit
138
130
  }
139
131
 
140
132
  def self.single_json_to_geoloc(addr)
141
- loc = GeoLoc.new
142
- loc.provider = 'google'
133
+ loc = new_loc
143
134
  loc.success = true
144
135
  loc.full_address = addr['formatted_address']
145
136
 
@@ -11,15 +11,19 @@ module Geokit
11
11
  # parameter does not match an ip address.
12
12
  def self.do_geocode(ip)
13
13
  return GeoLoc.new unless valid_ip?(ip)
14
- url = "http://api.hostip.info/get_html.php?ip=#{ip}&position=true"
14
+ url = submit_url(ip)
15
15
  res = call_geocoder_service(url)
16
- ensure_utf8_encoding(res)
17
16
  return GeoLoc.new if !res.is_a?(Net::HTTPSuccess)
17
+ ensure_utf8_encoding(res)
18
18
  body = res.body
19
19
  body = body.encode('UTF-8') if body.respond_to? :encode
20
20
  parse :yaml, body
21
21
  end
22
22
 
23
+ def self.submit_url(ip)
24
+ "http://api.hostip.info/get_html.php?ip=#{ip}&position=true"
25
+ end
26
+
23
27
  # Converts the body to YAML since its in the form of:
24
28
  #
25
29
  # Country: UNITED STATES (US)
@@ -29,8 +33,7 @@ module Geokit
29
33
  #
30
34
  # then instantiates a GeoLoc instance to populate with location data.
31
35
  def self.parse_yaml(yaml) # :nodoc:
32
- loc = GeoLoc.new
33
- loc.provider = 'hostip'
36
+ loc = new_loc
34
37
  loc.city, loc.state = yaml['City'].split(', ')
35
38
  loc.country, loc.country_code = yaml['Country'].split(' (')
36
39
  loc.lat = yaml['Latitude']
@@ -11,22 +11,14 @@ module Geokit
11
11
  def self.do_reverse_geocode(latlng)
12
12
  latlng=LatLng.normalize(latlng)
13
13
  url = "http://www.mapquestapi.com/geocoding/v1/reverse?key=#{key}&location=#{latlng.lat},#{latlng.lng}"
14
- res = call_geocoder_service(url)
15
- return GeoLoc.new if !res.is_a?(Net::HTTPSuccess)
16
- json = res.body
17
- logger.debug "MapQuest reverse-geocoding. LL: #{latlng}. Result: #{json}"
18
- parse :json, json, latlng
14
+ process :json, url
19
15
  end
20
16
 
21
17
  # Template method which does the geocode lookup.
22
18
  def self.do_geocode(address)
23
19
  address_str = address.is_a?(GeoLoc) ? address.to_geocodeable_s : address
24
20
  url = "http://www.mapquestapi.com/geocoding/v1/address?key=#{key}&location=#{Geokit::Inflector::url_escape(address_str)}"
25
- res = call_geocoder_service(url)
26
- return GeoLoc.new if !res.is_a?(Net::HTTPSuccess)
27
- json = res.body
28
- logger.debug "Mapquest geocoding. Address: #{address}. Result: #{json}"
29
- parse :json, json
21
+ process :json, url
30
22
  end
31
23
 
32
24
  def self.parse_json(results)
@@ -46,10 +38,9 @@ module Geokit
46
38
  end
47
39
 
48
40
  def self.extract_geoloc(result_json)
49
- loc = GeoLoc.new
50
- loc.lat = result_json['latLng']['lat']
51
- loc.lng = result_json['latLng']['lng']
52
- loc.provider = 'mapquest'
41
+ loc = new_loc
42
+ loc.lat = result_json['latLng']['lat']
43
+ loc.lng = result_json['latLng']['lng']
53
44
  set_address_components(result_json, loc)
54
45
  set_precision(result_json, loc)
55
46
  loc.success = true
@@ -1,25 +1,21 @@
1
1
  module Geokit
2
2
  module Geocoders
3
-
4
- @@geoip_data_path = 'DEFINE_THE_PATH_TO_GeoLiteCity.dat'
5
- __define_accessors
6
-
7
3
  # Provides geocoding based upon an IP address. The underlying web service is MaxMind
8
4
  class MaxmindGeocoder < Geocoder
5
+ config :geoip_data_path # path to GeoLiteCity.dat
6
+
9
7
  private
10
8
 
11
9
  def self.do_geocode(ip)
12
- res = GeoIP.new(Geokit::Geocoders::geoip_data_path).city(ip)
10
+ res = GeoIP.new(geoip_data_path).city(ip)
13
11
 
14
- loc = GeoLoc.new(
15
- :provider => 'maxmind_city',
16
- :lat => res.latitude,
17
- :lng => res.longitude,
18
- :city => res.city_name,
19
- :state => res.region_name,
20
- :zip => res.postal_code,
21
- :country_code => res.country_code2
22
- )
12
+ loc = new_loc
13
+ loc.lat = res.latitude
14
+ loc.lng = res.longitude
15
+ loc.city = res.city_name
16
+ loc.state = res.region_name
17
+ loc.zip = res.postal_code
18
+ loc.country_code = res.country_code2
23
19
 
24
20
  loc.success = ( res.longitude.kind_of?(Numeric) && res.latitude.kind_of?(Numeric) )
25
21
  loc
@@ -15,11 +15,7 @@ module Geokit
15
15
  address_str = address.is_a?(GeoLoc) ? address.to_geocodeable_s : address
16
16
 
17
17
  url = "http://nominatim.openstreetmap.org/search?format=json#{options_str}&addressdetails=1&q=#{Geokit::Inflector::url_escape(address_str)}"
18
- res = call_geocoder_service(url)
19
- return GeoLoc.new if !res.is_a?(Net::HTTPSuccess)
20
- json = res.body
21
- logger.debug "OSM geocoding. Address: #{address}. Result: #{json}"
22
- parse :json, json
18
+ process :json, url
23
19
  end
24
20
 
25
21
  def self.do_reverse_geocode(latlng, options = {})
@@ -31,11 +27,7 @@ module Geokit
31
27
  options_str << generate_param_for_option(:osm_id, options)
32
28
  options_str << generate_param_for_option(:json_callback, options)
33
29
  url = "http://nominatim.openstreetmap.org/reverse?format=json&addressdetails=1#{options_str}"
34
- res = call_geocoder_service(url)
35
- return GeoLoc.new if !res.is_a?(Net::HTTPSuccess)
36
- json = res.body
37
- logger.debug "OSM reverse geocoding: Lat: #{latlng.lat}, Lng: #{latlng.lng}. Result: #{json}"
38
- parse :json, json
30
+ process :json, url
39
31
  end
40
32
 
41
33
  def self.generate_param_for(param, value)
@@ -70,14 +62,12 @@ module Geokit
70
62
  end
71
63
 
72
64
  def self.extract_geoloc(result_json)
73
- loc = GeoLoc.new
65
+ loc = new_loc
74
66
 
75
67
  # basic
76
68
  loc.lat = result_json['lat']
77
69
  loc.lng = result_json['lon']
78
70
 
79
- loc.provider = 'osm'
80
-
81
71
  set_address_components(result_json['address'], loc)
82
72
  set_precision(result_json, loc)
83
73
  set_bounds(result_json['boundingbox'], loc)
@@ -5,18 +5,17 @@ module Geokit
5
5
  private
6
6
 
7
7
  def self.do_geocode(ip)
8
- return GeoLoc.new unless valid_ip?(ip)
9
- url = "http://stat.ripe.net/data/geoloc/data.json?resource=#{ip}"
10
- res = call_geocoder_service(url)
11
- return GeoLoc.new if !res.is_a?(Net::HTTPSuccess)
12
- parse :json, res.body
8
+ process :json, ip
9
+ end
10
+
11
+ def self.submit_url(ip)
12
+ "http://stat.ripe.net/data/geoloc/data.json?resource=#{ip}"
13
13
  end
14
14
 
15
15
  def self.parse_json(json)
16
- loc = GeoLoc.new
16
+ loc = new_loc
17
17
  data = json['data']['locations'][0]
18
18
 
19
- loc.provider='RIPE'
20
19
  loc.lat = data['latitude']
21
20
  loc.lng = data['longitude']
22
21
  set_address_components(data, loc)
@@ -8,27 +8,17 @@ module Geokit
8
8
 
9
9
  private
10
10
  def self.do_geocode(address)
11
- address_str = address.is_a?(GeoLoc) ? address.to_geocodeable_s : address
11
+ process :csv, submit_url(address)
12
+ end
12
13
 
14
+ def self.submit_url(address)
15
+ address_str = address.is_a?(GeoLoc) ? address.to_geocodeable_s : address
13
16
  query = (address_str =~ /^\d{5}(?:-\d{4})?$/ ? "zip" : "address") + "=#{Geokit::Inflector::url_escape(address_str)}"
14
- url = if key
15
- "http://#{key}@geocoder.us/member/service/csv/geocode"
16
- else
17
- "http://geocoder.us/service/csv/geocode"
18
- end
19
-
20
- url = "#{url}?#{query}"
21
- res = call_geocoder_service(url)
22
-
23
- return GeoLoc.new if !res.is_a?(Net::HTTPSuccess)
24
- data = res.body
25
- logger.debug "Geocoder.us geocoding. Address: #{address}. Result: #{data}"
26
- parse_csv data
17
+ base = key ? "http://#{key}@geocoder.us/member" : "http://geocoder.us"
18
+ "#{base}/service/csv/geocode?#{query}"
27
19
  end
28
20
 
29
- def self.parse_csv(data)
30
- array = data.chomp.split(',')
31
-
21
+ def self.parse_csv(array)
32
22
  loc = GeoLoc.new
33
23
  if array.length == 5
34
24
  loc.lat,loc.lng,loc.city,loc.state,loc.zip=array
@@ -20,12 +20,7 @@ module Geokit
20
20
 
21
21
  # Template method which does the geocode lookup.
22
22
  def self.do_geocode(address)
23
- url = submit_url(address)
24
- res = call_geocoder_service(url)
25
- return GeoLoc.new if !res.is_a?(Net::HTTPSuccess)
26
- json = res.body
27
- logger.debug "Yahoo geocoding. Address: #{address}. Result: #{json}"
28
- parse :json, json
23
+ process :json, submit_url(address)
29
24
  end
30
25
 
31
26
  def self.parse_json(results)
@@ -44,10 +39,9 @@ module Geokit
44
39
  end
45
40
 
46
41
  def self.extract_geoloc(result_json)
47
- loc = GeoLoc.new
42
+ loc = new_loc
48
43
  loc.lat = result_json['latitude']
49
44
  loc.lng = result_json['longitude']
50
- loc.provider = 'yahoo'
51
45
  set_address_components(result_json, loc)
52
46
  set_precision(result_json, loc)
53
47
  loc.success = true
@@ -9,21 +9,18 @@ module Geokit
9
9
 
10
10
  # Template method which does the geocode lookup.
11
11
  def self.do_geocode(address)
12
- address_str = address.is_a?(GeoLoc) ? address.to_geocodeable_s : address
13
- url = submit_url(address_str)
14
- res = call_geocoder_service(url)
15
- return GeoLoc.new if !res.is_a?(Net::HTTPSuccess)
16
- parse :json, res.body
12
+ process :json, submit_url(address)
17
13
  end
18
14
 
19
- def self.submit_url(address_str)
15
+ def self.submit_url(address)
16
+ address_str = address.is_a?(GeoLoc) ? address.to_geocodeable_s : address
20
17
  url = "http://geocode-maps.yandex.ru/1.x/?geocode=#{Geokit::Inflector::url_escape(address_str)}&format=json"
21
18
  url += "&key=#{key}" if key
22
19
  url
23
20
  end
24
21
 
25
22
  def self.parse_json(result)
26
- loc = GeoLoc.new
23
+ loc = new_loc
27
24
 
28
25
  coll = result["response"]["GeoObjectCollection"]
29
26
  return loc unless coll["metaDataProperty"]["GeocoderResponseMetaData"]["found"].to_i > 0
@@ -31,9 +28,9 @@ module Geokit
31
28
  l = coll["featureMember"][0]["GeoObject"]
32
29
 
33
30
  loc.success = true
34
- loc.provider = "yandex"
35
- loc.lng = l["Point"]["pos"].split(" ").first
36
- loc.lat = l["Point"]["pos"].split(" ").last
31
+ ll = l["Point"]["pos"].split(" ")
32
+ loc.lng = ll.first
33
+ loc.lat = ll.last
37
34
 
38
35
  country = l["metaDataProperty"]["GeocoderMetaData"]["AddressDetails"]["Country"]
39
36
  locality = country["Locality"] || country["AdministrativeArea"]["Locality"] || country["AdministrativeArea"]["SubAdministrativeArea"]["Locality"] rescue nil
@@ -1,3 +1,3 @@
1
1
  module Geokit
2
- VERSION = '1.8.1'
2
+ VERSION = '1.8.2'
3
3
  end
@@ -1,9 +1,28 @@
1
1
  require File.join(File.dirname(__FILE__), 'helper')
2
2
 
3
3
  class GeoPluginGeocoderTest < BaseGeocoderTest #:nodoc: all
4
+ IP_SUCCESS=<<-EOF
5
+ <?xml version="1.0" encoding="UTF-8"?>
6
+ <geoPlugin>
7
+ <geoplugin_city>Belo Horizonte</geoplugin_city>
8
+ <geoplugin_region>Minas Gerais</geoplugin_region>
9
+ <geoplugin_areaCode>0</geoplugin_areaCode>
10
+ <geoplugin_dmaCode>0</geoplugin_dmaCode>
11
+ <geoplugin_countryCode>BR</geoplugin_countryCode>
12
+ <geoplugin_countryName>Brazil</geoplugin_countryName>
13
+ <geoplugin_continentCode>SA</geoplugin_continentCode>
14
+ <geoplugin_latitude>-19.916700</geoplugin_latitude>
15
+ <geoplugin_longitude>-43.933300</geoplugin_longitude>
16
+ <geoplugin_currencyCode>BRL</geoplugin_currencyCode>
17
+ <geoplugin_currencySymbol>&#82;&#36;</geoplugin_currencySymbol>
18
+ <geoplugin_currencyConverter>2.2575001717</geoplugin_currencyConverter>
19
+ </geoPlugin>
20
+ EOF
21
+
4
22
  def setup
5
23
  super
6
24
  @ip = '74.125.237.209'
25
+ @success.provider = "geo_plugin"
7
26
  end
8
27
 
9
28
  def assert_url(expected_url)
@@ -20,4 +39,35 @@ class GeoPluginGeocoderTest < BaseGeocoderTest #:nodoc: all
20
39
  assert_equal res.country_code, 'US'
21
40
  end
22
41
  end
42
+
43
+ def test_successful_lookup
44
+ success = MockSuccess.new
45
+ success.expects(:body).returns(IP_SUCCESS)
46
+ url = 'http://www.geoplugin.net/xml.gp?ip=200.150.38.66'
47
+ Geokit::Geocoders::GeoPluginGeocoder.expects(:call_geocoder_service).with(url).returns(success)
48
+ location = Geokit::Geocoders::GeoPluginGeocoder.geocode('200.150.38.66')
49
+ assert_not_nil location
50
+ assert_equal(-19.916700, location.lat)
51
+ assert_equal(-43.933300, location.lng)
52
+ assert_equal "Belo Horizonte", location.city
53
+ assert_equal "Minas Gerais", location.state
54
+ assert_equal "BR", location.country_code
55
+ assert_equal "geo_plugin", location.provider
56
+ assert location.success?
57
+ end
58
+
59
+ def test_invalid_ip
60
+ location = Geokit::Geocoders::GeoPluginGeocoder.geocode("pixrum")
61
+ assert_not_nil location
62
+ assert !location.success?
63
+ end
64
+
65
+ def test_service_unavailable
66
+ failure = MockFailure.new
67
+ url = 'http://www.geoplugin.net/xml.gp?ip=69.10.10.10'
68
+ Geokit::Geocoders::GeoPluginGeocoder.expects(:call_geocoder_service).with(url).returns(failure)
69
+ location = Geokit::Geocoders::GeoPluginGeocoder.geocode("69.10.10.10")
70
+ assert_not_nil location
71
+ assert !location.success?
72
+ end
23
73
  end
@@ -45,7 +45,7 @@ class IpGeocoderTest < BaseGeocoderTest #:nodoc: all
45
45
 
46
46
  def setup
47
47
  super
48
- @success.provider = "hostip"
48
+ @success.provider = "ip"
49
49
  end
50
50
 
51
51
  def test_successful_lookup
@@ -60,7 +60,7 @@ class IpGeocoderTest < BaseGeocoderTest #:nodoc: all
60
60
  assert_equal "Sugar Grove", location.city
61
61
  assert_equal "IL", location.state
62
62
  assert_equal "US", location.country_code
63
- assert_equal "hostip", location.provider
63
+ assert_equal "ip", location.provider
64
64
  assert location.success?
65
65
  end
66
66
 
@@ -76,7 +76,7 @@ class IpGeocoderTest < BaseGeocoderTest #:nodoc: all
76
76
  assert_equal "Bor\303\245s", location.city
77
77
  assert_nil location.state
78
78
  assert_equal "SE", location.country_code
79
- assert_equal "hostip", location.provider
79
+ assert_equal "ip", location.provider
80
80
  assert location.success?
81
81
  end
82
82
 
@@ -93,7 +93,7 @@ class IpGeocoderTest < BaseGeocoderTest #:nodoc: all
93
93
  assert_equal "São José Do Rio Prêto", location.city
94
94
  assert_nil location.state
95
95
  assert_equal "BR", location.country_code
96
- assert_equal "hostip", location.provider
96
+ assert_equal "ip", location.provider
97
97
  assert location.success?
98
98
  end
99
99
 
metadata CHANGED
@@ -1,9 +1,9 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: geokit
3
- version: !ruby/object:Gem::Version
4
- version: 1.8.1
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.8.2
5
5
  platform: ruby
6
- authors:
6
+ authors:
7
7
  - Michael Noack
8
8
  - James Cox
9
9
  - Andre Lewis
@@ -11,98 +11,143 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
-
15
- date: 2013-12-26 00:00:00 Z
16
- dependencies:
17
- - !ruby/object:Gem::Dependency
18
- version_requirements: &id001 !ruby/object:Gem::Requirement
19
- requirements:
20
- - - ">="
21
- - !ruby/object:Gem::Version
14
+ date: 2013-12-26 00:00:00.000000000 Z
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: multi_json
18
+ requirement: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
22
  version: 1.3.2
23
23
  type: :runtime
24
- name: multi_json
25
24
  prerelease: false
26
- requirement: *id001
27
- - !ruby/object:Gem::Dependency
28
- version_requirements: &id002 !ruby/object:Gem::Requirement
29
- requirements:
30
- - - ">"
31
- - !ruby/object:Gem::Version
32
- version: "1.0"
33
- type: :development
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 1.3.2
30
+ - !ruby/object:Gem::Dependency
34
31
  name: bundler
35
- prerelease: false
36
- requirement: *id002
37
- - !ruby/object:Gem::Dependency
38
- version_requirements: &id003 !ruby/object:Gem::Requirement
39
- requirements:
40
- - &id004
41
- - ">="
42
- - !ruby/object:Gem::Version
43
- version: "0"
32
+ requirement: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - ! '>'
35
+ - !ruby/object:Gem::Version
36
+ version: '1.0'
44
37
  type: :development
45
- name: simplecov
46
38
  prerelease: false
47
- requirement: *id003
48
- - !ruby/object:Gem::Dependency
49
- version_requirements: &id005 !ruby/object:Gem::Requirement
50
- requirements:
51
- - *id004
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ! '>'
42
+ - !ruby/object:Gem::Version
43
+ version: '1.0'
44
+ - !ruby/object:Gem::Dependency
45
+ name: simplecov
46
+ requirement: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ! '>='
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
52
51
  type: :development
53
- name: simplecov-rcov
54
52
  prerelease: false
55
- requirement: *id005
56
- - !ruby/object:Gem::Dependency
57
- version_requirements: &id006 !ruby/object:Gem::Requirement
58
- requirements:
59
- - *id004
53
+ version_requirements: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ! '>='
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ - !ruby/object:Gem::Dependency
59
+ name: simplecov-rcov
60
+ requirement: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ! '>='
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
60
65
  type: :development
61
- name: rake
62
66
  prerelease: false
63
- requirement: *id006
64
- - !ruby/object:Gem::Dependency
65
- version_requirements: &id007 !ruby/object:Gem::Requirement
66
- requirements:
67
- - *id004
67
+ version_requirements: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ! '>='
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ - !ruby/object:Gem::Dependency
73
+ name: rake
74
+ requirement: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ! '>='
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
68
79
  type: :development
69
- name: mocha
70
80
  prerelease: false
71
- requirement: *id007
72
- - !ruby/object:Gem::Dependency
73
- version_requirements: &id008 !ruby/object:Gem::Requirement
74
- requirements:
75
- - *id004
81
+ version_requirements: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ - !ruby/object:Gem::Dependency
87
+ name: mocha
88
+ requirement: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ! '>='
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
76
93
  type: :development
77
- name: coveralls
78
94
  prerelease: false
79
- requirement: *id008
80
- - !ruby/object:Gem::Dependency
81
- version_requirements: &id009 !ruby/object:Gem::Requirement
82
- requirements:
83
- - *id004
95
+ version_requirements: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ! '>='
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ - !ruby/object:Gem::Dependency
101
+ name: coveralls
102
+ requirement: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ! '>='
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
84
107
  type: :development
85
- name: vcr
86
108
  prerelease: false
87
- requirement: *id009
88
- - !ruby/object:Gem::Dependency
89
- version_requirements: &id010 !ruby/object:Gem::Requirement
90
- requirements:
91
- - *id004
109
+ version_requirements: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ! '>='
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ - !ruby/object:Gem::Dependency
115
+ name: vcr
116
+ requirement: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ! '>='
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
92
121
  type: :development
122
+ prerelease: false
123
+ version_requirements: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - ! '>='
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ - !ruby/object:Gem::Dependency
93
129
  name: webmock
130
+ requirement: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - ! '>='
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ type: :development
94
136
  prerelease: false
95
- requirement: *id010
96
- description: Geokit provides geocoding and distance calculation in an easy-to-use API
97
- email:
137
+ version_requirements: !ruby/object:Gem::Requirement
138
+ requirements:
139
+ - - ! '>='
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
142
+ description: Geokit provides geocoding and distance calculation in an easy-to-use
143
+ API
144
+ email:
98
145
  - michael+geokit@noack.com.au
99
146
  executables: []
100
-
101
147
  extensions: []
102
-
103
- extra_rdoc_files:
148
+ extra_rdoc_files:
104
149
  - README.markdown
105
- files:
150
+ files:
106
151
  - .gitignore
107
152
  - .travis.yml
108
153
  - CHANGELOG.md
@@ -167,7 +212,6 @@ files:
167
212
  - test/test_geo_plugin_geocoder.rb
168
213
  - test/test_geoloc.rb
169
214
  - test/test_geonames_geocoder.rb
170
- - test/test_geoplugin_geocoder.rb
171
215
  - test/test_google_geocoder.rb
172
216
  - test/test_inflector.rb
173
217
  - test/test_ipgeocoder.rb
@@ -182,30 +226,32 @@ files:
182
226
  - test/test_yahoo_geocoder.rb
183
227
  - test/test_yandex_geocoder.rb
184
228
  homepage: http://github.com/geokit/geokit
185
- licenses:
229
+ licenses:
186
230
  - MIT
187
231
  metadata: {}
188
-
189
232
  post_install_message:
190
- rdoc_options:
233
+ rdoc_options:
191
234
  - --main
192
235
  - README.markdown
193
- require_paths:
236
+ require_paths:
194
237
  - lib
195
- required_ruby_version: !ruby/object:Gem::Requirement
196
- requirements:
197
- - *id004
198
- required_rubygems_version: !ruby/object:Gem::Requirement
199
- requirements:
200
- - *id004
238
+ required_ruby_version: !ruby/object:Gem::Requirement
239
+ requirements:
240
+ - - ! '>='
241
+ - !ruby/object:Gem::Version
242
+ version: '0'
243
+ required_rubygems_version: !ruby/object:Gem::Requirement
244
+ requirements:
245
+ - - ! '>='
246
+ - !ruby/object:Gem::Version
247
+ version: '0'
201
248
  requirements: []
202
-
203
249
  rubyforge_project:
204
- rubygems_version: 2.0.14
250
+ rubygems_version: 2.1.11
205
251
  signing_key:
206
252
  specification_version: 4
207
- summary: "Geokit: encoding and distance calculation gem"
208
- test_files:
253
+ summary: ! 'Geokit: encoding and distance calculation gem'
254
+ test_files:
209
255
  - test/helper.rb
210
256
  - test/test_base_geocoder.rb
211
257
  - test/test_bing_geocoder.rb
@@ -216,7 +262,6 @@ test_files:
216
262
  - test/test_geo_plugin_geocoder.rb
217
263
  - test/test_geoloc.rb
218
264
  - test/test_geonames_geocoder.rb
219
- - test/test_geoplugin_geocoder.rb
220
265
  - test/test_google_geocoder.rb
221
266
  - test/test_inflector.rb
222
267
  - test/test_ipgeocoder.rb
@@ -1,58 +0,0 @@
1
- require File.join(File.dirname(__FILE__), 'helper')
2
-
3
- class IpGeocoderTest < BaseGeocoderTest #:nodoc: all
4
-
5
- IP_SUCCESS=<<-EOF
6
- <?xml version="1.0" encoding="UTF-8"?>
7
- <geoPlugin>
8
- <geoplugin_city>Belo Horizonte</geoplugin_city>
9
- <geoplugin_region>Minas Gerais</geoplugin_region>
10
- <geoplugin_areaCode>0</geoplugin_areaCode>
11
- <geoplugin_dmaCode>0</geoplugin_dmaCode>
12
- <geoplugin_countryCode>BR</geoplugin_countryCode>
13
- <geoplugin_countryName>Brazil</geoplugin_countryName>
14
- <geoplugin_continentCode>SA</geoplugin_continentCode>
15
- <geoplugin_latitude>-19.916700</geoplugin_latitude>
16
- <geoplugin_longitude>-43.933300</geoplugin_longitude>
17
- <geoplugin_currencyCode>BRL</geoplugin_currencyCode>
18
- <geoplugin_currencySymbol>&#82;&#36;</geoplugin_currencySymbol>
19
- <geoplugin_currencyConverter>2.2575001717</geoplugin_currencyConverter>
20
- </geoPlugin>
21
- EOF
22
-
23
- def setup
24
- super
25
- @success.provider = "geoPlugin"
26
- end
27
-
28
- def test_successful_lookup
29
- success = MockSuccess.new
30
- success.expects(:body).returns(IP_SUCCESS)
31
- url = 'http://www.geoplugin.net/xml.gp?ip=200.150.38.66'
32
- Geokit::Geocoders::GeoPluginGeocoder.expects(:call_geocoder_service).with(url).returns(success)
33
- location = Geokit::Geocoders::GeoPluginGeocoder.geocode('200.150.38.66')
34
- assert_not_nil location
35
- assert_equal(-19.916700, location.lat)
36
- assert_equal(-43.933300, location.lng)
37
- assert_equal "Belo Horizonte", location.city
38
- assert_equal "Minas Gerais", location.state
39
- assert_equal "BR", location.country_code
40
- assert_equal "geoPlugin", location.provider
41
- assert location.success?
42
- end
43
-
44
- def test_invalid_ip
45
- location = Geokit::Geocoders::GeoPluginGeocoder.geocode("pixrum")
46
- assert_not_nil location
47
- assert !location.success?
48
- end
49
-
50
- def test_service_unavailable
51
- failure = MockFailure.new
52
- url = 'http://www.geoplugin.net/xml.gp?ip=10.10.10.10'
53
- Geokit::Geocoders::GeoPluginGeocoder.expects(:call_geocoder_service).with(url).returns(failure)
54
- location = Geokit::Geocoders::GeoPluginGeocoder.geocode("10.10.10.10")
55
- assert_not_nil location
56
- assert !location.success?
57
- end
58
- end