class MoreHoliday::Connector

Attributes

file_path[R]
state[R]
year[R]

Public Class Methods

new(state, file_path: nil, year: Date.today.year) click to toggle source
# File lib/more_holiday/connector.rb, line 11
def initialize state, file_path: nil, year: Date.today.year
  @state = state
  @file_path = file_path
  @year = year
end

Public Instance Methods

holidays() click to toggle source
# File lib/more_holiday/connector.rb, line 17
def holidays
  @holidays ||=
    if file_path.nil?
      connect
    else
      Importer.new(year).from_file(file_path)
    end
end
source() click to toggle source
# File lib/more_holiday/connector.rb, line 26
def source
  return "file" unless file_path.nil?
  service::SOURCE
end

Private Instance Methods

cache() click to toggle source
# File lib/more_holiday/connector.rb, line 77
def cache
  @cache ||= Cache::File.new(file_name: year.to_s, folder_path: File.join("holidays", country, state))
end
connect() click to toggle source
# File lib/more_holiday/connector.rb, line 41
def connect
  eval(cache.resolve Proc.new { Importer.new(year).from_stream(service::Connect.get(state)).to_s })
end
countries() click to toggle source
# File lib/more_holiday/connector.rb, line 54
def countries
  @countries ||= {
    "de" => [
      "Baden-Württemberg",
      "Bavaria",
      "Berlin",
      "Brandenburg",
      "Bremen",
      "Hamburg",
      "Hesse",
      "Lower Saxony",
      "Mecklenburg-Vorpommern",
      "North Rhine-Westphalia",
      "Rhine-Palatinate",
      "Saarland",
      "Saxony",
      "Saxony-Anhalt",
      "Schleswig-Holstein",
      "Thuringia"
    ]
  }
end
country() click to toggle source
# File lib/more_holiday/connector.rb, line 49
def country
  raise InvalidStateError, "#{state} not included in states list." unless state_valid?
  countries.each { |country, states| return country if states.include?(state) }
end
service() click to toggle source
# File lib/more_holiday/connector.rb, line 33
def service
  @service ||=
    case country
    when "de" then Ifeiertage
    else raise NotProvidedError, "No service for #{country} officals provided."
    end
end
state_valid?() click to toggle source
# File lib/more_holiday/connector.rb, line 45
def state_valid?
  countries.values.flatten.include?(state)
end