Unify scoping in app/lib

This commit is contained in:
gregoirenovel 2018-10-15 17:26:06 +02:00
parent 831cd56b95
commit c654122e90
4 changed files with 58 additions and 66 deletions

View file

@ -1,28 +1,26 @@
module ApiAdresse
class AddressRetriever
def initialize(address)
@address = address
class ApiAdresse::AddressRetriever
def initialize(address)
@address = address
end
def list
@list ||= convert_api_result_to_full_address
end
private
def convert_api_result_to_full_address
result = JSON.parse(ApiAdresse::API.call(@address, 5))
if result['features'].empty?
Rails.logger.error "unable to find location for address #{@address}"
return []
end
def list
@list ||= convert_api_result_to_full_address
end
private
def convert_api_result_to_full_address
result = JSON.parse(ApiAdresse::API.call(@address, 5))
if result['features'].empty?
Rails.logger.error "unable to find location for address #{@address}"
return []
end
result['features'].map do |feature|
feature['properties']['label']
end
rescue TypeError, JSON::ParserError
[]
result['features'].map do |feature|
feature['properties']['label']
end
rescue TypeError, JSON::ParserError
[]
end
end

View file

@ -1,10 +1,8 @@
module ApiAdresse
class Geocodeur
def self.convert_adresse_to_point(address)
ApiAdresse::PointRetriever.new(address).point
rescue RestClient::Exception, JSON::ParserError => e
Rails.logger.error(e.message)
nil
end
class ApiAdresse::Geocodeur
def self.convert_adresse_to_point(address)
ApiAdresse::PointRetriever.new(address).point
rescue RestClient::Exception, JSON::ParserError => e
Rails.logger.error(e.message)
nil
end
end

View file

@ -1,22 +1,20 @@
module ApiAdresse
class PointRetriever
def initialize(address)
@address = address
end
class ApiAdresse::PointRetriever
def initialize(address)
@address = address
end
def point
@point ||= convert_api_result_to_point
end
def point
@point ||= convert_api_result_to_point
end
private
private
def convert_api_result_to_point
result = JSON.parse(ApiAdresse::API.call(@address))
if result['features'].empty?
Rails.logger.error "unable to find location for address #{@address}"
return nil
end
RGeo::GeoJSON.decode(result['features'][0]['geometry'], json_parser: :json)
def convert_api_result_to_point
result = JSON.parse(ApiAdresse::API.call(@address))
if result['features'].empty?
Rails.logger.error "unable to find location for address #{@address}"
return nil
end
RGeo::GeoJSON.decode(result['features'][0]['geometry'], json_parser: :json)
end
end

View file

@ -1,25 +1,23 @@
module ApiGeo
class Driver
def self.regions
url = [API_GEO_URL, "regions"].join("/")
call(url)
end
class ApiGeo::Driver
def self.regions
url = [API_GEO_URL, "regions"].join("/")
call(url)
end
def self.departements
url = [API_GEO_URL, "departements"].join("/")
call(url)
end
def self.departements
url = [API_GEO_URL, "departements"].join("/")
call(url)
end
def self.pays
File.open('app/lib/api_geo/pays.json').read
end
def self.pays
File.open('app/lib/api_geo/pays.json').read
end
private
private
def self.call(url)
RestClient.get(url, params: { fields: :nom })
rescue RestClient::ServiceUnavailable
nil
end
def self.call(url)
RestClient.get(url, params: { fields: :nom })
rescue RestClient::ServiceUnavailable
nil
end
end