geocoder 1.4.9 → 1.6.7

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.
Files changed (107) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +60 -0
  3. data/LICENSE +1 -1
  4. data/README.md +350 -979
  5. data/bin/console +13 -0
  6. data/examples/autoexpire_cache_redis.rb +2 -0
  7. data/lib/easting_northing.rb +171 -0
  8. data/lib/generators/geocoder/config/templates/initializer.rb +1 -1
  9. data/lib/geocoder/cache.rb +4 -0
  10. data/lib/geocoder/calculations.rb +1 -1
  11. data/lib/geocoder/cli.rb +2 -2
  12. data/lib/geocoder/configuration.rb +3 -2
  13. data/lib/geocoder/configuration_hash.rb +4 -4
  14. data/lib/geocoder/exceptions.rb +1 -1
  15. data/lib/geocoder/ip_address.rb +16 -2
  16. data/lib/geocoder/lookup.rb +9 -5
  17. data/lib/geocoder/lookups/abstract_api.rb +46 -0
  18. data/lib/geocoder/lookups/amap.rb +7 -3
  19. data/lib/geocoder/lookups/baidu.rb +14 -10
  20. data/lib/geocoder/lookups/baidu_ip.rb +7 -36
  21. data/lib/geocoder/lookups/ban_data_gouv_fr.rb +18 -5
  22. data/lib/geocoder/lookups/base.rb +27 -4
  23. data/lib/geocoder/lookups/bing.rb +10 -13
  24. data/lib/geocoder/lookups/db_ip_com.rb +9 -6
  25. data/lib/geocoder/lookups/dstk.rb +4 -2
  26. data/lib/geocoder/lookups/esri.rb +44 -28
  27. data/lib/geocoder/lookups/freegeoip.rb +11 -7
  28. data/lib/geocoder/lookups/geocoder_ca.rb +4 -4
  29. data/lib/geocoder/lookups/geocodio.rb +5 -5
  30. data/lib/geocoder/lookups/geoportail_lu.rb +7 -7
  31. data/lib/geocoder/lookups/google.rb +15 -10
  32. data/lib/geocoder/lookups/google_places_details.rb +11 -17
  33. data/lib/geocoder/lookups/google_places_search.rb +30 -4
  34. data/lib/geocoder/lookups/google_premier.rb +14 -0
  35. data/lib/geocoder/lookups/here.rb +29 -23
  36. data/lib/geocoder/lookups/ip2location.rb +67 -0
  37. data/lib/geocoder/lookups/ipapi_com.rb +9 -13
  38. data/lib/geocoder/lookups/ipdata_co.rb +9 -4
  39. data/lib/geocoder/lookups/ipgeolocation.rb +51 -0
  40. data/lib/geocoder/lookups/ipinfo_io.rb +11 -29
  41. data/lib/geocoder/lookups/ipregistry.rb +68 -0
  42. data/lib/geocoder/lookups/ipstack.rb +11 -12
  43. data/lib/geocoder/lookups/latlon.rb +5 -6
  44. data/lib/geocoder/lookups/location_iq.rb +10 -4
  45. data/lib/geocoder/lookups/mapbox.rb +7 -6
  46. data/lib/geocoder/lookups/mapquest.rb +4 -5
  47. data/lib/geocoder/lookups/maxmind.rb +6 -6
  48. data/lib/geocoder/lookups/maxmind_geoip2.rb +8 -7
  49. data/lib/geocoder/lookups/maxmind_local.rb +7 -1
  50. data/lib/geocoder/lookups/nationaal_georegister_nl.rb +38 -0
  51. data/lib/geocoder/lookups/nominatim.rb +4 -4
  52. data/lib/geocoder/lookups/opencagedata.rb +6 -5
  53. data/lib/geocoder/lookups/osmnames.rb +57 -0
  54. data/lib/geocoder/lookups/pelias.rb +8 -9
  55. data/lib/geocoder/lookups/pickpoint.rb +9 -3
  56. data/lib/geocoder/lookups/pointpin.rb +10 -9
  57. data/lib/geocoder/lookups/postcode_anywhere_uk.rb +4 -5
  58. data/lib/geocoder/lookups/postcodes_io.rb +6 -3
  59. data/lib/geocoder/lookups/smarty_streets.rb +26 -11
  60. data/lib/geocoder/lookups/telize.rb +24 -4
  61. data/lib/geocoder/lookups/tencent.rb +59 -0
  62. data/lib/geocoder/lookups/test.rb +4 -0
  63. data/lib/geocoder/lookups/uk_ordnance_survey_names.rb +59 -0
  64. data/lib/geocoder/lookups/yandex.rb +7 -8
  65. data/lib/geocoder/query.rb +14 -0
  66. data/lib/geocoder/railtie.rb +1 -1
  67. data/lib/geocoder/results/abstract_api.rb +146 -0
  68. data/lib/geocoder/results/baidu.rb +10 -14
  69. data/lib/geocoder/results/ban_data_gouv_fr.rb +27 -2
  70. data/lib/geocoder/results/base.rb +13 -1
  71. data/lib/geocoder/results/bing.rb +1 -1
  72. data/lib/geocoder/results/db_ip_com.rb +1 -6
  73. data/lib/geocoder/results/freegeoip.rb +0 -5
  74. data/lib/geocoder/results/geocoder_ca.rb +3 -3
  75. data/lib/geocoder/results/geoip2.rb +0 -4
  76. data/lib/geocoder/results/geoportail_lu.rb +5 -3
  77. data/lib/geocoder/results/here.rb +4 -1
  78. data/lib/geocoder/results/ip2location.rb +22 -0
  79. data/lib/geocoder/results/ipdata_co.rb +0 -5
  80. data/lib/geocoder/results/ipgeolocation.rb +59 -0
  81. data/lib/geocoder/results/ipregistry.rb +304 -0
  82. data/lib/geocoder/results/maxmind.rb +0 -5
  83. data/lib/geocoder/results/maxmind_local.rb +0 -5
  84. data/lib/geocoder/results/nationaal_georegister_nl.rb +62 -0
  85. data/lib/geocoder/results/nominatim.rb +4 -0
  86. data/lib/geocoder/results/osmnames.rb +56 -0
  87. data/lib/geocoder/results/smarty_streets.rb +48 -18
  88. data/lib/geocoder/results/telize.rb +0 -5
  89. data/lib/geocoder/results/tencent.rb +72 -0
  90. data/lib/geocoder/results/test.rb +1 -1
  91. data/lib/geocoder/results/uk_ordnance_survey_names.rb +59 -0
  92. data/lib/geocoder/results/yandex.rb +217 -59
  93. data/lib/geocoder/sql.rb +4 -4
  94. data/lib/geocoder/stores/active_record.rb +1 -3
  95. data/lib/geocoder/util.rb +29 -0
  96. data/lib/geocoder/version.rb +1 -1
  97. data/lib/maxmind_database.rb +3 -3
  98. metadata +27 -23
  99. data/lib/geocoder/lookups/geocoder_us.rb +0 -43
  100. data/lib/geocoder/lookups/mapzen.rb +0 -15
  101. data/lib/geocoder/lookups/okf.rb +0 -44
  102. data/lib/geocoder/lookups/ovi.rb +0 -62
  103. data/lib/geocoder/results/geocoder_us.rb +0 -39
  104. data/lib/geocoder/results/mapzen.rb +0 -5
  105. data/lib/geocoder/results/okf.rb +0 -106
  106. data/lib/geocoder/results/ovi.rb +0 -71
  107. data/lib/hash_recursive_merge.rb +0 -74
@@ -15,51 +15,77 @@ module Geocoder::Result
15
15
  end
16
16
 
17
17
  def address
18
- [
19
- delivery_line_1,
20
- delivery_line_2,
21
- last_line
22
- ].select{ |i| i.to_s != "" }.join(" ")
18
+ parts =
19
+ if international_endpoint?
20
+ (1..12).map { |i| @data["address#{i}"] }
21
+ else
22
+ [
23
+ delivery_line_1,
24
+ delivery_line_2,
25
+ last_line
26
+ ]
27
+ end
28
+ parts.select{ |i| i.to_s != "" }.join(" ")
23
29
  end
24
30
 
25
31
  def state
26
- zipcode_endpoint? ?
27
- city_states.first['state'] :
32
+ if international_endpoint?
33
+ components['administrative_area']
34
+ elsif zipcode_endpoint?
35
+ city_states.first['state']
36
+ else
28
37
  components['state_abbreviation']
38
+ end
29
39
  end
30
40
 
31
41
  def state_code
32
- zipcode_endpoint? ?
33
- city_states.first['state_abbreviation'] :
42
+ if international_endpoint?
43
+ components['administrative_area']
44
+ elsif zipcode_endpoint?
45
+ city_states.first['state_abbreviation']
46
+ else
34
47
  components['state_abbreviation']
48
+ end
35
49
  end
36
50
 
37
51
  def country
38
- # SmartyStreets returns results for USA only
39
- "United States"
52
+ international_endpoint? ?
53
+ components['country_iso_3'] :
54
+ "United States"
40
55
  end
41
56
 
42
57
  def country_code
43
- # SmartyStreets returns results for USA only
44
- "US"
58
+ international_endpoint? ?
59
+ components['country_iso_3'] :
60
+ "US"
45
61
  end
46
62
 
47
63
  ## Extra methods not in base.rb ------------------------
48
64
 
49
65
  def street
50
- components['street_name']
66
+ international_endpoint? ?
67
+ components['thoroughfare_name'] :
68
+ components['street_name']
51
69
  end
52
70
 
53
71
  def city
54
- zipcode_endpoint? ?
55
- city_states.first['city'] :
72
+ if international_endpoint?
73
+ components['locality']
74
+ elsif zipcode_endpoint?
75
+ city_states.first['city']
76
+ else
56
77
  components['city_name']
78
+ end
57
79
  end
58
80
 
59
81
  def zipcode
60
- zipcode_endpoint? ?
61
- zipcodes.first['zipcode'] :
82
+ if international_endpoint?
83
+ components['postal_code']
84
+ elsif zipcode_endpoint?
85
+ zipcodes.first['zipcode']
86
+ else
62
87
  components['zipcode']
88
+ end
63
89
  end
64
90
  alias_method :postal_code, :zipcode
65
91
 
@@ -78,6 +104,10 @@ module Geocoder::Result
78
104
  zipcodes.any?
79
105
  end
80
106
 
107
+ def international_endpoint?
108
+ !@data['address1'].nil?
109
+ end
110
+
81
111
  [
82
112
  :delivery_line_1,
83
113
  :delivery_line_2,
@@ -3,11 +3,6 @@ require 'geocoder/results/base'
3
3
  module Geocoder::Result
4
4
  class Telize < Base
5
5
 
6
- def address(format = :full)
7
- s = state_code.to_s == "" ? "" : ", #{state_code}"
8
- "#{city}#{s} #{postal_code}, #{country}".sub(/^[ ,]*/, "")
9
- end
10
-
11
6
  def city
12
7
  @data['city']
13
8
  end
@@ -0,0 +1,72 @@
1
+ require 'geocoder/results/base'
2
+
3
+ module Geocoder::Result
4
+ class Tencent < Base
5
+
6
+ def coordinates
7
+ ['lat', 'lng'].map{ |i| @data['location'][i] }
8
+ end
9
+
10
+ def address
11
+ "#{province}#{city}#{district}#{street}#{street_number}"
12
+
13
+ #@data['title'] or @data['address']
14
+ end
15
+
16
+ # NOTE: The Tencent reverse geocoding API has the field named
17
+ # 'address_component' compared to 'address_components' in the
18
+ # regular geocoding API.
19
+ def province
20
+ @data['address_components'] and (@data['address_components']['province']) or
21
+ (@data['address_component'] and @data['address_component']['province']) or
22
+ ""
23
+ end
24
+
25
+ alias_method :state, :province
26
+
27
+ def city
28
+ @data['address_components'] and (@data['address_components']['city']) or
29
+ (@data['address_component'] and @data['address_component']['city']) or
30
+ ""
31
+ end
32
+
33
+ def district
34
+ @data['address_components'] and (@data['address_components']['district']) or
35
+ (@data['address_component'] and @data['address_component']['district']) or
36
+ ""
37
+ end
38
+
39
+ def street
40
+ @data['address_components'] and (@data['address_components']['street']) or
41
+ (@data['address_component'] and @data['address_component']['street']) or
42
+ ""
43
+ end
44
+
45
+ def street_number
46
+ @data['address_components'] and (@data['address_components']['street_number']) or
47
+ (@data['address_component'] and @data['address_component']['street_number']) or
48
+ ""
49
+ end
50
+
51
+ def address_components
52
+ @data['address_components'] or @data['address_component']
53
+ end
54
+
55
+ def state_code
56
+ ""
57
+ end
58
+
59
+ def postal_code
60
+ ""
61
+ end
62
+
63
+ def country
64
+ "China"
65
+ end
66
+
67
+ def country_code
68
+ "CN"
69
+ end
70
+
71
+ end
72
+ end
@@ -15,7 +15,7 @@ module Geocoder
15
15
  end
16
16
  end
17
17
 
18
- %w[latitude longitude neighborhood city state state_code sub_state
18
+ %w[coordinates neighborhood city state state_code sub_state
19
19
  sub_state_code province province_code postal_code country
20
20
  country_code address street_address street_number route geometry].each do |attr|
21
21
  add_result_attribute(attr)
@@ -0,0 +1,59 @@
1
+ require 'geocoder/results/base'
2
+ require 'easting_northing'
3
+
4
+ module Geocoder::Result
5
+ class UkOrdnanceSurveyNames < Base
6
+
7
+ def coordinates
8
+ @coordinates ||= Geocoder::EastingNorthing.new(
9
+ easting: data['GEOMETRY_X'],
10
+ northing: data['GEOMETRY_Y'],
11
+ ).lat_lng
12
+ end
13
+
14
+ def city
15
+ is_postcode? ? data['DISTRICT_BOROUGH'] : data['NAME1']
16
+ end
17
+
18
+ def county
19
+ data['COUNTY_UNITARY']
20
+ end
21
+ alias state county
22
+
23
+ def county_code
24
+ code_from_uri data['COUNTY_UNITARY_URI']
25
+ end
26
+ alias state_code county_code
27
+
28
+ def province
29
+ data['REGION']
30
+ end
31
+
32
+ def province_code
33
+ code_from_uri data['REGION_URI']
34
+ end
35
+
36
+ def postal_code
37
+ is_postcode? ? data['NAME1'] : ''
38
+ end
39
+
40
+ def country
41
+ 'United Kingdom'
42
+ end
43
+
44
+ def country_code
45
+ 'UK'
46
+ end
47
+
48
+ private
49
+
50
+ def is_postcode?
51
+ data['LOCAL_TYPE'] == 'Postcode'
52
+ end
53
+
54
+ def code_from_uri(uri)
55
+ return '' if uri.nil?
56
+ uri.split('/').last
57
+ end
58
+ end
59
+ end
@@ -2,78 +2,223 @@ require 'geocoder/results/base'
2
2
 
3
3
  module Geocoder::Result
4
4
  class Yandex < Base
5
+ # Yandex result has difficult tree structure,
6
+ # and presence of some nodes depends on exact search case.
7
+
8
+ # Also Yandex lacks documentation about it.
9
+ # See https://tech.yandex.com/maps/doc/geocoder/desc/concepts/response_structure-docpage/
10
+
11
+ # Ultimatly, we need to find Locality and/or Thoroughfare data.
12
+
13
+ # It may resides on the top (ADDRESS_DETAILS) level.
14
+ # example: 'Baltic Sea'
15
+ # "AddressDetails": {
16
+ # "Locality": {
17
+ # "Premise": {
18
+ # "PremiseName": "Baltic Sea"
19
+ # }
20
+ # }
21
+ # }
22
+
23
+ ADDRESS_DETAILS = %w[
24
+ GeoObject metaDataProperty GeocoderMetaData
25
+ AddressDetails
26
+ ].freeze
27
+
28
+ # On COUNTRY_LEVEL.
29
+ # example: 'Potomak'
30
+ # "AddressDetails": {
31
+ # "Country": {
32
+ # "AddressLine": "reka Potomak",
33
+ # "CountryNameCode": "US",
34
+ # "CountryName": "United States of America",
35
+ # "Locality": {
36
+ # "Premise": {
37
+ # "PremiseName": "reka Potomak"
38
+ # }
39
+ # }
40
+ # }
41
+ # }
42
+
43
+ COUNTRY_LEVEL = %w[
44
+ GeoObject metaDataProperty GeocoderMetaData
45
+ AddressDetails Country
46
+ ].freeze
47
+
48
+ # On ADMIN_LEVEL (usually state or city)
49
+ # example: 'Moscow, Tverskaya'
50
+ # "AddressDetails": {
51
+ # "Country": {
52
+ # "AddressLine": "Moscow, Tverskaya Street",
53
+ # "CountryNameCode": "RU",
54
+ # "CountryName": "Russia",
55
+ # "AdministrativeArea": {
56
+ # "AdministrativeAreaName": "Moscow",
57
+ # "Locality": {
58
+ # "LocalityName": "Moscow",
59
+ # "Thoroughfare": {
60
+ # "ThoroughfareName": "Tverskaya Street"
61
+ # }
62
+ # }
63
+ # }
64
+ # }
65
+ # }
66
+
67
+ ADMIN_LEVEL = %w[
68
+ GeoObject metaDataProperty GeocoderMetaData
69
+ AddressDetails Country
70
+ AdministrativeArea
71
+ ].freeze
72
+
73
+ # On SUBADMIN_LEVEL (may refer to urban district)
74
+ # example: 'Moscow Region, Krasnogorsk'
75
+ # "AddressDetails": {
76
+ # "Country": {
77
+ # "AddressLine": "Moscow Region, Krasnogorsk",
78
+ # "CountryNameCode": "RU",
79
+ # "CountryName": "Russia",
80
+ # "AdministrativeArea": {
81
+ # "AdministrativeAreaName": "Moscow Region",
82
+ # "SubAdministrativeArea": {
83
+ # "SubAdministrativeAreaName": "gorodskoy okrug Krasnogorsk",
84
+ # "Locality": {
85
+ # "LocalityName": "Krasnogorsk"
86
+ # }
87
+ # }
88
+ # }
89
+ # }
90
+ # }
91
+
92
+ SUBADMIN_LEVEL = %w[
93
+ GeoObject metaDataProperty GeocoderMetaData
94
+ AddressDetails Country
95
+ AdministrativeArea
96
+ SubAdministrativeArea
97
+ ].freeze
98
+
99
+ # On DEPENDENT_LOCALITY_1 (may refer to district of city)
100
+ # example: 'Paris, Etienne Marcel'
101
+ # "AddressDetails": {
102
+ # "Country": {
103
+ # "AddressLine": "Île-de-France, Paris, 1er Arrondissement, Rue Étienne Marcel",
104
+ # "CountryNameCode": "FR",
105
+ # "CountryName": "France",
106
+ # "AdministrativeArea": {
107
+ # "AdministrativeAreaName": "Île-de-France",
108
+ # "Locality": {
109
+ # "LocalityName": "Paris",
110
+ # "DependentLocality": {
111
+ # "DependentLocalityName": "1er Arrondissement",
112
+ # "Thoroughfare": {
113
+ # "ThoroughfareName": "Rue Étienne Marcel"
114
+ # }
115
+ # }
116
+ # }
117
+ # }
118
+ # }
119
+ # }
120
+
121
+ DEPENDENT_LOCALITY_1 = %w[
122
+ GeoObject metaDataProperty GeocoderMetaData
123
+ AddressDetails Country
124
+ AdministrativeArea Locality
125
+ DependentLocality
126
+ ].freeze
127
+
128
+ # On DEPENDENT_LOCALITY_2 (for special cases like turkish "mahalle")
129
+ # https://en.wikipedia.org/wiki/Mahalle
130
+ # example: 'Istanbul Mabeyinci Yokuşu 17'
131
+
132
+ # "AddressDetails": {
133
+ # "Country": {
134
+ # "AddressLine": "İstanbul, Fatih, Saraç İshak Mah., Mabeyinci Yokuşu, 17",
135
+ # "CountryNameCode": "TR",
136
+ # "CountryName": "Turkey",
137
+ # "AdministrativeArea": {
138
+ # "AdministrativeAreaName": "İstanbul",
139
+ # "SubAdministrativeArea": {
140
+ # "SubAdministrativeAreaName": "Fatih",
141
+ # "Locality": {
142
+ # "DependentLocality": {
143
+ # "DependentLocalityName": "Saraç İshak Mah.",
144
+ # "Thoroughfare": {
145
+ # "ThoroughfareName": "Mabeyinci Yokuşu",
146
+ # "Premise": {
147
+ # "PremiseNumber": "17"
148
+ # }
149
+ # }
150
+ # }
151
+ # }
152
+ # }
153
+ # }
154
+ # }
155
+ # }
156
+
157
+ DEPENDENT_LOCALITY_2 = %w[
158
+ GeoObject metaDataProperty GeocoderMetaData
159
+ AddressDetails Country
160
+ AdministrativeArea
161
+ SubAdministrativeArea Locality
162
+ DependentLocality
163
+ ].freeze
5
164
 
6
165
  def coordinates
7
166
  @data['GeoObject']['Point']['pos'].split(' ').reverse.map(&:to_f)
8
167
  end
9
168
 
10
- def address(format = :full)
169
+ def address(_format = :full)
11
170
  @data['GeoObject']['metaDataProperty']['GeocoderMetaData']['text']
12
171
  end
13
172
 
14
173
  def city
15
- if state.empty? and address_details and address_details.has_key? 'Locality'
16
- address_details['Locality']['LocalityName']
17
- elsif sub_state.empty? and address_details and address_details.has_key? 'AdministrativeArea' and
18
- address_details['AdministrativeArea'].has_key? 'Locality'
19
- address_details['AdministrativeArea']['Locality']['LocalityName']
20
- elsif not sub_state_city.empty?
21
- sub_state_city
22
- else
23
- ""
24
- end
174
+ result =
175
+ if state.empty?
176
+ find_in_hash(@data, *COUNTRY_LEVEL, 'Locality', 'LocalityName')
177
+ elsif sub_state.empty?
178
+ find_in_hash(@data, *ADMIN_LEVEL, 'Locality', 'LocalityName')
179
+ else
180
+ find_in_hash(@data, *SUBADMIN_LEVEL, 'Locality', 'LocalityName')
181
+ end
182
+
183
+ result || ""
25
184
  end
26
185
 
27
186
  def country
28
- if address_details
29
- address_details['CountryName']
30
- else
31
- ""
32
- end
187
+ find_in_hash(@data, *COUNTRY_LEVEL, 'CountryName') || ""
33
188
  end
34
189
 
35
190
  def country_code
36
- if address_details
37
- address_details['CountryNameCode']
38
- else
39
- ""
40
- end
191
+ find_in_hash(@data, *COUNTRY_LEVEL, 'CountryNameCode') || ""
41
192
  end
42
193
 
43
194
  def state
44
- if address_details and address_details['AdministrativeArea']
45
- address_details['AdministrativeArea']['AdministrativeAreaName']
46
- else
47
- ""
48
- end
195
+ find_in_hash(@data, *ADMIN_LEVEL, 'AdministrativeAreaName') || ""
49
196
  end
50
197
 
51
198
  def sub_state
52
- if !state.empty? and address_details and address_details['AdministrativeArea']['SubAdministrativeArea']
53
- address_details['AdministrativeArea']['SubAdministrativeArea']['SubAdministrativeAreaName']
54
- else
55
- ""
56
- end
199
+ return "" if state.empty?
200
+ find_in_hash(@data, *SUBADMIN_LEVEL, 'SubAdministrativeAreaName') || ""
57
201
  end
58
202
 
59
203
  def state_code
60
204
  ""
61
205
  end
62
206
 
63
- def postal_code
64
- ""
207
+ def street
208
+ thoroughfare_data.is_a?(Hash) ? thoroughfare_data['ThoroughfareName'] : ""
65
209
  end
66
210
 
67
- def premise_name
68
- address_details['Locality']['Premise']['PremiseName']
211
+ def street_number
212
+ premise.is_a?(Hash) ? premise.fetch('PremiseNumber', "") : ""
69
213
  end
70
214
 
71
- def street
72
- thoroughfare_data && thoroughfare_data['ThoroughfareName']
215
+ def premise_name
216
+ premise.is_a?(Hash) ? premise.fetch('PremiseName', "") : ""
73
217
  end
74
218
 
75
- def street_number
76
- thoroughfare_data && thoroughfare_data['Premise'] && thoroughfare_data['Premise']['PremiseNumber']
219
+ def postal_code
220
+ return "" unless premise.is_a?(Hash)
221
+ find_in_hash(premise, 'PostalCode', 'PostalCodeNumber') || ""
77
222
  end
78
223
 
79
224
  def kind
@@ -93,42 +238,55 @@ module Geocoder::Result
93
238
 
94
239
  private # ----------------------------------------------------------------
95
240
 
96
- def thoroughfare_data
97
- locality_data && locality_data['Thoroughfare']
241
+ def top_level_locality
242
+ find_in_hash(@data, *ADDRESS_DETAILS, 'Locality')
98
243
  end
99
244
 
100
- def locality_data
101
- dependent_locality && subadmin_locality && admin_locality
245
+ def country_level_locality
246
+ find_in_hash(@data, *COUNTRY_LEVEL, 'Locality')
102
247
  end
103
248
 
104
249
  def admin_locality
105
- address_details && address_details['AdministrativeArea'] &&
106
- address_details['AdministrativeArea']['Locality']
250
+ find_in_hash(@data, *ADMIN_LEVEL, 'Locality')
107
251
  end
108
252
 
109
253
  def subadmin_locality
110
- address_details && address_details['AdministrativeArea'] &&
111
- address_details['AdministrativeArea']['SubAdministrativeArea'] &&
112
- address_details['AdministrativeArea']['SubAdministrativeArea']['Locality']
254
+ find_in_hash(@data, *SUBADMIN_LEVEL, 'Locality')
113
255
  end
114
256
 
115
257
  def dependent_locality
116
- address_details && address_details['AdministrativeArea'] &&
117
- address_details['AdministrativeArea']['SubAdministrativeArea'] &&
118
- address_details['AdministrativeArea']['SubAdministrativeArea']['Locality'] &&
119
- address_details['AdministrativeArea']['SubAdministrativeArea']['Locality']['DependentLocality']
258
+ find_in_hash(@data, *DEPENDENT_LOCALITY_1) ||
259
+ find_in_hash(@data, *DEPENDENT_LOCALITY_2)
260
+ end
261
+
262
+ def locality_data
263
+ dependent_locality || subadmin_locality || admin_locality ||
264
+ country_level_locality || top_level_locality
265
+ end
266
+
267
+ def thoroughfare_data
268
+ locality_data['Thoroughfare'] if locality_data.is_a?(Hash)
120
269
  end
121
270
 
122
- def address_details
123
- @data['GeoObject']['metaDataProperty']['GeocoderMetaData']['AddressDetails']['Country']
271
+ def premise
272
+ if thoroughfare_data.is_a?(Hash)
273
+ thoroughfare_data['Premise']
274
+ elsif locality_data.is_a?(Hash)
275
+ locality_data['Premise']
276
+ end
124
277
  end
125
278
 
126
- def sub_state_city
127
- if !sub_state.empty? and address_details and address_details['AdministrativeArea']['SubAdministrativeArea'].has_key? 'Locality'
128
- address_details['AdministrativeArea']['SubAdministrativeArea']['Locality']['LocalityName'] || ""
129
- else
130
- ""
279
+ def find_in_hash(source, *keys)
280
+ key = keys.shift
281
+ result = source[key]
282
+
283
+ if keys.empty?
284
+ return result
285
+ elsif !result.is_a?(Hash)
286
+ return nil
131
287
  end
288
+
289
+ find_in_hash(result, *keys)
132
290
  end
133
291
  end
134
292
  end