Skip to content

Instantly share code, notes, and snippets.

@Tocacar
Created June 28, 2019 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tocacar/3802a34dbe91eab56eac24e32bf2572d to your computer and use it in GitHub Desktop.
Save Tocacar/3802a34dbe91eab56eac24e32bf2572d to your computer and use it in GitHub Desktop.
# frozen_string_literal: true
class BankHolidayRetriever
UnsuccessfulRetrievalError = Class.new(StandardError)
API_URL = 'https://www.gov.uk/bank-holidays.json'
DEFAULT_GROUP = 'england-and-wales'
def self.dates
new.dates(DEFAULT_GROUP)
end
def data
return raise_error unless response.is_a?(Net::HTTPOK)
@data ||= JSON.parse(response.body)
end
def dates(group)
return if data.empty?
data.dig(group, 'events')&.pluck('date')
end
private
def response
@response ||= Net::HTTP.get_response(uri)
end
def uri
URI.parse(API_URL)
end
def raise_error
raise UnsuccessfulRetrievalError, "Retrieval Failed: #{response.message} (#{response.code}) #{response.body}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment