Class: Metwit::Weather
- Inherits:
-
Object
- Object
- Metwit::Weather
- Includes:
- HTTParty
- Defined in:
- lib/metwit/weather.rb
Instance Attribute Summary collapse
-
#geo ⇒ RGeo::Feature::Point
readonly
Guaranteed.
-
#icon ⇒ Object
readonly
The icon URL.
-
#location ⇒ Object
readonly
The locality and country name for this geographical location.
-
#sources ⇒ Object
readonly
The sources for the weather status.
-
#sun_altitude ⇒ Object
readonly
The altitude of the sun.
-
#timestamp ⇒ Time
readonly
Guaranteed.
-
#weather ⇒ {Symbol => String, Hash}
readonly
Guaranteed.
Class Method Summary collapse
- .authenticated(opts) ⇒ Object
- .from_json(response) ⇒ Object
-
.in_location(lat, lng) ⇒ Object
An array with current weather and weather forecast.
Instance Method Summary collapse
-
#initialize(args = {}) ⇒ Weather
constructor
A new instance of Weather.
Constructor Details
#initialize(args = {}) ⇒ Weather
Returns a new instance of Weather.
40 41 42 43 44 |
# File 'lib/metwit/weather.rb', line 40 def initialize(args={}) args.each do |key, value| instance_variable_set("@#{key}", value) end end |
Instance Attribute Details
#geo ⇒ RGeo::Feature::Point (readonly)
Guaranteed. The geo location of the metag with GeoJSON format
26 27 28 |
# File 'lib/metwit/weather.rb', line 26 def geo @geo end |
#icon ⇒ Object (readonly)
The icon URL
32 33 34 |
# File 'lib/metwit/weather.rb', line 32 def icon @icon end |
#location ⇒ Object (readonly)
The locality and country name for this geographical location
29 30 31 |
# File 'lib/metwit/weather.rb', line 29 def location @location end |
#sources ⇒ Object (readonly)
The sources for the weather status
38 39 40 |
# File 'lib/metwit/weather.rb', line 38 def sources @sources end |
#sun_altitude ⇒ Object (readonly)
The altitude of the sun
35 36 37 |
# File 'lib/metwit/weather.rb', line 35 def sun_altitude @sun_altitude end |
#timestamp ⇒ Time (readonly)
Guaranteed. The metag timestamp.
21 22 23 |
# File 'lib/metwit/weather.rb', line 21 def @timestamp end |
#weather ⇒ {Symbol => String, Hash} (readonly)
Guaranteed. Weather is an Hash with two keys: :status and :details Valid :status values are: :clear, :rainy, :stormy, :snowy, :partly_cloudy, :cloudy, :hailing, :heavy_seas, :calm_seas, :foggy, :snow_flurries, :windy, :partly_cloudy, :uknown
16 17 18 |
# File 'lib/metwit/weather.rb', line 16 def weather @weather end |
Class Method Details
.authenticated(opts) ⇒ Object
68 69 70 |
# File 'lib/metwit/weather.rb', line 68 def authenticated(opts) opts.deep_merge(:headers => {'Authorization' => "Basic #{Metwit.access_token}"}) if Metwit.logged? end |
.from_json(response) ⇒ Object
59 60 61 62 63 64 65 66 |
# File 'lib/metwit/weather.rb', line 59 def from_json(response) self.new(timestamp: Time.parse(response['timestamp']), weather: response['weather'], geo: RGeo::GeoJSON.decode(response['geo']), sun_altitude: response['sun_altitude'], sources: response['sources'], location: response['location']) end |
.in_location(lat, lng) ⇒ Object
An array with current weather and weather forecast
49 50 51 52 53 54 55 56 57 |
# File 'lib/metwit/weather.rb', line 49 def in_location(lat, lng) response = get("/?location_lat=#{lat}&location_lng=#{lng}", authenticated({})) raise AccessTokenExpiredError if response.code == 401 raise "api error" unless response.code == 200 response['objects'].map {|weather_json| self.from_json(weather_json)} rescue AccessTokenExpiredError => e Metwit.refresh_access_token in_location(lat, lng) end |