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_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
)

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.



35
36
37
# File 'lib/jma_code/area_information_city.rb', line 35

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_codeObject

Returns the value of attribute area_forecast_local_code

Returns:

  • (Object)

    the current value of area_forecast_local_code



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

def area_forecast_local_code
  @area_forecast_local_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



57
58
59
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
# File 'lib/jma_code/area_information_city.rb', line 57

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_code: row[:area_forecast_local_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



53
54
55
# File 'lib/jma_code/area_information_city.rb', line 53

def get
  @data ||= load
end

.load(**args) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/jma_code/area_information_city.rb', line 45

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") ⇒ Object



37
38
39
40
41
42
43
# File 'lib/jma_code/area_information_city.rb', line 37

def load_csv(version: "20240216")
  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_localObject



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

def area_forecast_local
  @area_forecast_local ||= AreaForecastLocal.get.find{|x| x.code == area_forecast_local_code}
end

#child_of?(area_or_city) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#nameObject



94
95
96
# File 'lib/jma_code/area_information_city.rb', line 94

def name
  alt_name.presence || long_name
end

#name_phoneticObject



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

def name_phonetic
  alt_name_phonetic
end

#prefecture_codeObject



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

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

#to_hObject



114
115
116
117
118
119
120
121
122
# File 'lib/jma_code/area_information_city.rb', line 114

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