Refactor ApiAdresse::AddressAdapter

This commit is contained in:
gregoirenovel 2018-10-15 23:21:22 +02:00
parent 7af41b35a2
commit 81d2b27160
2 changed files with 38 additions and 14 deletions

View file

@ -0,0 +1,33 @@
class ApiAdresse::Adapter
private
def initialize(address, limit, blank_return)
@address = address
@limit = limit
@blank_return = blank_return
end
def features
@features ||= get_features
end
def get_features
response = ApiAdresse::API.call(@address, @limit)
result = JSON.parse(response)
result['features']
rescue RestClient::Exception, JSON::ParserError, TypeError
@blank_return
end
def handle_result
if features.present?
process_features
else
@blank_return
end
end
def process_features
raise NoMethodError
end
end

View file

@ -1,26 +1,17 @@
class ApiAdresse::AddressAdapter
class ApiAdresse::AddressAdapter < ApiAdresse::Adapter
def initialize(address)
@address = address
super(address, 5, [])
end
def get_suggestions
@list ||= convert_api_result_to_full_address
handle_result
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|
def process_features
features.map do |feature|
feature['properties']['label']
end
rescue TypeError, JSON::ParserError
[]
end
end