Skip to content

Instantly share code, notes, and snippets.

@Tocacar
Created June 28, 2019 13:56
Show Gist options
  • Select an option

  • Save Tocacar/3802a34dbe91eab56eac24e32bf2572d to your computer and use it in GitHub Desktop.

Select an option

Save Tocacar/3802a34dbe91eab56eac24e32bf2572d to your computer and use it in GitHub Desktop.

Revisions

  1. Tocacar created this gist Jun 28, 2019.
    37 changes: 37 additions & 0 deletions sample.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    # 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