From 2650de9670fea322643099fe2297687e57ef4603 Mon Sep 17 00:00:00 2001 From: Martin Laursen Date: Thu, 6 Jan 2022 16:00:26 +0100 Subject: [PATCH] retry when dawa command fails --- lib/aion_cli/helpers/dawa_client.rb | 39 ++++++++++++++++------------- lib/aion_cli/main.rb | 2 ++ lib/aion_cli/version.rb | 2 +- 3 files changed, 25 insertions(+), 18 deletions(-) diff --git a/lib/aion_cli/helpers/dawa_client.rb b/lib/aion_cli/helpers/dawa_client.rb index 762dd64..89b4b22 100644 --- a/lib/aion_cli/helpers/dawa_client.rb +++ b/lib/aion_cli/helpers/dawa_client.rb @@ -13,16 +13,19 @@ class DAWAClient def initialize; end def scrub(address_string) - request(URL_SCRUB_ADDRESS, betegnelse: prepare_address_string(address_string)) + dawa_response = request(URL_SCRUB_ADDRESS, params: { betegnelse: prepare_address_string(address_string) }) + $stderr << "Address validation failed for: '#{address_string}'\n" if dawa_response.nil? + dawa_response end def address(address_string) scrub_object = scrub(address_string) - if ['A','B'].include?(scrub_object['kategori']) - a = scrub_object['resultater'][0]['aktueladresse'] - a = scrub_object['resultater'][0]['adresse'] if a.nil? - [1,3].include?(a['status']) ? a : nil - end + return if scrub_object.nil? + return unless %w[A B].include?(scrub_object['kategori']) + + a = scrub_object['resultater'][0]['aktueladresse'] + a = scrub_object['resultater'][0]['adresse'] if a.nil? + [1, 3].include?(a['status']) ? a : nil end def address_by_guid(address_guid) @@ -50,9 +53,7 @@ def address_object_to_s(address_object) out << ' ' << address_object['dør'] if address_object['dør'] out << ', ' end - if address_object['supplerendebynavn'] - out << address_object['supplerendebynavn'] << ', ' - end + out << address_object['supplerendebynavn'] << ', ' if address_object['supplerendebynavn'] out << address_object['postnr'] << ' ' << address_object['postnrnavn'] out.string end @@ -81,20 +82,24 @@ def municipality_names(codes) # Helper method for returning objects from requests - def request(url, params = {}) - json = HTTP.timeout(connect: 5, write: 30, read: 30).get(url, params: params).to_s - JSON.parse(json) - rescue => e - $stderr << e.message + def request(url, params: {}, max_retries: 3) + max_retries.times do + dawa_response = HTTP.timeout(connect: 5, write: 30, read: 30).get(url, params: params).to_s + return JSON.parse(dawa_response) + rescue JSON::ParserError => e + $stderr << "Error occured when attempting to parse response from DAWA: #{e.message}\nRetrying...\n" + rescue => e + $stderr << "Error occured: #{e.message}\nRetrying...\n" + end + + $stderr << "Request failed #{max_retries} times, skipping\n" nil end private def prepare_address_string(address_string) - unless address_string.is_a? String - raise ArgumentError, 'Supplied address is not a string' - end + raise ArgumentError, 'Supplied address is not a string' unless address_string.is_a? String # DAWA service does not handle leading zeroes that well # address_string.gsub(/ 0+/, ' ') diff --git a/lib/aion_cli/main.rb b/lib/aion_cli/main.rb index 50bcecf..1157b71 100644 --- a/lib/aion_cli/main.rb +++ b/lib/aion_cli/main.rb @@ -50,8 +50,10 @@ def install say("aion script installed into path #{path}") end + method_option :ruby, type: :boolean, desc: 'Also print version of ruby used to run CLI' desc 'version', 'Print aion version and exit' def version + say("Running ruby version is #{RUBY_VERSION}") if options[:ruby] say(AionCLI::VERSION) end diff --git a/lib/aion_cli/version.rb b/lib/aion_cli/version.rb index 9b1cd98..9b5e028 100644 --- a/lib/aion_cli/version.rb +++ b/lib/aion_cli/version.rb @@ -1,3 +1,3 @@ module AionCLI - VERSION = '0.2.8' + VERSION = '0.2.9' end