Class: HolidaysRest::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/holidays_rest/client.rb

Constant Summary collapse

BASE_URL =
"https://api.holidays.rest/v1"

Instance Method Summary collapse

Constructor Details

#initialize(api_key:, base_url: BASE_URL, open_timeout: 5, read_timeout: 15) ⇒ Client

Returns a new instance of Client.

Parameters:

  • api_key (String)

    Bearer token from www.holidays.rest/dashboard

  • base_url (String) (defaults to: BASE_URL)

    Override base URL (useful for testing)

  • open_timeout (Integer) (defaults to: 5)

    Seconds to wait for connection (default 5)

  • read_timeout (Integer) (defaults to: 15)

    Seconds to wait for response (default 15)

Raises:

  • (ArgumentError)


13
14
15
16
17
18
19
20
# File 'lib/holidays_rest/client.rb', line 13

def initialize(api_key:, base_url: BASE_URL, open_timeout: 5, read_timeout: 15)
  raise ArgumentError, "api_key must not be empty" if api_key.nil? || api_key.empty?

  @api_key      = api_key
  @base_url     = base_url.chomp("/")
  @open_timeout = open_timeout
  @read_timeout = read_timeout
end

Instance Method Details

#countriesArray<Country>

Return all supported countries.

Returns:



52
53
54
# File 'lib/holidays_rest/client.rb', line 52

def countries
  get("/countries", {}).map { Country.from_hash(_1) }
end

#country(country_code) ⇒ Country

Return details for one country, including subdivision codes.

Parameters:

  • country_code (String)

    ISO 3166 alpha-2 code, e.g. “US”

Returns:

Raises:

  • (ArgumentError)


59
60
61
62
63
# File 'lib/holidays_rest/client.rb', line 59

def country(country_code)
  raise ArgumentError, "country_code must not be empty" if country_code.nil? || country_code.empty?

  Country.from_hash(get("/country/#{URI.encode_uri_component(country_code)}", {}))
end

#holidays(country:, year:, month: nil, day: nil, type: nil, religion: nil, region: nil, lang: nil, response: nil) ⇒ Array<Holiday>

Fetch public holidays.

Parameters:

  • country (String)

    ISO 3166 alpha-2 code, e.g. “US” (required)

  • year (Integer, String)

    Four-digit year (required)

  • month (Integer, String) (defaults to: nil)

    1–12 (optional)

  • day (Integer, String) (defaults to: nil)

    1–31 (optional)

  • type (String, Array<String>) (defaults to: nil)

    “religious”, “national”, “local”

  • religion (Integer, Array<Integer>) (defaults to: nil)

    Religion code(s) 1–11

  • region (String, Array<String>) (defaults to: nil)

    Subdivision code(s)

  • lang (String, Array<String>) (defaults to: nil)

    Language code(s)

  • response (String) (defaults to: nil)

    “json” | “xml” | “yaml” | “csv”

Returns:



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/holidays_rest/client.rb', line 34

def holidays(country:, year:, month: nil, day: nil, type: nil,
             religion: nil, region: nil, lang: nil, response: nil)
  params = build_params(
    country:  country,
    year:     year,
    month:    month,
    day:      day,
    type:     type,
    religion: religion,
    region:   region,
    lang:     lang,
    response: response
  )
  get("/holidays", params).map { Holiday.from_hash(_1) }
end

#languagesArray<Language>

Return all supported language codes.

Returns:



67
68
69
# File 'lib/holidays_rest/client.rb', line 67

def languages
  get("/languages", {}).map { Language.from_hash(_1) }
end