Class: OpenMeteo::Entities::Forecast::Item

Inherits:
Object
  • Object
show all
Defined in:
lib/open_meteo/entities/forecast/item.rb

Overview

A forecast entity for a data item returned by OpenMeteo

Defined Under Namespace

Classes: UnknownWeatherCode

Constant Summary collapse

WMO_CODE_TO_SYMBOL_MAP =

The conversion map of weather_codes to a more descriptive symbol.

See “WMO Weather interpretation codes (WW)” on open-meteo.com/en/docs

{
  0 => :clear_sky,
  1 => :mainly_clear,
  2 => :partly_cloudy,
  3 => :overcast,
  45 => :fog,
  48 => :depositing_rime_fog,
  51 => :drizzle_light,
  53 => :drizzle_moderate,
  55 => :drizzle_dense,
  56 => :freezing_drizzle_light,
  57 => :freezing_drizzle_dense,
  61 => :rain_slight,
  63 => :rain_moderate,
  65 => :rain_heavy,
  66 => :freezing_rain_slight,
  67 => :freezing_rain_heavy,
  71 => :snow_slight,
  73 => :snow_moderate,
  75 => :snow_heavy,
  77 => :snow_grains,
  80 => :rain_showers_slight,
  81 => :rain_showers_moderate,
  82 => :rain_showers_violent,
  85 => :snow_showers_slight,
  86 => :snow_showers_heavy,
  95 => :thunderstorm_slight_or_moderate,
  96 => :thunderstorm_with_slight_hail,
  99 => :thunderstorm_with_heavy_hail,
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(json_data) ⇒ Item

Returns a new instance of Item.



48
49
50
51
52
53
# File 'lib/open_meteo/entities/forecast/item.rb', line 48

def initialize(json_data)
  @raw_json = json_data

  @attributes = json_data.keys
  @time = json_data["time"]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object (private)



67
68
69
70
71
72
73
# File 'lib/open_meteo/entities/forecast/item.rb', line 67

def method_missing(name, *args)
  if raw_json.key?(name.to_s)
    raw_json[name.to_s]
  else
    super
  end
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



46
47
48
# File 'lib/open_meteo/entities/forecast/item.rb', line 46

def attributes
  @attributes
end

#raw_jsonObject (readonly)

Returns the value of attribute raw_json.



46
47
48
# File 'lib/open_meteo/entities/forecast/item.rb', line 46

def raw_json
  @raw_json
end

#timeObject (readonly)

Returns the value of attribute time.



46
47
48
# File 'lib/open_meteo/entities/forecast/item.rb', line 46

def time
  @time
end

Instance Method Details

#weather_code_symbolObject

Provide a symbol for the weather_code.



59
60
61
62
63
# File 'lib/open_meteo/entities/forecast/item.rb', line 59

def weather_code_symbol
  return if weather_code.nil?

  WMO_CODE_TO_SYMBOL_MAP.fetch(weather_code) { raise UnknownWeatherCode, weather_code }
end