class Barometer::Noaa::Response::ForecastedWeather

Attributes

payload[R]
predictions[R]

Public Class Methods

new(payload) click to toggle source
# File lib/barometer/noaa/response/forecasted_weather.rb, line 5
def initialize(payload)
  @payload = payload
  @predictions = Barometer::Response::PredictionCollection.new
end

Public Instance Methods

parse() click to toggle source
# File lib/barometer/noaa/response/forecasted_weather.rb, line 10
def parse
  each_prediction do |prediction, index, shared_index|
    prediction.starts_at = start_times[index]
    prediction.ends_at = end_times[index]
    prediction.icon = icons[shared_index]
    prediction.condition = summaries[shared_index]
    prediction.pop = precipitations[index]
    prediction.high = [:imperial, high_temperatures[shared_index]]
    prediction.low = [:imperial, low_temperatures[shared_index]]
  end

  predictions
end

Private Instance Methods

each_forecast() { |index, shared_index(index)| ... } click to toggle source
# File lib/barometer/noaa/response/forecasted_weather.rb, line 32
def each_forecast
  (0...total_forecasts).each do |index|
    yield index, shared_index(index)
  end
end
each_prediction() { |prediction, index, shared_index| ... } click to toggle source
# File lib/barometer/noaa/response/forecasted_weather.rb, line 38
def each_prediction
  each_forecast do |index, shared_index|
    predictions.build do |prediction|
      yield prediction, index, shared_index
    end
  end
end
end_times() click to toggle source
# File lib/barometer/noaa/response/forecasted_weather.rb, line 58
def end_times
  @end_times ||= times['end_valid_time']
end
high_temperatures() click to toggle source
# File lib/barometer/noaa/response/forecasted_weather.rb, line 66
def high_temperatures
  @high_temperatures ||= temperatures.detect{|t| t['@type'] == 'maximum'}.fetch('value', [])
end
icons() click to toggle source
# File lib/barometer/noaa/response/forecasted_weather.rb, line 82
def icons
  @icons ||= payload.fetch('parameters', 'conditions_icon', 'icon_link').map{|l| l.match(/(\w*)\.[a-zA-Z]{3}$/)[1] }
end
low_temperatures() click to toggle source
# File lib/barometer/noaa/response/forecasted_weather.rb, line 70
def low_temperatures
  @low_temperatures ||= temperatures.detect{|t| t['@type'] == 'minimum'}.fetch('value', [])
end
precipitations() click to toggle source
# File lib/barometer/noaa/response/forecasted_weather.rb, line 74
def precipitations
  @precipitations ||= payload.fetch('parameters', 'probability_of_precipitation', 'value').map(&:to_i)
end
shared_index(index) click to toggle source
# File lib/barometer/noaa/response/forecasted_weather.rb, line 46
def shared_index(index)
  (index / 2).floor
end
start_times() click to toggle source
# File lib/barometer/noaa/response/forecasted_weather.rb, line 54
def start_times
  @start_times ||= times['start_valid_time']
end
summaries() click to toggle source
# File lib/barometer/noaa/response/forecasted_weather.rb, line 78
def summaries
  @summaries ||= payload.fetch('parameters', 'weather', 'weather_conditions').map{|c| c['@weather_summary'] }
end
temperatures() click to toggle source
# File lib/barometer/noaa/response/forecasted_weather.rb, line 62
def temperatures
  @temperatures ||= payload.fetch('parameters', 'temperature')
end
times() click to toggle source
# File lib/barometer/noaa/response/forecasted_weather.rb, line 50
def times
  @times ||= payload.fetch('time_layout').detect{|layout| layout['layout_key'] == 'k-p12h-n14-2'}
end
total_forecasts() click to toggle source
# File lib/barometer/noaa/response/forecasted_weather.rb, line 28
def total_forecasts
  high_temperatures.count * 2
end