Class: DateHolidays::Reader::Locale

Inherits:
Object
  • Object
show all
Defined in:
lib/date_holidays/reader/locale.rb

Overview

Represents specific locale which contains holidays. This is the main entry point into the gem.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(country:, state: nil, region: nil, js_bridge: JsBridge.new) ⇒ Locale

Returns a new instance of Locale.



17
18
19
20
21
22
23
24
# File 'lib/date_holidays/reader/locale.rb', line 17

def initialize(country:, state: nil, region: nil, js_bridge: JsBridge.new)
  @country = country || raise(ArgumentError, 'a country is required')
  @state = state
  @region = region
  @js_bridge = js_bridge

  freeze
end

Instance Attribute Details

#countryObject (readonly)

Returns the value of attribute country.



15
16
17
# File 'lib/date_holidays/reader/locale.rb', line 15

def country
  @country
end

#js_bridgeObject (readonly)

Returns the value of attribute js_bridge.



15
16
17
# File 'lib/date_holidays/reader/locale.rb', line 15

def js_bridge
  @js_bridge
end

#regionObject (readonly)

Returns the value of attribute region.



15
16
17
# File 'lib/date_holidays/reader/locale.rb', line 15

def region
  @region
end

#stateObject (readonly)

Returns the value of attribute state.



15
16
17
# File 'lib/date_holidays/reader/locale.rb', line 15

def state
  @state
end

Instance Method Details

#holidays(year, language: :en, types: Set.new) ⇒ Object

Returns DateHolidays::Reader::Holiday instances.



36
37
38
39
40
# File 'lib/date_holidays/reader/locale.rb', line 36

def holidays(year, language: :en, types: Set.new)
  raw_holidays(year, language: language, types: types).map do |raw_holiday|
    Holiday.make(transform_raw_holiday(raw_holiday))
  end
end

#languagesObject



52
53
54
# File 'lib/date_holidays/reader/locale.rb', line 52

def languages
  js_bridge.extract(:languages, locale_selector)
end

#raw_holidays(year, language: :en, types: Set.new) ⇒ Object

Returns the holiday data as a hash exactly as returned from the date-holiday node module.



28
29
30
31
32
33
# File 'lib/date_holidays/reader/locale.rb', line 28

def raw_holidays(year, language: :en, types: Set.new)
  types = validate_and_convert_types_to_set(types)

  result = js_bridge.extract(:holidays, locale_selector, year, *lang_opts(language))
  type_filter(result, types)
end

#regions(language = :en) ⇒ Object

Raises:

  • (Caution::IllegalStateError)


46
47
48
49
50
# File 'lib/date_holidays/reader/locale.rb', line 46

def regions(language = :en)
  raise Caution::IllegalStateError, 'a state is required' unless state

  js_bridge.extract(:regions, locale_selector, *lang_opts(language))
end

#states(language = :en) ⇒ Object



42
43
44
# File 'lib/date_holidays/reader/locale.rb', line 42

def states(language = :en)
  js_bridge.extract(:states, locale_selector, *lang_opts(language))
end

#time_zonesObject



56
57
58
# File 'lib/date_holidays/reader/locale.rb', line 56

def time_zones
  js_bridge.extract(:time_zones, locale_selector)
end