diff --git a/app/lib/api_adresse/adapter.rb b/app/lib/api_adresse/adapter.rb new file mode 100644 index 000000000..cd012357b --- /dev/null +++ b/app/lib/api_adresse/adapter.rb @@ -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 diff --git a/app/lib/api_adresse/address_adapter.rb b/app/lib/api_adresse/address_adapter.rb index 747899137..68e737cfe 100644 --- a/app/lib/api_adresse/address_adapter.rb +++ b/app/lib/api_adresse/address_adapter.rb @@ -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