Class: MeteofranceApi::Client

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

Instance Method Summary collapse

Constructor Details

#initialize(token = MeteofranceApi::Constants::API_TOKENS.first) ⇒ Client

Returns a new instance of Client.



7
8
9
# File 'lib/meteofrance_api/client.rb', line 7

def initialize(token = MeteofranceApi::Constants::API_TOKENS.first)
  @token = token
end

Instance Method Details

#connectionObject



11
12
13
14
15
16
# File 'lib/meteofrance_api/client.rb', line 11

def connection
  @conn ||= Faraday.new(
    url: MeteofranceApi::Constants::API_URL,
    params: {token: @token },
  )
end

#get_forecast(latitude, longitude, language = nil) ⇒ Object

Retrieve the weather forecast for a given GPS location.

Results can be fetched in french or english according to the language parameter.

Args:

latitude: Latitude in degree of the GPS point corresponding to the weather
    forecast.
longitude: Longitude in degree of the GPS point corresponding to the weather
    forecast.
language: Optional; If language is equal "fr" (default value) results will
    be in French. All other value will give results in English.

Returns:

A Forecast intance representing the hourly and daily weather forecast.


72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/meteofrance_api/client.rb', line 72

def get_forecast(
  latitude,
  longitude,
  language = nil
)
  language ||= "fr"

  # Send the API request
  resp = connection.get("/v2/forecast", {
    lat: latitude,
    lon: longitude,
    lang: language
  })


  return MeteofranceApi::Forecast.new(JSON.parse(resp.body))
end

#get_forecast_for_place(place, language = nil) ⇒ Object

Args:

place: Place class instance corresponding to a location.
language: Optional; If language is equal "fr" (default value) results will
    be in French. All other value will give results in English.

Returns:

A Forecast intance representing the hourly and daily weather forecast.


102
103
104
105
106
107
108
109
# File 'lib/meteofrance_api/client.rb', line 102

def get_forecast_for_place(
  place,
  language = nil
)
  language ||= "fr"

  return get_forecast(place.latitude, place.longitude, language)
end

#get_picture_of_the_day(domain = "france") ⇒ Object

Retrieve the picture of the day image URL & description.

Args:
    domain: could be `france`

Returns:
    An array of 2 objects: the image url, and the description


254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/meteofrance_api/client.rb', line 254

def get_picture_of_the_day(domain = "france")
  params = {
    domain: domain,
    report_type: "observation",
    report_subtype: "image du jour",
  }

  resp = connection.get(
    "/v2/report",
    params.merge(format: "txt")
  )
  image_description = resp.body

  image_url = [
    "#{MeteofranceApi::Constants::API_URL}/v2/report",
    "?",
    URI.encode_www_form(params.merge(format: "jpg", token: @token))
  ].join

  return [image_url, image_description]
end

#get_rain(latitude, longitude, language = nil) ⇒ Object

Retrieve the next 1 hour rain forecast for a given GPS the location.

Results can be fetched in french or english according to the language parameter.

Args:

latitude: Latitude in degree of the GPS point corresponding to the rain
    forecast.
longitude: Longitude in degree of the GPS point corresponding to the rain
    forecast.
language: Optional; If language is equal "fr" (default value) results will
    be in French. All other value will give results in English.

Returns:

A Rain instance representing the next hour rain forecast.


126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/meteofrance_api/client.rb', line 126

def get_rain(latitude, longitude, language = nil)
  # TODO: add protection if no rain forecast for this position

  language ||= "fr"

  # Send the API request
  resp = connection.get("/v2/rain", {
    lat: latitude,
    lon: longitude,
    lang: language
  })

  return MeteofranceApi::Rain.new(JSON.parse(resp.body))
end

#get_warning_current_phenomenons(domain, depth = 0, with_costal_bulletin = false) ⇒ Object

Return the current weather phenomenons (or alerts) for a given domain.

Args:

domain: could be `france` or any metropolitan France department numbers on
    two digits. For some departments you can access an additional bulletin
    for coastal phenomenons. To access it add `10` after the domain id
    (example: `1310`).
depth: Optional; To be used with domain = 'france'. With depth = 0 the
    results will show only natinal sum up of the weather alerts. If
    depth = 1, you will have in addition, the bulletin for all metropolitan
    France department and Andorre
with_costal_bulletin: Optional; If set to True (default is False), you can
    get the basic bulletin and coastal bulletin merged.

Returns:

A warning.CurrentPhenomenons instance representing the weather alert
bulletin.


159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/meteofrance_api/client.rb', line 159

def get_warning_current_phenomenons(domain, depth = 0, with_costal_bulletin = false)
  # Send the API request
  resp = connection.get("/v2/warning/currentphenomenons", {
    domain: domain,
    depth: depth
  })

  # Create object with API response
  phenomenons = MeteofranceApi::Warning::Current.new(JSON.parse(resp.body))


  # if user ask to have the coastal bulletin merged
  if with_costal_bulletin
    if MeteofranceApi::Constants::COASTAL_DEPARTMENTS.include?(domain)
      resp = connection.get("/v2/warning/currentphenomenons", {
        domain: domain + "10",
      })
      coastal_phenomenons = MeteofranceApi::Warning::Current.new(JSON.parse(resp.body))

      phenomenons.merge_with_coastal_phenomenons!(coastal_phenomenons)
    end
  end

  return phenomenons
end

#get_warning_full(domain, with_costal_bulletin = false) ⇒ Object

Retrieve a complete bulletin of the weather phenomenons for a given domain.

For a given domain we can access the maximum alert, a timelaps of the alert evolution for the next 24 hours, a list of alerts and other metadatas.

Args:

domain: could be `france` or any metropolitan France department numbers on
    two digits. For some departments you can access an additional bulletin
    for coastal phenomenons. To access it add `10` after the domain id
    (example: `1310`).
with_costal_bulletin: Optional; If set to True (default is False), you can
    get the basic bulletin and coastal bulletin merged.

Returns:

A warning.Full instance representing the complete weather alert bulletin.


201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/meteofrance_api/client.rb', line 201

def get_warning_full(domain, with_costal_bulletin = false)
  # TODO: add formatDate parameter

  # Send the API request
  resp = connection.get("/v2/warning/full", {
    domain: domain,
  })

  # Create object with API response
  full_phenomenons = MeteofranceApi::Warning::Full.new(JSON.parse(resp.body))

  # if user ask to have the coastal bulletin merged
  if with_costal_bulletin
    if MeteofranceApi::Constants::COASTAL_DEPARTMENTS.include?(domain)
      resp = connection.get("/v2/warning/full", {
        domain: domain + 10,
      })
      coastal_full_phenomenons = MeteofranceApi::Warning::Full.new(JSON.parse(resp.body))

      full_phenomenons.merge_with_coastal_phenomenons!(coastal_full_phenomenons)
    end
  end

  return full_phenomenons
end

#get_warning_thumbnail(domain = "france") ⇒ Object

Retrieve the thumbnail URL of the weather phenomenons or alerts map.

Args:
    domain: could be `france` or any metropolitan France department numbers on
        two digits.

Returns:
    The URL of the thumbnail representing the weather alert status.


236
237
238
239
240
241
242
243
# File 'lib/meteofrance_api/client.rb', line 236

def get_warning_thumbnail(domain = "france")
  # Return directly the URL of the gif image
  [
    "#{MeteofranceApi::Constants::API_URL}/warning/thumbnail",
    "?",
    URI.encode_www_form({ domain: domain, token: @token }),
  ].join
end

#search_places(search_query, latitude: nil, longitude: nil) ⇒ Object

Returns:

A list of places (Place instance) corresponding to the query.


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/meteofrance_api/client.rb', line 34

def search_places(
    search_query,
    latitude: nil,
    longitude: nil
)
  # Construct the list of the GET parameters
  params = {"q": search_query}
  if !!latitude
    params["lat"] = latitude
  end
  if !!longitude
    params["lon"] = longitude
  end

  # Send the API resuest
  resp = connection.get("/v2/places", params)
  data = JSON.parse(resp.body)

  places = data.map {|datum| MeteofranceApi::Place.new(datum)}

  places
end