Class: WeatherPinpointJp::Forecast

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(location, url) ⇒ Forecast

Returns a new instance of Forecast.



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/weather_pinpoint_jp.rb', line 73

def initialize(location, url)
  @location = location
  @url = url

  # load xml
  doc = REXML::Document.new(open(url))
  day = doc.elements['//weathernews/data/day']

  # get starttime
  year  = day.attributes['startYear'].to_i
  month = day.attributes['startMonth'].to_i
  date  = day.attributes['startDate'].to_i
  hour  = day.attributes['startHour'].to_i

  @start_time = Time.local(year, month, date, hour, 0, 0)

  # weather
  @weather = []
  day.elements.each('weather/hour') do |e|
    @weather << e.text.to_i
  end

  @weather_3h = create_weather_3h(@weather)

  # temperature
  @temperature = []
  day.elements.each('temperature/hour') do |e|
    @temperature << e.text.to_i
  end

  # precipitation
  @precipitation = []
  day.elements.each('precipitation/hour') do |e|
    @precipitation << e.text.to_i
  end
end

Instance Attribute Details

#locationObject (readonly)

Returns the value of attribute location.



151
152
153
# File 'lib/weather_pinpoint_jp.rb', line 151

def location
  @location
end

#precipitationObject (readonly)

Returns the value of attribute precipitation.



151
152
153
# File 'lib/weather_pinpoint_jp.rb', line 151

def precipitation
  @precipitation
end

#start_timeObject (readonly)

Returns the value of attribute start_time.



151
152
153
# File 'lib/weather_pinpoint_jp.rb', line 151

def start_time
  @start_time
end

#temperatureObject (readonly)

Returns the value of attribute temperature.



151
152
153
# File 'lib/weather_pinpoint_jp.rb', line 151

def temperature
  @temperature
end

#urlObject (readonly)

Returns the value of attribute url.



151
152
153
# File 'lib/weather_pinpoint_jp.rb', line 151

def url
  @url
end

#weatherObject (readonly)

Returns the value of attribute weather.



151
152
153
# File 'lib/weather_pinpoint_jp.rb', line 151

def weather
  @weather
end

#weather_3hObject (readonly)

Returns the value of attribute weather_3h.



151
152
153
# File 'lib/weather_pinpoint_jp.rb', line 151

def weather_3h
  @weather_3h
end

Instance Method Details

#code_rank(a) ⇒ Object

天気コードの強さを返す



111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/weather_pinpoint_jp.rb', line 111

def code_rank(a)
  rank = {
      0 => 0,
    100 => 1,
    550 => 2,
    200 => 3,
    300 => 4,
    400 => 5,
    850 => 6,
  }
  return rank[a] if rank.key?(a)

  return 10
end

#compare_code(a, b) ⇒ Object

どちらの天気コードが影響をおよぼすか?比較



127
128
129
# File 'lib/weather_pinpoint_jp.rb', line 127

def compare_code(a, b)
  return code_rank(a) <= code_rank(b)
end

#create_weather_3h(codes) ⇒ Object

3時間毎の天気コードに集約



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/weather_pinpoint_jp.rb', line 132

def create_weather_3h(codes)
  codes_3h = []
  count = 0;
  max_code = 0;
  codes.each {|c|
    if compare_code(max_code ,c)
      max_code = c
    end
    count += 1
    if count == 3
      codes_3h << max_code
      max_code = 0
      count = 0
    end
  }

  codes_3h
end