Class: VerifiedHolidays::CabinetOffice

Inherits:
Object
  • Object
show all
Defined in:
lib/verified_holidays/cabinet_office.rb

Constant Summary collapse

CSV_URL =
'https://www8.cao.go.jp/chosei/shukujitsu/syukujitsu.csv'

Class Method Summary collapse

Class Method Details

.fetchObject



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/verified_holidays/cabinet_office.rb', line 12

def self.fetch
  uri = URI.parse(CSV_URL)
  response = Net::HTTP.get(uri)

  # Remove UTF-8 BOM bytes if present (before encoding conversion)
  raw = response.b
  raw = raw.byteslice(3..) if raw.byteslice(0, 3) == "\xEF\xBB\xBF".b

  # Convert from CP932 (Shift_JIS) to UTF-8
  utf8 = raw.force_encoding('CP932').encode('UTF-8')

  parse(utf8)
end

.parse(csv_string) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/verified_holidays/cabinet_office.rb', line 31

def self.parse(csv_string)
  holidays = {}
  CSV.parse(csv_string, headers: true) do |row|
    date_str = presence(row['国民の祝日・休日月日'])
    name = presence(row['国民の祝日・休日名称'])
    next if date_str.nil? || name.nil?

    holidays[Date.strptime(date_str, '%Y/%m/%d')] = name
  end
  holidays
end