Class: MeteofranceApi::Weather

Inherits:
Object
  • Object
show all
Defined in:
lib/meteofrance_api/models/weather.rb

Defined Under Namespace

Classes: DailyForecast, Forecast, HazardForecast

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Weather

Returns a new instance of Weather.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/meteofrance_api/models/weather.rb', line 30

def initialize(data)
  @updated_on = Time.at(data["updated_time"])

  @position = data["geometry"]["coordinates"]
  @altitude = data["properties"]["altitude"]
  @name = data["properties"]["name"]
  @country = data["properties"]["country"]
  @timezone = data["properties"]["timezone"]
  @insee = data["properties"]["insee"]&.to_i
  @french_department_code = data["properties"]["french_department_code"]&.to_i

  @bulletin_cote = data["properties"]["bulletin_cote"]

  @daily_forecasts = data.dig("daily_forecast", []).map {|d| MeteofranceApi::Weather::DailyForecast.new(d)}
  @forecasts = data.dig("forecast", []).map {|d| MeteofranceApi::Weather::Forecast.new(d)}
  @hazard_forecasts = data.dig("probability_forecast", []).map {|d| MeteofranceApi::Weather::HazardForecast.new(d)}
end

Instance Attribute Details

#altitudeObject (readonly)

Altitude of the position (in meter)



8
9
10
# File 'lib/meteofrance_api/models/weather.rb', line 8

def altitude
  @altitude
end

#bulletin_coteObject (readonly)

Returns the value of attribute bulletin_cote.



17
18
19
# File 'lib/meteofrance_api/models/weather.rb', line 17

def bulletin_cote
  @bulletin_cote
end

#countryObject (readonly)

Country of the position



12
13
14
# File 'lib/meteofrance_api/models/weather.rb', line 12

def country
  @country
end

#daily_forecastsObject (readonly)

daily forecast for the following days. A list of Hash to describe the daily forecast for the next 15 days.



21
22
23
# File 'lib/meteofrance_api/models/weather.rb', line 21

def daily_forecasts
  @daily_forecasts
end

#forecastsObject (readonly)

hourly forecast. A list of Hash to describe the hourly forecast for the next day



24
25
26
# File 'lib/meteofrance_api/models/weather.rb', line 24

def forecasts
  @forecasts
end

#french_department_codeObject (readonly)

Returns the value of attribute french_department_code.



15
16
17
# File 'lib/meteofrance_api/models/weather.rb', line 15

def french_department_code
  @french_department_code
end

#hazard_forecastsObject (readonly)

wheather event forecast. A list of object to describe the event probability forecast (rain, snow, freezing) for next 10 days.



27
28
29
# File 'lib/meteofrance_api/models/weather.rb', line 27

def hazard_forecasts
  @hazard_forecasts
end

#inseeObject (readonly)

Returns the value of attribute insee.



14
15
16
# File 'lib/meteofrance_api/models/weather.rb', line 14

def insee
  @insee
end

#nameObject (readonly)

Name of the position



10
11
12
# File 'lib/meteofrance_api/models/weather.rb', line 10

def name
  @name
end

#positionObject (readonly)

position information of the forecast. An Array [longitude, latitude].



6
7
8
# File 'lib/meteofrance_api/models/weather.rb', line 6

def position
  @position
end

#timezoneObject (readonly)

Returns the value of attribute timezone.



13
14
15
# File 'lib/meteofrance_api/models/weather.rb', line 13

def timezone
  @timezone
end

#updated_atObject (readonly)

update Time of the forecast.



3
4
5
# File 'lib/meteofrance_api/models/weather.rb', line 3

def updated_at
  @updated_at
end

Instance Method Details

#current_forecastObject

Return the forecast of the current hour. A Hash corresponding to the hourly forecast for the current hour



66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/meteofrance_api/models/weather.rb', line 66

def current_forecast
  # Get the timestamp for the current hour.
  current_hour = Time.now.utc.to_a
  current_hour[0] = 0
  current_hour[1] = 0
  current_hour_timestamp = Time.utc.new(*current_hour).to_i

  # create a Hash using timestamp as keys
  forecasts_by_datetime = self.forecasts.map {|f| [f.time.to_i, f]}.to_h

  # Return the forecast corresponding to the timestamp of the current hour if
  # exists.
  forecasts_by_datetime[current_hour_timestamp]
end

#nearest_forecastObject

Return the nearest hourly forecast. A Hash corresponding to the nearest hourly forecast.



54
55
56
57
58
59
60
61
62
63
# File 'lib/meteofrance_api/models/weather.rb', line 54

def nearest_forecast
  # get timestamp for current time
  now_timestamp = Time.now

  # sort list of forecast by distance between current timestamp and
  # forecast timestamp
  sorted_forecasts = self.forecasts.sort_by {|f| (f.time - now_timestamp).abs }

  sorted_forecasts.first
end

#to_locale_time(time) ⇒ Object

Convert time in the forecast location timezone



82
83
84
# File 'lib/meteofrance_api/models/weather.rb', line 82

def to_locale_time(time)
  time.in_time_zone(timezone)
end

#today_forecastObject

Return the forecast for today. A Hash corresponding to the daily forecast for the current day



49
50
51
# File 'lib/meteofrance_api/models/weather.rb', line 49

def today_forecast
  self.daily_forecasts.first
end