geocoder 1.5.2 → 1.6.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of geocoder might be problematic. Click here for more details.

checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b6e1ac31854d2fcd30eb9cecb552d2b148b17355da7b15a71130cee3a432c335
4
- data.tar.gz: 8cebc67335e4d3fd263249cc441673353cf0487415ccc8a855e2b457c6631e42
3
+ metadata.gz: 0d1523b73c17cd042f5af380ea44d2711c66684928aee867490f539cc3507582
4
+ data.tar.gz: c63760a1b75b7ce43d785736ffb6b0afcace52f3283ee4365b4d7af269822603
5
5
  SHA512:
6
- metadata.gz: 826c1fb83145b010b610d34e42b0719673b4c51ce755b639425e3072bdb96dddcf373e605dce9f33c34aba46e95db71f64b50a84df71c67189a81fc8426f344b
7
- data.tar.gz: 698ef9a7643854cb7ac2316e22a51aed4a1a2ee763a8b65c9d7ab722566bbdcb3a95ce685389d20ecc164b3f9ccf1626deef21515d662d1339e88d28152985f7
6
+ metadata.gz: 96048af2e42535ec2091a21d29eac9c4ebb3f8a3081762b34c3c97a72215ce8f4312ee5f9f072153ef530370580e609996946b4afa23aa89b1973c942c18417f
7
+ data.tar.gz: d3993aa69ba09c543e24215c290e9e7a5b56db0c49b065ece947d3b53eeecb6a8014c8c7737138939ba7cdb67acba36a6d537c081ad86fcbf707eff9cf97e5b3
@@ -3,6 +3,12 @@ Changelog
3
3
 
4
4
  Major changes to Geocoder for each release. Please see the Git log for complete list of changes.
5
5
 
6
+ 1.6.0 (2020 Jan 6)
7
+ -------------------
8
+ * Drop support for Rails 3.x.
9
+ * Add support for :osmnames lookup (thanks github.com/zacviandier).
10
+ * Add support for :ipgeolocation IP lookup (thanks github.com/ahsannawaz111).
11
+
6
12
  1.5.2 (2019 Oct 3)
7
13
  -------------------
8
14
  * Add support for :ipregistry lookup (thanks github.com/ipregistry).
data/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2009-11 Alex Reisner
1
+ Copyright (c) 2009-2020 Alex Reisner
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
data/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  Geocoder
2
2
  ========
3
3
 
4
- **A complete geocoding solution for Ruby.**
4
+ **Complete geocoding solution for Ruby.**
5
5
 
6
6
  [![Gem Version](https://badge.fury.io/rb/geocoder.svg)](http://badge.fury.io/rb/geocoder)
7
7
  [![Code Climate](https://codeclimate.com/github/alexreisner/geocoder/badges/gpa.svg)](https://codeclimate.com/github/alexreisner/geocoder)
@@ -11,19 +11,19 @@ Geocoder
11
11
 
12
12
  Key features:
13
13
 
14
- * Forward and reverse geocoding, and IP address geocoding.
14
+ * Forward and reverse geocoding.
15
+ * IP address geocoding.
15
16
  * Connects to more than 40 APIs worldwide.
16
17
  * Performance-enhancing features like caching.
17
- * Advanced configuration allows different parameters and APIs to be used in different conditions.
18
18
  * Integrates with ActiveRecord and Mongoid.
19
19
  * Basic geospatial queries: search within radius (or rectangle, or ring).
20
20
 
21
21
  Compatibility:
22
22
 
23
- * Supports multiple Ruby versions: Ruby 2.x, and JRuby.
24
- * Supports multiple databases: MySQL, PostgreSQL, SQLite, and MongoDB (1.7.0 and higher).
25
- * Supports Rails 3, 4, and 5. If you need to use it with Rails 2 please see the `rails2` branch (no longer maintained, limited feature set).
26
- * Works very well outside of Rails, you just need to install either the `json` (for MRI) or `json_pure` (for JRuby) gem.
23
+ * Ruby versions: 2.x, and JRuby.
24
+ * Databases: MySQL, PostgreSQL, SQLite, and MongoDB.
25
+ * Rails: 4, 5, and 6.
26
+ * Works outside of Rails with the `json` (for MRI) or `json_pure` (for JRuby) gem.
27
27
 
28
28
 
29
29
  Table of Contents
@@ -703,4 +703,4 @@ For all contributions, please respect the following guidelines:
703
703
  * If your pull request is merged, please do not ask for an immediate release of the gem. There are many factors contributing to when releases occur (remember that they affect thousands of apps with Geocoder in their Gemfiles). If necessary, please install from the Github source until the next official release.
704
704
 
705
705
 
706
- Copyright (c) 2009-18 Alex Reisner, released under the MIT license.
706
+ Copyright :copyright: 2009-2020 Alex Reisner, released under the MIT license.
@@ -1,6 +1,8 @@
1
1
  # This class implements a cache with simple delegation to the Redis store, but
2
2
  # when it creates a key/value pair, it also sends an EXPIRE command with a TTL.
3
3
  # It should be fairly simple to do the same thing with Memcached.
4
+ # Alternatively, this class could inherit from Redis, which would make most
5
+ # of the below methods unnecessary.
4
6
  class AutoexpireCacheRedis
5
7
  def initialize(store, ttl = 86400)
6
8
  @store = store
@@ -32,7 +32,6 @@ module Geocoder
32
32
  :google_places_search,
33
33
  :bing,
34
34
  :geocoder_ca,
35
- :geocoder_us,
36
35
  :yandex,
37
36
  :nominatim,
38
37
  :mapbox,
@@ -51,7 +50,8 @@ module Geocoder
51
50
  :ban_data_gouv_fr,
52
51
  :test,
53
52
  :latlon,
54
- :amap
53
+ :amap,
54
+ :osmnames
55
55
  ]
56
56
  end
57
57
 
@@ -74,7 +74,8 @@ module Geocoder
74
74
  :ipdata_co,
75
75
  :db_ip_com,
76
76
  :ipstack,
77
- :ip2location
77
+ :ip2location,
78
+ :ipgeolocation
78
79
  ]
79
80
  end
80
81
 
@@ -86,6 +86,12 @@ module Geocoder::Lookup
86
86
  unless (citycode = query.options[:citycode]).nil? || !code_param_is_valid?(citycode)
87
87
  params[:citycode] = citycode.to_s
88
88
  end
89
+ unless (lat = query.options[:lat]).nil? || !latitude_is_valid?(lat)
90
+ params[:lat] = lat
91
+ end
92
+ unless (lon = query.options[:lon]).nil? || !longitude_is_valid?(lon)
93
+ params[:lon] = lon
94
+ end
89
95
  params
90
96
  end
91
97
 
@@ -126,5 +132,12 @@ module Geocoder::Lookup
126
132
  (1..99999).include?(param.to_i)
127
133
  end
128
134
 
135
+ def latitude_is_valid?(param)
136
+ param.to_f <= 90 && param.to_f >= -90
137
+ end
138
+
139
+ def longitude_is_valid?(param)
140
+ param.to_f <= 180 && param.to_f >= -180
141
+ end
129
142
  end
130
143
  end
@@ -9,13 +9,17 @@ module Geocoder::Lookup
9
9
  end
10
10
 
11
11
  def required_api_key_parts
12
- ["app_id", "app_code"]
12
+ ['api_key']
13
+ end
14
+
15
+ def supported_protocols
16
+ [:https]
13
17
  end
14
18
 
15
19
  private # ---------------------------------------------------------------
16
20
 
17
21
  def base_query_url(query)
18
- "#{protocol}://#{if query.reverse_geocode? then 'reverse.' end}geocoder.api.here.com/6.2/#{if query.reverse_geocode? then 'reverse' end}geocode.json?"
22
+ "#{protocol}://#{if query.reverse_geocode? then 'reverse.' end}geocoder.ls.hereapi.com/6.2/#{if query.reverse_geocode? then 'reverse' end}geocode.json?"
19
23
  end
20
24
 
21
25
  def results(query)
@@ -31,8 +35,7 @@ module Geocoder::Lookup
31
35
  def query_url_here_options(query, reverse_geocode)
32
36
  options = {
33
37
  gen: 9,
34
- app_id: api_key,
35
- app_code: api_code,
38
+ apikey: configuration.api_key,
36
39
  language: (query.language || configuration.language)
37
40
  }
38
41
  if reverse_geocode
@@ -61,17 +64,5 @@ module Geocoder::Lookup
61
64
  )
62
65
  end
63
66
  end
64
-
65
- def api_key
66
- if (a = configuration.api_key)
67
- return a.first if a.is_a?(Array)
68
- end
69
- end
70
-
71
- def api_code
72
- if (a = configuration.api_key)
73
- return a.last if a.is_a?(Array)
74
- end
75
- end
76
67
  end
77
68
  end
@@ -19,11 +19,11 @@ module Geocoder::Lookup
19
19
  end
20
20
 
21
21
  def query_url_params(query)
22
- {
23
- key: configuration.api_key ? configuration.api_key : "demo",
24
- format: "json",
25
- ip: query.sanitized_text
26
- }.merge(super)
22
+ params = super
23
+ if configuration.has_key?(:package)
24
+ params.merge!(package: configuration[:package])
25
+ end
26
+ params
27
27
  end
28
28
 
29
29
  def results(query)
@@ -63,13 +63,5 @@ module Geocoder::Lookup
63
63
  }
64
64
  end
65
65
 
66
- def query_url_params(query)
67
- params = super
68
- if configuration.has_key?(:package)
69
- params.merge!(package: configuration[:package])
70
- end
71
- params
72
- end
73
-
74
66
  end
75
67
  end
@@ -0,0 +1,63 @@
1
+ require 'geocoder/lookups/base'
2
+ require 'geocoder/results/ipgeolocation'
3
+
4
+
5
+ module Geocoder::Lookup
6
+ class Ipgeolocation < Base
7
+
8
+ ERROR_CODES = {
9
+ 404 => Geocoder::InvalidRequest,
10
+ 401 => Geocoder::RequestDenied, # missing/invalid API key
11
+ 101 => Geocoder::InvalidApiKey,
12
+ 102 => Geocoder::Error,
13
+ 103 => Geocoder::InvalidRequest,
14
+ 104 => Geocoder::OverQueryLimitError,
15
+ 105 => Geocoder::RequestDenied,
16
+ 301 => Geocoder::InvalidRequest,
17
+ 302 => Geocoder::InvalidRequest,
18
+ 303 => Geocoder::RequestDenied,
19
+ }
20
+ ERROR_CODES.default = Geocoder::Error
21
+
22
+ def name
23
+ "Ipgeolocation"
24
+ end
25
+
26
+ def supported_protocols
27
+ [:https]
28
+ end
29
+
30
+ private # ----------------------------------------------------------------
31
+
32
+ def base_query_url(query)
33
+ "#{protocol}://api.ipgeolocation.io/ipgeo?"
34
+ end
35
+ def query_url_params(query)
36
+ {
37
+ ip: query.sanitized_text,
38
+ apiKey: configuration.api_key
39
+ }.merge(super)
40
+ end
41
+
42
+ def results(query)
43
+ # don't look up a loopback or private address, just return the stored result
44
+ return [reserved_result(query.text)] if query.internal_ip_address?
45
+ return [] unless doc = fetch_data(query)
46
+ if error = doc['error']
47
+ code = error['code']
48
+ msg = error['info']
49
+ raise_error(ERROR_CODES[code], msg ) || Geocoder.log(:warn, "Ipgeolocation Geocoding API error: #{msg}")
50
+ return []
51
+ end
52
+ [doc]
53
+ end
54
+
55
+ def reserved_result(ip)
56
+ {
57
+ "ip" => ip,
58
+ "country_name" => "Reserved",
59
+ "country_code2" => "RD"
60
+ }
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,57 @@
1
+ require 'cgi'
2
+ require 'geocoder/lookups/base'
3
+ require 'geocoder/results/osmnames'
4
+
5
+ module Geocoder::Lookup
6
+ class Osmnames < Base
7
+
8
+ def name
9
+ 'OSM Names'
10
+ end
11
+
12
+ def required_api_key_parts
13
+ configuration[:host] ? [] : ['key']
14
+ end
15
+
16
+ def supported_protocols
17
+ [:https]
18
+ end
19
+
20
+ private
21
+
22
+ def base_query_url(query)
23
+ "#{base_url(query)}/#{params_url(query)}.js?"
24
+ end
25
+
26
+ def base_url(query)
27
+ host = configuration[:host] || 'geocoder.tilehosting.com'
28
+ "#{protocol}://#{host}"
29
+ end
30
+
31
+ def params_url(query)
32
+ method, args = 'q', CGI.escape(query.sanitized_text)
33
+ method, args = 'r', query.coordinates.join('/') if query.reverse_geocode?
34
+ "#{country_limited(query)}#{method}/#{args}"
35
+ end
36
+
37
+ def results(query)
38
+ return [] unless doc = fetch_data(query)
39
+ if (error = doc['message'])
40
+ raise_error(Geocoder::InvalidRequest, error) ||
41
+ Geocoder.log(:warn, "OSMNames Geocoding API error: #{error}")
42
+ else
43
+ return doc['results']
44
+ end
45
+ end
46
+
47
+ def query_url_params(query)
48
+ {
49
+ key: configuration.api_key
50
+ }.merge(super)
51
+ end
52
+
53
+ def country_limited(query)
54
+ "#{query.options[:country_code].downcase}/" if query.options[:country_code] && !query.reverse_geocode?
55
+ end
56
+ end
57
+ end
@@ -23,7 +23,7 @@ module Geocoder::Lookup
23
23
  end
24
24
 
25
25
  def query_url_params(query)
26
- params = {
26
+ {
27
27
  key: configuration.api_key
28
28
  }.merge(super)
29
29
  end
@@ -31,18 +31,18 @@ module Geocoder::Lookup
31
31
  case doc['status']
32
32
  when 0
33
33
  return [doc[content_key]]
34
- when 199
35
- raise error(Geocoder::InvalidApiKey, "invalid api key") ||
36
- Geocoder.log(:warn, "#{name} Geocoding API error: key is not enabled for web service usage.")
37
- when 311
38
- raise_error(Geocoder::RequestDenied, "request denied") ||
39
- Geocoder.log(:warn, "#{name} Geocoding API error: request denied.")
40
- when 310, 306
41
- raise_error(Geocoder::InvalidRequest, "invalid request.") ||
42
- Geocoder.log(:warn, "#{name} Geocoding API error: invalid request.")
43
34
  when 311
44
35
  raise_error(Geocoder::InvalidApiKey, "invalid api key") ||
45
36
  Geocoder.log(:warn, "#{name} Geocoding API error: invalid api key.")
37
+ when 310
38
+ raise_error(Geocoder::InvalidRequest, "invalid request.") ||
39
+ Geocoder.log(:warn, "#{name} Geocoding API error: invalid request, invalid parameters.")
40
+ when 306
41
+ raise_error(Geocoder::InvalidRequest, "invalid request.") ||
42
+ Geocoder.log(:warn, "#{name} Geocoding API error: invalid request, check response for more info.")
43
+ when 110
44
+ raise_error(Geocoder::RequestDenied, "request denied.") ||
45
+ Geocoder.log(:warn, "#{name} Geocoding API error: request source is not authorized.")
46
46
  end
47
47
  return []
48
48
  end
@@ -7,10 +7,6 @@ module Geocoder::Result
7
7
  ['lat', 'lng'].map{ |i| @data['location'][i] }
8
8
  end
9
9
 
10
- def address
11
- @data['formatted_address']
12
- end
13
-
14
10
  def province
15
11
  @data['addressComponent'] and @data['addressComponent']['province'] or ""
16
12
  end
@@ -7,7 +7,7 @@ module Geocoder::Result
7
7
  #### BASE METHODS ####
8
8
 
9
9
  def self.response_attributes
10
- %w[limit attribution version licence type features]
10
+ %w[limit attribution version licence type features center]
11
11
  end
12
12
 
13
13
  response_attributes.each do |a|
@@ -0,0 +1,59 @@
1
+ require 'geocoder/results/base'
2
+
3
+ module Geocoder::Result
4
+ class Ipgeolocation < Base
5
+
6
+ def coordinates
7
+ [@data['latitude'].to_f, @data['longitude'].to_f]
8
+ end
9
+
10
+ def address(format = :full)
11
+ "#{city}, #{state} #{postal_code}, #{country_name}".sub(/^[ ,]*/, "")
12
+ end
13
+
14
+ def state
15
+ @data['state_prov']
16
+ end
17
+
18
+ def state_code
19
+ @data['state_prov']
20
+ end
21
+
22
+ def country
23
+ @data['country_name']
24
+ end
25
+
26
+ def country_code
27
+ @data['country_code2']
28
+ end
29
+
30
+ def postal_code
31
+ @data['zipcode']
32
+ end
33
+
34
+ def self.response_attributes
35
+ [
36
+ ['ip', ''],
37
+ ['hostname', ''],
38
+ ['continent_code', ''],
39
+ ['continent_name', ''],
40
+ ['country_code2', ''],
41
+ ['country_code3', ''],
42
+ ['country_name', ''],
43
+ ['country_capital',''],
44
+ ['district',''],
45
+ ['state_prov',''],
46
+ ['city', ''],
47
+ ['zipcode', ''],
48
+ ['time_zone', {}],
49
+ ['currency', {}]
50
+ ]
51
+ end
52
+
53
+ response_attributes.each do |attr, default|
54
+ define_method attr do
55
+ @data[attr] || default
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,56 @@
1
+ require 'geocoder/results/base'
2
+
3
+ module Geocoder::Result
4
+ class Osmnames < Base
5
+ def address
6
+ @data['display_name']
7
+ end
8
+
9
+ def coordinates
10
+ [@data['lat'].to_f, @data['lon'].to_f]
11
+ end
12
+
13
+ def viewport
14
+ west, south, east, north = @data['boundingbox'].map(&:to_f)
15
+ [south, west, north, east]
16
+ end
17
+
18
+ def state
19
+ @data['state']
20
+ end
21
+ alias_method :state_code, :state
22
+
23
+ def place_class
24
+ @data['class']
25
+ end
26
+
27
+ def place_type
28
+ @data['type']
29
+ end
30
+
31
+ def postal_code
32
+ ''
33
+ end
34
+
35
+ def country_code
36
+ @data['country_code']
37
+ end
38
+
39
+ def country
40
+ @data['country']
41
+ end
42
+
43
+ def self.response_attributes
44
+ %w[house_number street city name osm_id osm_type boundingbox place_rank
45
+ importance county rank name_suffix]
46
+ end
47
+
48
+ response_attributes.each do |a|
49
+ unless method_defined?(a)
50
+ define_method a do
51
+ @data[a]
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
@@ -1,3 +1,3 @@
1
1
  module Geocoder
2
- VERSION = "1.5.2"
2
+ VERSION = "1.6.0"
3
3
  end
@@ -60,9 +60,8 @@ module HashRecursiveMerge
60
60
  # h1.merge(h2) #=> {"a" => 100, "b" = >254, "c" => {"c1" => 16, "c3" => 94}}
61
61
  #
62
62
  def rmerge(other_hash)
63
- r = {}
64
63
  merge(other_hash) do |key, oldval, newval|
65
- r[key] = oldval.class == self.class ? oldval.rmerge(newval) : newval
64
+ oldval.class == self.class ? oldval.rmerge(newval) : newval
66
65
  end
67
66
  end
68
67
 
@@ -96,9 +96,9 @@ module Geocoder
96
96
 
97
97
  def archive_url_path(package)
98
98
  {
99
- geolite_country_csv: "GeoIPCountryCSV.zip",
100
- geolite_city_csv: "GeoLiteCity_CSV/GeoLiteCity-latest.zip",
101
- geolite_asn_csv: "asnum/GeoIPASNum2.zip"
99
+ geolite_country_csv: "GeoLite2-Country-CSV.zip",
100
+ geolite_city_csv: "GeoLite2-City-CSV.zip",
101
+ geolite_asn_csv: "GeoLite2-ASN-CSV.zip"
102
102
  }[package]
103
103
  end
104
104
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: geocoder
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.2
4
+ version: 1.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex Reisner
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-10-03 00:00:00.000000000 Z
11
+ date: 2020-01-06 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Provides object geocoding (by street or IP address), reverse geocoding
14
14
  (coordinates to street address), distance queries for ActiveRecord and Mongoid,
@@ -59,7 +59,6 @@ files:
59
59
  - lib/geocoder/lookups/esri.rb
60
60
  - lib/geocoder/lookups/freegeoip.rb
61
61
  - lib/geocoder/lookups/geocoder_ca.rb
62
- - lib/geocoder/lookups/geocoder_us.rb
63
62
  - lib/geocoder/lookups/geocodio.rb
64
63
  - lib/geocoder/lookups/geoip2.rb
65
64
  - lib/geocoder/lookups/geoportail_lu.rb
@@ -71,6 +70,7 @@ files:
71
70
  - lib/geocoder/lookups/ip2location.rb
72
71
  - lib/geocoder/lookups/ipapi_com.rb
73
72
  - lib/geocoder/lookups/ipdata_co.rb
73
+ - lib/geocoder/lookups/ipgeolocation.rb
74
74
  - lib/geocoder/lookups/ipinfo_io.rb
75
75
  - lib/geocoder/lookups/ipregistry.rb
76
76
  - lib/geocoder/lookups/ipstack.rb
@@ -83,6 +83,7 @@ files:
83
83
  - lib/geocoder/lookups/maxmind_local.rb
84
84
  - lib/geocoder/lookups/nominatim.rb
85
85
  - lib/geocoder/lookups/opencagedata.rb
86
+ - lib/geocoder/lookups/osmnames.rb
86
87
  - lib/geocoder/lookups/pelias.rb
87
88
  - lib/geocoder/lookups/pickpoint.rb
88
89
  - lib/geocoder/lookups/pointpin.rb
@@ -112,7 +113,6 @@ files:
112
113
  - lib/geocoder/results/esri.rb
113
114
  - lib/geocoder/results/freegeoip.rb
114
115
  - lib/geocoder/results/geocoder_ca.rb
115
- - lib/geocoder/results/geocoder_us.rb
116
116
  - lib/geocoder/results/geocodio.rb
117
117
  - lib/geocoder/results/geoip2.rb
118
118
  - lib/geocoder/results/geoportail_lu.rb
@@ -124,6 +124,7 @@ files:
124
124
  - lib/geocoder/results/ip2location.rb
125
125
  - lib/geocoder/results/ipapi_com.rb
126
126
  - lib/geocoder/results/ipdata_co.rb
127
+ - lib/geocoder/results/ipgeolocation.rb
127
128
  - lib/geocoder/results/ipinfo_io.rb
128
129
  - lib/geocoder/results/ipregistry.rb
129
130
  - lib/geocoder/results/ipstack.rb
@@ -136,6 +137,7 @@ files:
136
137
  - lib/geocoder/results/maxmind_local.rb
137
138
  - lib/geocoder/results/nominatim.rb
138
139
  - lib/geocoder/results/opencagedata.rb
140
+ - lib/geocoder/results/osmnames.rb
139
141
  - lib/geocoder/results/pelias.rb
140
142
  - lib/geocoder/results/pickpoint.rb
141
143
  - lib/geocoder/results/pointpin.rb
@@ -178,8 +180,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
178
180
  - !ruby/object:Gem::Version
179
181
  version: '0'
180
182
  requirements: []
181
- rubyforge_project:
182
- rubygems_version: 2.7.6
183
+ rubygems_version: 3.0.1
183
184
  signing_key:
184
185
  specification_version: 4
185
186
  summary: Complete geocoding solution for Ruby.
@@ -1,51 +0,0 @@
1
- require 'geocoder/lookups/base'
2
- require "geocoder/results/geocoder_us"
3
-
4
- module Geocoder::Lookup
5
- class GeocoderUs < Base
6
-
7
- def name
8
- "Geocoder.us"
9
- end
10
-
11
- def supported_protocols
12
- [:http]
13
- end
14
-
15
- private # ----------------------------------------------------------------
16
-
17
- def base_query_url(query)
18
- base_query_url_with_optional_key(configuration.api_key)
19
- end
20
-
21
- def cache_key(query)
22
- base_query_url_with_optional_key(nil) + url_query_string(query)
23
- end
24
-
25
- def base_query_url_with_optional_key(key = nil)
26
- base = "#{protocol}://"
27
- if configuration.api_key
28
- base << "#{configuration.api_key}@"
29
- end
30
- base + "geocoder.us/member/service/csv/geocode?"
31
- end
32
-
33
- def results(query)
34
- return [] unless doc = fetch_data(query)
35
- if doc[0].to_s =~ /^(\d+)\:/
36
- return []
37
- else
38
- return [doc.size == 5 ? ((doc[0..1] << nil) + doc[2..4]) : doc]
39
- end
40
- end
41
-
42
- def query_url_params(query)
43
- (query.text =~ /^\d{5}(?:-\d{4})?$/ ? {:zip => query} : {:address => query.sanitized_text}).merge(super)
44
- end
45
-
46
- def parse_raw_data(raw_data)
47
- raw_data.chomp.split(',')
48
- end
49
- end
50
- end
51
-
@@ -1,39 +0,0 @@
1
- require 'geocoder/results/base'
2
-
3
- module Geocoder::Result
4
- class GeocoderUs < Base
5
- def coordinates
6
- [@data[0].to_f, @data[1].to_f]
7
- end
8
-
9
- def address(format = :full)
10
- "#{street_address}, #{city}, #{state} #{postal_code}, #{country}".sub(/^[ ,]*/, "")
11
- end
12
-
13
- def street_address
14
- @data[2]
15
- end
16
-
17
- def city
18
- @data[3]
19
- end
20
-
21
- def state
22
- @data[4]
23
- end
24
-
25
- alias_method :state_code, :state
26
-
27
- def postal_code
28
- @data[5]
29
- end
30
-
31
- def country
32
- 'United States'
33
- end
34
-
35
- def country_code
36
- 'US'
37
- end
38
- end
39
- end