Refactor ApiAdresse::AddressAdapter
This commit is contained in:
parent
7af41b35a2
commit
81d2b27160
2 changed files with 38 additions and 14 deletions
33
app/lib/api_adresse/adapter.rb
Normal file
33
app/lib/api_adresse/adapter.rb
Normal 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
|
|
@ -1,26 +1,17 @@
|
||||||
class ApiAdresse::AddressAdapter
|
class ApiAdresse::AddressAdapter < ApiAdresse::Adapter
|
||||||
def initialize(address)
|
def initialize(address)
|
||||||
@address = address
|
super(address, 5, [])
|
||||||
end
|
end
|
||||||
|
|
||||||
def get_suggestions
|
def get_suggestions
|
||||||
@list ||= convert_api_result_to_full_address
|
handle_result
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def convert_api_result_to_full_address
|
def process_features
|
||||||
result = JSON.parse(ApiAdresse::API.call(@address, 5))
|
features.map do |feature|
|
||||||
|
|
||||||
if result['features'].empty?
|
|
||||||
Rails.logger.error "unable to find location for address #{@address}"
|
|
||||||
return []
|
|
||||||
end
|
|
||||||
|
|
||||||
result['features'].map do |feature|
|
|
||||||
feature['properties']['label']
|
feature['properties']['label']
|
||||||
end
|
end
|
||||||
rescue TypeError, JSON::ParserError
|
|
||||||
[]
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue