Class: JMACode::AreaInformationCity

Inherits:
Struct
  • Object
show all
Defined in:
lib/jma_code/area_information_city.rb

Constant Summary collapse

CSV_ROW_SEP =
"\r\n"
NUM_HEADER_ROWS =
3
HEADERS =
%i(
  code
  long_name
  name_used_by_weather
  name_phonetic_used_by_weather
  area_forecast_local_m_code
  used_by_weather
  used_by_tornado
  used_by_storm_surge
  used_by_high_wave
  used_by_landslide
  used_by_flood
  name_used_by_earthquake
  name_phonetic_used_by_earthquake
  name_used_by_volcano
  name_phonetic_used_by_volcano
  name_used_by_uv
  name_phonetic_used_by_uv
  name_used_by_rainstorm
  name_phonetic_used_by_rainstorm
  area_forecast_local_e_code
)

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.dataObject

Returns the value of attribute data.



38
39
40
# File 'lib/jma_code/area_information_city.rb', line 38

def data
  @data
end

Instance Attribute Details

#alt_nameObject

Returns the value of attribute alt_name

Returns:

  • (Object)

    the current value of alt_name



5
6
7
# File 'lib/jma_code/area_information_city.rb', line 5

def alt_name
  @alt_name
end

#alt_name_phoneticObject

Returns the value of attribute alt_name_phonetic

Returns:

  • (Object)

    the current value of alt_name_phonetic



5
6
7
# File 'lib/jma_code/area_information_city.rb', line 5

def alt_name_phonetic
  @alt_name_phonetic
end

#area_forecast_local_e_codeObject

Returns the value of attribute area_forecast_local_e_code

Returns:

  • (Object)

    the current value of area_forecast_local_e_code



5
6
7
# File 'lib/jma_code/area_information_city.rb', line 5

def area_forecast_local_e_code
  @area_forecast_local_e_code
end

#area_forecast_local_m_codeObject

Returns the value of attribute area_forecast_local_m_code

Returns:

  • (Object)

    the current value of area_forecast_local_m_code



5
6
7
# File 'lib/jma_code/area_information_city.rb', line 5

def area_forecast_local_m_code
  @area_forecast_local_m_code
end

#codeObject

Returns the value of attribute code

Returns:

  • (Object)

    the current value of code



5
6
7
# File 'lib/jma_code/area_information_city.rb', line 5

def code
  @code
end

#long_nameObject

Returns the value of attribute long_name

Returns:

  • (Object)

    the current value of long_name



5
6
7
# File 'lib/jma_code/area_information_city.rb', line 5

def long_name
  @long_name
end

#used_byObject

Returns the value of attribute used_by

Returns:

  • (Object)

    the current value of used_by



5
6
7
# File 'lib/jma_code/area_information_city.rb', line 5

def used_by
  @used_by
end

Class Method Details

.build_by_csv_row(row) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/jma_code/area_information_city.rb', line 60

def build_by_csv_row(row)
  alt_name, alt_name_phonetic = if row[:name_used_by_weather].present?
    [row[:name_used_by_weather], row[:name_phonetic_used_by_weather]]
  elsif row[:name_used_by_earthquake].present?
    [row[:name_used_by_earthquake], row[:name_phonetic_used_by_earthquake]]
  elsif row[:name_used_by_volcano].present?
    [row[:name_used_by_volcano], row[:name_phonetic_used_by_volcano]]
  elsif row[:name_used_by_uv].present?
    [row[:name_used_by_uv], row[:name_phonetic_used_by_uv]]
  elsif row[:name_used_by_rainstorm].present?
    [row[:name_used_by_rainstorm], row[:name_phonetic_used_by_rainstorm]]
  else
    []
  end

  new(
    code: row[:code], 
    long_name: row[:long_name], 
    alt_name: alt_name,
    alt_name_phonetic: alt_name_phonetic,
    area_forecast_local_m_code: row[:area_forecast_local_m_code],
    area_forecast_local_e_code: row[:area_forecast_local_e_code],
    used_by: [
      row[:used_by_weather] == '1' ? :weather : nil,
      row[:used_by_tornado] == '1' ? :tornado : nil,
      row[:used_by_storm_surge] == '1' ? :storm_surge : nil,
      row[:used_by_high_wave] == '1' ? :high_wave : nil,
      row[:used_by_landslide] == '1' ? :landslide : nil,
      row[:used_by_flood] == '1' ? :flood : nil,
      row[:name_used_by_earthquake].present? ? :earthquake : nil,
      row[:name_used_by_volcano].present? ? :volcano : nil,
      row[:name_used_by_uv].present? ? :uv : nil,
      row[:name_used_by_rainstorm].present? ? :rainstorm : nil,
    ].compact
  )
end

.getObject



56
57
58
# File 'lib/jma_code/area_information_city.rb', line 56

def get
  @data ||= load
end

.load(**args) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/jma_code/area_information_city.rb', line 48

def load(**args)
  load_csv(**args) do |csv|
    csv.drop(NUM_HEADER_ROWS).map do |row|
      build_by_csv_row(row)
    end
  end
end

.load_csv(version: "20240216-completed") ⇒ Object



40
41
42
43
44
45
46
# File 'lib/jma_code/area_information_city.rb', line 40

def load_csv(version: "20240216-completed")
  path = File.join(File.dirname(__FILE__), "../../data/#{version}_AreaInformationCity-AreaForecastLocalM/AreaInformationCity.csv")
  File.open(path) do |f|
    csv = CSV.new(f, headers: HEADERS, row_sep: CSV_ROW_SEP)
    yield(csv)
  end
end

Instance Method Details

#area_forecast_local_eObject



114
115
116
# File 'lib/jma_code/area_information_city.rb', line 114

def area_forecast_local_e
  @area_forecast_local_e ||= AreaForecastLocalE.get.find{|x| x.code == area_forecast_local_e_code}
end

#area_forecast_local_mObject



110
111
112
# File 'lib/jma_code/area_information_city.rb', line 110

def area_forecast_local_m
  @area_forecast_local_m ||= AreaForecastLocalM.get.find{|x| x.code == area_forecast_local_m_code}
end

#child_of?(area_or_city) ⇒ Boolean

Returns:

  • (Boolean)


118
119
120
# File 'lib/jma_code/area_information_city.rb', line 118

def child_of?(area_or_city)
  area_forecast_local_m_code == area_or_city.code
end

#nameObject



98
99
100
# File 'lib/jma_code/area_information_city.rb', line 98

def name
  alt_name.presence || long_name
end

#name_phoneticObject



102
103
104
# File 'lib/jma_code/area_information_city.rb', line 102

def name_phonetic
  alt_name_phonetic
end

#prefecture_codeObject



106
107
108
# File 'lib/jma_code/area_information_city.rb', line 106

def prefecture_code
  @prefecture_code ||= code[0, 2]
end

#to_csv_rowObject



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/jma_code/area_information_city.rb', line 122

def to_csv_row
  HEADERS.map do |k|
    if respond_to?(k)
      public_send(k)
    else
      if k.to_s.start_with?("used_by_")
        x = k.to_s.sub('used_by_', '').to_sym
        used_by.include?(x) ? '1' : nil
      elsif k.to_s.start_with?("name_used_by_")
        x = k.to_s.sub('name_used_by_', '').to_sym
        used_by.include?(x) ? alt_name : nil
      elsif k.to_s.start_with?("name_phonetic_used_by_")
        x = k.to_s.sub('name_phonetic_used_by_', '').to_sym
        used_by.include?(x) ? alt_name_phonetic : nil
      end
    end
  end
end

#to_hObject



141
142
143
144
145
146
147
148
149
# File 'lib/jma_code/area_information_city.rb', line 141

def to_h
  {
    code: code,
    name: name,
    name_phonetic: name_phonetic,
    area_forecast_local_m_code: area_forecast_local_m_code,
    used_by: used_by,
  }
end