Skip to content

Commit

Permalink
Merge pull request #6 from aion-dk/retry-dawa-wash
Browse files Browse the repository at this point in the history
Retry when dawa command fails
  • Loading branch information
Afibsen authored Jan 6, 2022
2 parents a482d1d + 2650de9 commit 2ecfdd3
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 18 deletions.
39 changes: 22 additions & 17 deletions lib/aion_cli/helpers/dawa_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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+/, ' ')
Expand Down
2 changes: 2 additions & 0 deletions lib/aion_cli/main.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion lib/aion_cli/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module AionCLI
VERSION = '0.2.8'
VERSION = '0.2.9'
end

0 comments on commit 2ecfdd3

Please sign in to comment.