Class: Geocoder::Result::Yandex

Inherits:
Base
  • Object
show all
Defined in:
lib/geocoder/results/yandex.rb

Constant Summary collapse

ADDRESS_DETAILS =

It may resides on the top (ADDRESS_DETAILS) level. example: 'Baltic Sea' “AddressDetails”: {

"Locality": {
  "Premise": {
    "PremiseName": "Baltic Sea"
  }
}

}

%w[
  GeoObject metaDataProperty GeocoderMetaData
  AddressDetails
].freeze
COUNTRY_LEVEL =

On COUNTRY_LEVEL. example: 'Potomak' “AddressDetails”: {

"Country": {
  "AddressLine": "reka Potomak",
  "CountryNameCode": "US",
  "CountryName": "United States of America",
  "Locality": {
    "Premise": {
      "PremiseName": "reka Potomak"
    }
  }
}

}

%w[
  GeoObject metaDataProperty GeocoderMetaData
  AddressDetails Country
].freeze
ADMIN_LEVEL =

On ADMIN_LEVEL (usually state or city) example: 'Moscow, Tverskaya' “AddressDetails”: {

"Country": {
  "AddressLine": "Moscow, Tverskaya Street",
  "CountryNameCode": "RU",
  "CountryName": "Russia",
  "AdministrativeArea": {
    "AdministrativeAreaName": "Moscow",
    "Locality": {
      "LocalityName": "Moscow",
      "Thoroughfare": {
        "ThoroughfareName": "Tverskaya Street"
      }
    }
  }
}

}

%w[
  GeoObject metaDataProperty GeocoderMetaData
  AddressDetails Country
  AdministrativeArea
].freeze
SUBADMIN_LEVEL =

On SUBADMIN_LEVEL (may refer to urban district) example: 'Moscow Region, Krasnogorsk' “AddressDetails”: {

"Country": {
  "AddressLine": "Moscow Region, Krasnogorsk",
  "CountryNameCode": "RU",
  "CountryName": "Russia",
  "AdministrativeArea": {
    "AdministrativeAreaName": "Moscow Region",
    "SubAdministrativeArea": {
      "SubAdministrativeAreaName": "gorodskoy okrug Krasnogorsk",
      "Locality": {
        "LocalityName": "Krasnogorsk"
      }
    }
  }
}

}

%w[
  GeoObject metaDataProperty GeocoderMetaData
  AddressDetails Country
  AdministrativeArea
  SubAdministrativeArea
].freeze
DEPENDENT_LOCALITY_1 =

On DEPENDENT_LOCALITY_1 (may refer to district of city) example: 'Paris, Etienne Marcel' “AddressDetails”: {

"Country": {
  "AddressLine": "Île-de-France, Paris, 1er Arrondissement, Rue Étienne Marcel",
  "CountryNameCode": "FR",
  "CountryName": "France",
  "AdministrativeArea": {
    "AdministrativeAreaName": "Île-de-France",
    "Locality": {
      "LocalityName": "Paris",
      "DependentLocality": {
        "DependentLocalityName": "1er Arrondissement",
        "Thoroughfare": {
          "ThoroughfareName": "Rue Étienne Marcel"
        }
      }
    }
  }
}

}

%w[
  GeoObject metaDataProperty GeocoderMetaData
  AddressDetails Country
  AdministrativeArea Locality
  DependentLocality
].freeze
DEPENDENT_LOCALITY_2 =

“AddressDetails”: {

"Country": {
  "AddressLine": "İstanbul, Fatih, Saraç İshak Mah., Mabeyinci Yokuşu, 17",
  "CountryNameCode": "TR",
  "CountryName": "Turkey",
  "AdministrativeArea": {
    "AdministrativeAreaName": "İstanbul",
    "SubAdministrativeArea": {
      "SubAdministrativeAreaName": "Fatih",
      "Locality": {
        "DependentLocality": {
          "DependentLocalityName": "Saraç İshak Mah.",
          "Thoroughfare": {
            "ThoroughfareName": "Mabeyinci Yokuşu",
            "Premise": {
              "PremiseNumber": "17"
            }
          }
        }
      }
    }
  }
}

}

%w[
  GeoObject metaDataProperty GeocoderMetaData
  AddressDetails Country
  AdministrativeArea
  SubAdministrativeArea Locality
  DependentLocality
].freeze

Instance Attribute Summary

Attributes inherited from Base

#cache_hit, #data

Instance Method Summary collapse

Methods inherited from Base

#initialize, #latitude, #longitude, #province, #province_code

Constructor Details

This class inherits a constructor from Geocoder::Result::Base

Instance Method Details

#address(_format = :full) ⇒ Object


169
170
171
# File 'lib/geocoder/results/yandex.rb', line 169

def address(_format = :full)
  @data['GeoObject']['metaDataProperty']['GeocoderMetaData']['text']
end

#cityObject


173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/geocoder/results/yandex.rb', line 173

def city
  result =
    if state.empty?
      find_in_hash(@data, *COUNTRY_LEVEL, 'Locality', 'LocalityName')
    elsif sub_state.empty?
      find_in_hash(@data, *ADMIN_LEVEL, 'Locality', 'LocalityName')
    else
      find_in_hash(@data, *SUBADMIN_LEVEL, 'Locality', 'LocalityName')
    end

  result || ""
end

#coordinatesObject


165
166
167
# File 'lib/geocoder/results/yandex.rb', line 165

def coordinates
  @data['GeoObject']['Point']['pos'].split(' ').reverse.map(&:to_f)
end

#countryObject


186
187
188
# File 'lib/geocoder/results/yandex.rb', line 186

def country
  find_in_hash(@data, *COUNTRY_LEVEL, 'CountryName') || ""
end

#country_codeObject


190
191
192
# File 'lib/geocoder/results/yandex.rb', line 190

def country_code
  find_in_hash(@data, *COUNTRY_LEVEL, 'CountryNameCode') || ""
end

#kindObject


224
225
226
# File 'lib/geocoder/results/yandex.rb', line 224

def kind
  @data['GeoObject']['metaDataProperty']['GeocoderMetaData']['kind']
end

#postal_codeObject


219
220
221
222
# File 'lib/geocoder/results/yandex.rb', line 219

def postal_code
  return "" unless premise.is_a?(Hash)
  find_in_hash(premise, 'PostalCode', 'PostalCodeNumber') || ""
end

#precisionObject


228
229
230
# File 'lib/geocoder/results/yandex.rb', line 228

def precision
  @data['GeoObject']['metaDataProperty']['GeocoderMetaData']['precision']
end

#premise_nameObject


215
216
217
# File 'lib/geocoder/results/yandex.rb', line 215

def premise_name
  premise.is_a?(Hash) ? premise.fetch('PremiseName', "") : ""
end

#stateObject


194
195
196
# File 'lib/geocoder/results/yandex.rb', line 194

def state
  find_in_hash(@data, *ADMIN_LEVEL, 'AdministrativeAreaName') || ""
end

#state_codeObject


203
204
205
# File 'lib/geocoder/results/yandex.rb', line 203

def state_code
  ""
end

#streetObject


207
208
209
# File 'lib/geocoder/results/yandex.rb', line 207

def street
  thoroughfare_data.is_a?(Hash) ? thoroughfare_data['ThoroughfareName'] : ""
end

#street_numberObject


211
212
213
# File 'lib/geocoder/results/yandex.rb', line 211

def street_number
  premise.is_a?(Hash) ? premise.fetch('PremiseNumber', "") : ""
end

#sub_stateObject


198
199
200
201
# File 'lib/geocoder/results/yandex.rb', line 198

def sub_state
  return "" if state.empty?
  find_in_hash(@data, *SUBADMIN_LEVEL, 'SubAdministrativeAreaName') || ""
end

#viewportObject


232
233
234
235
236
237
# File 'lib/geocoder/results/yandex.rb', line 232

def viewport
  envelope = @data['GeoObject']['boundedBy']['Envelope'] || fail
  east, north = envelope['upperCorner'].split(' ').map(&:to_f)
  west, south = envelope['lowerCorner'].split(' ').map(&:to_f)
  [south, west, north, east]
end