# encoding: utf-8require'geocoder/results/base'moduleGeocoder::ResultclassBanDataGouvFr<BaseSTATE_CODE_MAPPINGS={"Guadeloupe"=>"01","Martinique"=>"02","Guyane"=>"03","La Réunion"=>"04","Mayotte"=>"06","Île-de-France"=>"11","Centre-Val de Loire"=>"24","Bourgogne-Franche-Comté"=>"27","Normandie"=>"28","Hauts-de-France"=>"32","Grand Est"=>"44","Pays de la Loire"=>"52","Bretagne"=>"53","Nouvelle-Aquitaine"=>"75","Occitanie"=>"76","Auvergne-Rhône-Alpes"=>"84","Provence-Alpes-Côte d'Azur"=>"93","Corse"=>"94"}.freeze#### BASE METHODS ####defself.response_attributes%w[limit attribution version licence type features center]endresponse_attributes.eachdo|a|unlessmethod_defined?(a)define_methodado@data[a]endendend#### BEST RESULT ####defresultfeatures[0]iffeatures.any?end#### GEOMETRY ####defgeometryresult['geometry']ifresultenddefprecisiongeometry['type']ifgeometryenddefcoordinatescoords=geometry["coordinates"]return[coords[1].to_f,coords[0].to_f]end#### PROPERTIES ##### List of raw attrbutes returned by BAN data gouv fr API:## :id => [string] UUID of the result, said to be not stable# atm, based on IGN reference (Institut national de# l'information géographique et forestière)## :type => [string] result type (housenumber, street, city,# town, village, locality)## :score => [float] value between 0 and 1 giving result's# relevancy## :housenumber => [string] street number and extra information# (bis, ter, A, B)## :street => [string] street name## :name => [string] housenumber and street name## :postcode => [string] city post code (used for mails by La Poste,# beware many cities got severeal postcodes)## :citycode => [string] city code (INSEE reference,# consider it as a french institutional UUID)## :city => [string] city name## :context => [string] department code, department name and# region code## :label => [string] full address without state, country name# and country code## CITIES ONLY PROPERTIES## :adm_weight => [string] administrative weight (importance) of# the city## :population => [float] number of inhabitants with a 1000 factor## For up to date doc (in french only) : https://adresse.data.gouv.fr/api/#defpropertiesresult['properties']ifresultend# List of usable Geocoder results' methods## score => [float] result relevance 0 to 1## location_id => [string] location's IGN UUID## result_type => [string] housenumber / street / city# / town / village / locality## international_address => [string] full address with country code## national_address => [string] full address with country code## street_address => [string] housenumber + extra inf# + street name## street_number => [string] housenumber + extra inf# (bis, ter, etc)## street_name => [string] street's name## city_name => [string] city's name## city_code => [string] city's INSEE UUID## postal_code => [string] city's postal code (used for mails)## context => [string] city's department code, department# name and region name## demartment_name => [string] city's department name## department_code => [string] city's department INSEE UUID## region_name => [string] city's region name## population => [string] city's inhabitants count## administrative_weight => [integer] city's importance on a scale# from 6 (capital city) to 1 (regular village)#defscoreproperties['score']enddeflocation_idproperties['id']end# Types## housenumber# street# city# town# village# locality#defresult_typeproperties['type']enddefinternational_address"#{national_address}, #{country}"enddefnational_addressproperties['label']enddefstreet_addressproperties['name']enddefstreet_numberproperties['housenumber']enddefstreet_nameproperties['street']enddefcity_nameproperties['city']enddefcity_codeproperties['citycode']enddefpostal_codeproperties['postcode']enddefcontextproperties['context'].split(/,/).map(&:strip)enddefdepartment_codecontext[0]ifcontext.length>0end# Monkey logic to handle fact Paris is both a city and a department# in Île-de-France regiondefdepartment_nameifcontext.length>1ifcontext[1]=="Île-de-France""Paris"elsecontext[1]endendenddefregion_nameifcontext.length==2&&context[1]=="Île-de-France"context[1]elsifcontext.length>2context[2]endenddefregion_codeSTATE_CODE_MAPPINGS[region_name]enddefcountry"France"end# Country code types# FR : France# GF : Guyane Française# RE : Réunion# NC : Nouvelle-Calédonie# GP : Guadeloupe# MQ : Martinique# MU : Maurice# PF : Polynésie française## Will need refacto to handle different country codes, but BAN API# is currently mainly designed for geocode FR country code addressesdefcountry_code"FR"end#### ALIAS METHODS ####alias_method:address,:international_addressalias_method:street,:street_namealias_method:city,:city_namealias_method:state,:region_namealias_method:state_code,:region_code#### CITIES' METHODS ####defpopulation(properties['population'].to_f*1000).to_iifcity?(result_type)enddefadministrative_weightproperties['adm_weight'].to_iifcity?(result_type)endprivatedefcity?(result_type)result_type=='municipality'endendend