geocoder 1.6.4 → 1.7.0

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.
@@ -0,0 +1,57 @@
1
+ require 'geocoder/results/base'
2
+
3
+ module Geocoder::Result
4
+ class AmazonLocationService < Base
5
+ def initialize(result)
6
+ @place = result
7
+ end
8
+
9
+ def coordinates
10
+ [@place.geometry.point[1], @place.geometry.point[0]]
11
+ end
12
+
13
+ def address
14
+ @place.label
15
+ end
16
+
17
+ def neighborhood
18
+ @place.neighborhood
19
+ end
20
+
21
+ def route
22
+ @place.street
23
+ end
24
+
25
+ def city
26
+ @place.municipality || @place.sub_region
27
+ end
28
+
29
+ def state
30
+ @place.region
31
+ end
32
+
33
+ def state_code
34
+ @place.region
35
+ end
36
+
37
+ def province
38
+ @place.region
39
+ end
40
+
41
+ def province_code
42
+ @place.region
43
+ end
44
+
45
+ def postal_code
46
+ @place.postal_code
47
+ end
48
+
49
+ def country
50
+ @place.country
51
+ end
52
+
53
+ def country_code
54
+ @place.country
55
+ end
56
+ end
57
+ end
@@ -4,6 +4,27 @@ require 'geocoder/results/base'
4
4
  module Geocoder::Result
5
5
  class BanDataGouvFr < Base
6
6
 
7
+ STATE_CODE_MAPPINGS = {
8
+ "Guadeloupe" => "01",
9
+ "Martinique" => "02",
10
+ "Guyane" => "03",
11
+ "La Réunion" => "04",
12
+ "Mayotte" => "06",
13
+ "Île-de-France" => "11",
14
+ "Centre-Val de Loire" => "24",
15
+ "Bourgogne-Franche-Comté" => "27",
16
+ "Normandie" => "28",
17
+ "Hauts-de-France" => "32",
18
+ "Grand Est" => "44",
19
+ "Pays de la Loire" => "52",
20
+ "Bretagne" => "53",
21
+ "Nouvelle-Aquitaine" => "75",
22
+ "Occitanie" => "76",
23
+ "Auvergne-Rhône-Alpes" => "84",
24
+ "Provence-Alpes-Côte d'Azur" => "93",
25
+ "Corse" => "94"
26
+ }.freeze
27
+
7
28
  #### BASE METHODS ####
8
29
 
9
30
  def self.response_attributes
@@ -209,6 +230,10 @@ module Geocoder::Result
209
230
  end
210
231
  end
211
232
 
233
+ def region_code
234
+ STATE_CODE_MAPPINGS[region_name]
235
+ end
236
+
212
237
  def country
213
238
  "France"
214
239
  end
@@ -235,7 +260,7 @@ module Geocoder::Result
235
260
  alias_method :street, :street_name
236
261
  alias_method :city, :city_name
237
262
  alias_method :state, :region_name
238
- alias_method :state_code, :state
263
+ alias_method :state_code, :region_code
239
264
 
240
265
  #### CITIES' METHODS ####
241
266
 
@@ -16,11 +16,14 @@ module Geocoder::Result
16
16
  end
17
17
  end
18
18
 
19
- def state_code
19
+ def state
20
20
  attributes['Region']
21
21
  end
22
22
 
23
- alias_method :state, :state_code
23
+ def state_code
24
+ abbr = attributes['RegionAbbr']
25
+ abbr.to_s == "" ? state : abbr
26
+ end
24
27
 
25
28
  def country
26
29
  country_key = reverse_geocode? ? "CountryCode" : "Country"
@@ -0,0 +1,179 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'geocoder/results/base'
4
+
5
+ module Geocoder
6
+ module Result
7
+ # https://apidocs.geoapify.com/docs/geocoding/api
8
+ class Geoapify < Base
9
+ def address(_format = :full)
10
+ properties['formatted']
11
+ end
12
+
13
+ def address_line1
14
+ properties['address_line1']
15
+ end
16
+
17
+ def address_line2
18
+ properties['address_line2']
19
+ end
20
+
21
+ def house_number
22
+ properties['housenumber']
23
+ end
24
+
25
+ def street
26
+ properties['street']
27
+ end
28
+
29
+ def postal_code
30
+ properties['postcode']
31
+ end
32
+
33
+ def district
34
+ properties['district']
35
+ end
36
+
37
+ def suburb
38
+ properties['suburb']
39
+ end
40
+
41
+ def city
42
+ properties['city']
43
+ end
44
+
45
+ def county
46
+ properties['county']
47
+ end
48
+
49
+ def state
50
+ properties['state']
51
+ end
52
+
53
+ # Not currently available in the API
54
+ def state_code
55
+ ''
56
+ end
57
+
58
+ def country
59
+ properties['country']
60
+ end
61
+
62
+ def country_code
63
+ return unless properties['country_code']
64
+
65
+ properties['country_code'].upcase
66
+ end
67
+
68
+ def coordinates
69
+ return unless properties['lat']
70
+ return unless properties['lon']
71
+
72
+ [properties['lat'], properties['lon']]
73
+ end
74
+
75
+ # See: https://tools.ietf.org/html/rfc7946#section-3.1
76
+ #
77
+ # Each feature has a "Point" type in the Geoapify API.
78
+ def geometry
79
+ return unless data['geometry']
80
+
81
+ symbol_hash data['geometry']
82
+ end
83
+
84
+ # See: https://tools.ietf.org/html/rfc7946#section-5
85
+ def bounds
86
+ data['bbox']
87
+ end
88
+
89
+ # Type of the result, one of:
90
+ #
91
+ # * :unknown
92
+ # * :amenity
93
+ # * :building
94
+ # * :street
95
+ # * :suburb
96
+ # * :district
97
+ # * :postcode
98
+ # * :city
99
+ # * :county
100
+ # * :state
101
+ # * :country
102
+ #
103
+ def type
104
+ return :unknown unless properties['result_type']
105
+
106
+ properties['result_type'].to_sym
107
+ end
108
+
109
+ # Distance in meters to given bias:proximity or to given coordinates for
110
+ # reverse geocoding
111
+ def distance
112
+ properties['distance']
113
+ end
114
+
115
+ # Calculated rank for the result, containing the following keys:
116
+ #
117
+ # * `popularity` - The popularity score of the result
118
+ # * `confidence` - The confidence value of the result (0-1)
119
+ # * `match_type` - The result's match type, one of following:
120
+ # * full_match
121
+ # * inner_part
122
+ # * match_by_building
123
+ # * match_by_street
124
+ # * match_by_postcode
125
+ # * match_by_city_or_disrict
126
+ # * match_by_country_or_state
127
+ #
128
+ # Example:
129
+ # {
130
+ # popularity: 8.615793062435909,
131
+ # confidence: 0.88,
132
+ # match_type: :full_match
133
+ # }
134
+ def rank
135
+ return unless properties['rank']
136
+
137
+ r = symbol_hash(properties['rank'])
138
+ r[:match_type] = r[:match_type].to_sym if r[:match_type]
139
+ r
140
+ end
141
+
142
+ # Examples:
143
+ #
144
+ # Open
145
+ # {
146
+ # sourcename: 'openstreetmap',
147
+ # wheelchair: 'limited',
148
+ # wikidata: 'Q186125',
149
+ # wikipedia: 'en:Madison Square Garden',
150
+ # website: 'http://www.thegarden.com/',
151
+ # phone: '12124656741',
152
+ # osm_type: 'W',
153
+ # osm_id: 138141251,
154
+ # continent: 'North America',
155
+ # }
156
+ def datasource
157
+ return unless properties['datasource']
158
+
159
+ symbol_hash properties['datasource']
160
+ end
161
+
162
+ private
163
+
164
+ def properties
165
+ @properties ||= data['properties'] || {}
166
+ end
167
+
168
+ def symbol_hash(orig_hash)
169
+ {}.tap do |result|
170
+ orig_hash.each_key do |key|
171
+ next unless orig_hash[key]
172
+
173
+ result[key.to_sym] = orig_hash[key]
174
+ end
175
+ end
176
+ end
177
+ end
178
+ end
179
+ end
@@ -0,0 +1,54 @@
1
+ require 'geocoder/results/base'
2
+
3
+ module Geocoder
4
+ module Result
5
+ class Ipqualityscore < Base
6
+
7
+ def self.key_method_mappings
8
+ {
9
+ 'request_id' => :request_id,
10
+ 'success' => :success?,
11
+ 'message' => :message,
12
+ 'city' => :city,
13
+ 'region' => :state,
14
+ 'country_code' => :country_code,
15
+ 'mobile' => :mobile?,
16
+ 'fraud_score' => :fraud_score,
17
+ 'ISP' => :isp,
18
+ 'ASN' => :asn,
19
+ 'organization' => :organization,
20
+ 'is_crawler' => :crawler?,
21
+ 'host' => :host,
22
+ 'proxy' => :proxy?,
23
+ 'vpn' => :vpn?,
24
+ 'tor' => :tor?,
25
+ 'active_vpn' => :active_vpn?,
26
+ 'active_tor' => :active_tor?,
27
+ 'recent_abuse' => :recent_abuse?,
28
+ 'bot_status' => :bot?,
29
+ 'connection_type' => :connection_type,
30
+ 'abuse_velocity' => :abuse_velocity,
31
+ 'timezone' => :timezone,
32
+ }
33
+ end
34
+
35
+ key_method_mappings.each_pair do |key, meth|
36
+ define_method meth do
37
+ @data[key]
38
+ end
39
+ end
40
+
41
+ alias_method :state_code, :state
42
+ alias_method :country, :country_code
43
+
44
+ def postal_code
45
+ '' # No suitable fallback
46
+ end
47
+
48
+ def address
49
+ [city, state, country_code].compact.reject(&:empty?).join(', ')
50
+ end
51
+
52
+ end
53
+ end
54
+ end
@@ -9,6 +9,10 @@ module Geocoder::Result
9
9
  @data = flatten_hash(data)
10
10
  end
11
11
 
12
+ def coordinates
13
+ [@data['location_latitude'], @data['location_longitude']]
14
+ end
15
+
12
16
  def flatten_hash(hash)
13
17
  hash.each_with_object({}) do |(k, v), h|
14
18
  if v.is_a? Hash
@@ -35,14 +39,6 @@ module Geocoder::Result
35
39
  @data['location_country_code']
36
40
  end
37
41
 
38
- def latitude
39
- @data['location_latitude']
40
- end
41
-
42
- def longitude
43
- @data['location_longitude']
44
- end
45
-
46
42
  def postal_code
47
43
  @data['location_postal']
48
44
  end
@@ -23,7 +23,10 @@ module Geocoder::Result
23
23
  context_part('region')
24
24
  end
25
25
 
26
- alias_method :state_code, :state
26
+ def state_code
27
+ value = context_part('region', 'short_code')
28
+ value.split('-').last unless value.nil?
29
+ end
27
30
 
28
31
  def postal_code
29
32
  context_part('postcode')
@@ -33,7 +36,10 @@ module Geocoder::Result
33
36
  context_part('country')
34
37
  end
35
38
 
36
- alias_method :country_code, :country
39
+ def country_code
40
+ value = context_part('country', 'short_code')
41
+ value.upcase unless value.nil?
42
+ end
37
43
 
38
44
  def neighborhood
39
45
  context_part('neighborhood')
@@ -45,8 +51,8 @@ module Geocoder::Result
45
51
 
46
52
  private
47
53
 
48
- def context_part(name)
49
- context.map { |c| c['text'] if c['id'] =~ Regexp.new(name) }.compact.first
54
+ def context_part(name, key = 'text')
55
+ (context.detect { |c| c['id'] =~ Regexp.new(name) } || {})[key]
50
56
  end
51
57
 
52
58
  def context
@@ -0,0 +1,46 @@
1
+ require 'geocoder/results/base'
2
+
3
+ module Geocoder::Result
4
+ class MelissaStreet < Base
5
+ def address(format = :full)
6
+ @data['FormattedAddress']
7
+ end
8
+
9
+ def street_address
10
+ @data['AddressLine1']
11
+ end
12
+
13
+ def suffix
14
+ @data['ThoroughfareTrailingType']
15
+ end
16
+
17
+ def number
18
+ @data['PremisesNumber']
19
+ end
20
+
21
+ def city
22
+ @data['Locality']
23
+ end
24
+
25
+ def state_code
26
+ @data['AdministrativeArea']
27
+ end
28
+ alias_method :state, :state_code
29
+
30
+ def country
31
+ @data['CountryName']
32
+ end
33
+
34
+ def country_code
35
+ @data['CountryISO3166_1_Alpha2']
36
+ end
37
+
38
+ def postal_code
39
+ @data['PostalCode']
40
+ end
41
+
42
+ def coordinates
43
+ [@data['Latitude'].to_f, @data['Longitude'].to_f]
44
+ end
45
+ end
46
+ end
@@ -8,7 +8,7 @@ module Geocoder::Result
8
8
  end
9
9
 
10
10
  def coordinates
11
- @data['centroide_ll'][6..-2].split(' ').map(&:to_f)
11
+ @data['centroide_ll'][6..-2].split(' ').map(&:to_f).reverse
12
12
  end
13
13
 
14
14
  def formatted_address
@@ -0,0 +1,119 @@
1
+ require 'geocoder/results/base'
2
+
3
+ module Geocoder::Result
4
+ class Photon < Base
5
+ def name
6
+ properties['name']
7
+ end
8
+
9
+ def address(_format = :full)
10
+ parts = []
11
+ parts << name if name
12
+ parts << street_address if street_address
13
+ parts << city
14
+ parts << state if state
15
+ parts << postal_code
16
+ parts << country
17
+
18
+ parts.join(', ')
19
+ end
20
+
21
+ def street_address
22
+ return unless street
23
+ return street unless house_number
24
+
25
+ "#{house_number} #{street}"
26
+ end
27
+
28
+ def house_number
29
+ properties['housenumber']
30
+ end
31
+
32
+ def street
33
+ properties['street']
34
+ end
35
+
36
+ def postal_code
37
+ properties['postcode']
38
+ end
39
+
40
+ def city
41
+ properties['city']
42
+ end
43
+
44
+ def state
45
+ properties['state']
46
+ end
47
+
48
+ def state_code
49
+ ''
50
+ end
51
+
52
+ def country
53
+ properties['country']
54
+ end
55
+
56
+ def country_code
57
+ ''
58
+ end
59
+
60
+ def coordinates
61
+ return unless geometry
62
+ return unless geometry[:coordinates]
63
+
64
+ geometry[:coordinates].reverse
65
+ end
66
+
67
+ def geometry
68
+ return unless data['geometry']
69
+
70
+ symbol_hash data['geometry']
71
+ end
72
+
73
+ def bounds
74
+ properties['extent']
75
+ end
76
+
77
+ # Type of the result (OSM object type), one of:
78
+ #
79
+ # :node
80
+ # :way
81
+ # :relation
82
+ #
83
+ def type
84
+ {
85
+ 'N' => :node,
86
+ 'W' => :way,
87
+ 'R' => :relation
88
+ }[properties['osm_type']]
89
+ end
90
+
91
+ def osm_id
92
+ properties['osm_id']
93
+ end
94
+
95
+ # See: https://wiki.openstreetmap.org/wiki/Tags
96
+ def osm_tag
97
+ return unless properties['osm_key']
98
+ return properties['osm_key'] unless properties['osm_value']
99
+
100
+ "#{properties['osm_key']}=#{properties['osm_value']}"
101
+ end
102
+
103
+ private
104
+
105
+ def properties
106
+ @properties ||= data['properties'] || {}
107
+ end
108
+
109
+ def symbol_hash(orig_hash)
110
+ {}.tap do |result|
111
+ orig_hash.each_key do |key|
112
+ next unless orig_hash[key]
113
+
114
+ result[key.to_sym] = orig_hash[key]
115
+ end
116
+ end
117
+ end
118
+ end
119
+ end
@@ -1,3 +1,3 @@
1
1
  module Geocoder
2
- VERSION = "1.6.4"
2
+ VERSION = "1.7.0"
3
3
  end
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.6.4
4
+ version: 1.7.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: 2020-10-06 00:00:00.000000000 Z
11
+ date: 2021-10-11 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Object geocoding (by street or IP address), reverse geocoding (coordinates
14
14
  to street address), distance queries for ActiveRecord and Mongoid, result caching,
@@ -49,7 +49,9 @@ files:
49
49
  - lib/geocoder/kernel_logger.rb
50
50
  - lib/geocoder/logger.rb
51
51
  - lib/geocoder/lookup.rb
52
+ - lib/geocoder/lookups/abstract_api.rb
52
53
  - lib/geocoder/lookups/amap.rb
54
+ - lib/geocoder/lookups/amazon_location_service.rb
53
55
  - lib/geocoder/lookups/baidu.rb
54
56
  - lib/geocoder/lookups/baidu_ip.rb
55
57
  - lib/geocoder/lookups/ban_data_gouv_fr.rb
@@ -59,6 +61,7 @@ files:
59
61
  - lib/geocoder/lookups/dstk.rb
60
62
  - lib/geocoder/lookups/esri.rb
61
63
  - lib/geocoder/lookups/freegeoip.rb
64
+ - lib/geocoder/lookups/geoapify.rb
62
65
  - lib/geocoder/lookups/geocoder_ca.rb
63
66
  - lib/geocoder/lookups/geocodio.rb
64
67
  - lib/geocoder/lookups/geoip2.rb
@@ -73,6 +76,7 @@ files:
73
76
  - lib/geocoder/lookups/ipdata_co.rb
74
77
  - lib/geocoder/lookups/ipgeolocation.rb
75
78
  - lib/geocoder/lookups/ipinfo_io.rb
79
+ - lib/geocoder/lookups/ipqualityscore.rb
76
80
  - lib/geocoder/lookups/ipregistry.rb
77
81
  - lib/geocoder/lookups/ipstack.rb
78
82
  - lib/geocoder/lookups/latlon.rb
@@ -82,11 +86,13 @@ files:
82
86
  - lib/geocoder/lookups/maxmind.rb
83
87
  - lib/geocoder/lookups/maxmind_geoip2.rb
84
88
  - lib/geocoder/lookups/maxmind_local.rb
89
+ - lib/geocoder/lookups/melissa_street.rb
85
90
  - lib/geocoder/lookups/nationaal_georegister_nl.rb
86
91
  - lib/geocoder/lookups/nominatim.rb
87
92
  - lib/geocoder/lookups/opencagedata.rb
88
93
  - lib/geocoder/lookups/osmnames.rb
89
94
  - lib/geocoder/lookups/pelias.rb
95
+ - lib/geocoder/lookups/photon.rb
90
96
  - lib/geocoder/lookups/pickpoint.rb
91
97
  - lib/geocoder/lookups/pointpin.rb
92
98
  - lib/geocoder/lookups/postcode_anywhere_uk.rb
@@ -105,7 +111,9 @@ files:
105
111
  - lib/geocoder/query.rb
106
112
  - lib/geocoder/railtie.rb
107
113
  - lib/geocoder/request.rb
114
+ - lib/geocoder/results/abstract_api.rb
108
115
  - lib/geocoder/results/amap.rb
116
+ - lib/geocoder/results/amazon_location_service.rb
109
117
  - lib/geocoder/results/baidu.rb
110
118
  - lib/geocoder/results/baidu_ip.rb
111
119
  - lib/geocoder/results/ban_data_gouv_fr.rb
@@ -115,6 +123,7 @@ files:
115
123
  - lib/geocoder/results/dstk.rb
116
124
  - lib/geocoder/results/esri.rb
117
125
  - lib/geocoder/results/freegeoip.rb
126
+ - lib/geocoder/results/geoapify.rb
118
127
  - lib/geocoder/results/geocoder_ca.rb
119
128
  - lib/geocoder/results/geocodio.rb
120
129
  - lib/geocoder/results/geoip2.rb
@@ -129,6 +138,7 @@ files:
129
138
  - lib/geocoder/results/ipdata_co.rb
130
139
  - lib/geocoder/results/ipgeolocation.rb
131
140
  - lib/geocoder/results/ipinfo_io.rb
141
+ - lib/geocoder/results/ipqualityscore.rb
132
142
  - lib/geocoder/results/ipregistry.rb
133
143
  - lib/geocoder/results/ipstack.rb
134
144
  - lib/geocoder/results/latlon.rb
@@ -138,11 +148,13 @@ files:
138
148
  - lib/geocoder/results/maxmind.rb
139
149
  - lib/geocoder/results/maxmind_geoip2.rb
140
150
  - lib/geocoder/results/maxmind_local.rb
151
+ - lib/geocoder/results/melissa_street.rb
141
152
  - lib/geocoder/results/nationaal_georegister_nl.rb
142
153
  - lib/geocoder/results/nominatim.rb
143
154
  - lib/geocoder/results/opencagedata.rb
144
155
  - lib/geocoder/results/osmnames.rb
145
156
  - lib/geocoder/results/pelias.rb
157
+ - lib/geocoder/results/photon.rb
146
158
  - lib/geocoder/results/pickpoint.rb
147
159
  - lib/geocoder/results/pointpin.rb
148
160
  - lib/geocoder/results/postcode_anywhere_uk.rb