Class: JMACode::AreaInformationCity
- Inherits:
-
Struct
- Object
- Struct
- JMACode::AreaInformationCity
- 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
-
.data ⇒ Object
Returns the value of attribute data.
Instance Attribute Summary collapse
-
#alt_name ⇒ Object
Returns the value of attribute alt_name.
-
#alt_name_phonetic ⇒ Object
Returns the value of attribute alt_name_phonetic.
-
#area_forecast_local_e_code ⇒ Object
Returns the value of attribute area_forecast_local_e_code.
-
#area_forecast_local_m_code ⇒ Object
Returns the value of attribute area_forecast_local_m_code.
-
#code ⇒ Object
Returns the value of attribute code.
-
#long_name ⇒ Object
Returns the value of attribute long_name.
-
#used_by ⇒ Object
Returns the value of attribute used_by.
Class Method Summary collapse
- .build_by_csv_row(row) ⇒ Object
- .get ⇒ Object
- .load(**args) ⇒ Object
- .load_csv(version: "20240216-completed") ⇒ Object
Instance Method Summary collapse
- #area_forecast_local_e ⇒ Object
- #area_forecast_local_m ⇒ Object
- #child_of?(area_or_city) ⇒ Boolean
- #name ⇒ Object
- #name_phonetic ⇒ Object
- #prefecture_code ⇒ Object
- #to_csv_row ⇒ Object
- #to_h ⇒ Object
Class Attribute Details
.data ⇒ Object
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_name ⇒ Object
Returns the value of attribute alt_name
5 6 7 |
# File 'lib/jma_code/area_information_city.rb', line 5 def alt_name @alt_name end |
#alt_name_phonetic ⇒ Object
Returns the value of attribute 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_code ⇒ Object
Returns the value of attribute 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_code ⇒ Object
Returns the value of attribute 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 |
#code ⇒ Object
Returns the value of attribute code
5 6 7 |
# File 'lib/jma_code/area_information_city.rb', line 5 def code @code end |
#long_name ⇒ Object
Returns the value of attribute long_name
5 6 7 |
# File 'lib/jma_code/area_information_city.rb', line 5 def long_name @long_name end |
#used_by ⇒ Object
Returns the value of attribute 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 |
.get ⇒ Object
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_e ⇒ Object
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_m ⇒ Object
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
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 |
#name ⇒ Object
98 99 100 |
# File 'lib/jma_code/area_information_city.rb', line 98 def name alt_name.presence || long_name end |
#name_phonetic ⇒ Object
102 103 104 |
# File 'lib/jma_code/area_information_city.rb', line 102 def name_phonetic alt_name_phonetic end |
#prefecture_code ⇒ Object
106 107 108 |
# File 'lib/jma_code/area_information_city.rb', line 106 def prefecture_code @prefecture_code ||= code[0, 2] end |
#to_csv_row ⇒ Object
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_h ⇒ Object
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 |