Class: Metwit::Weather

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/metwit/weather.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#geoRGeo::Feature::Point (readonly)

Guaranteed. The geo location of the metag with GeoJSON format

Returns:

  • (RGeo::Feature::Point)

    geo location of the metag



26
27
28
# File 'lib/metwit/weather.rb', line 26

def geo
  @geo
end

#iconObject (readonly)

The icon URL



32
33
34
# File 'lib/metwit/weather.rb', line 32

def icon
  @icon
end

#locationObject (readonly)

The locality and country name for this geographical location



29
30
31
# File 'lib/metwit/weather.rb', line 29

def location
  @location
end

#sourcesObject (readonly)

The sources for the weather status



38
39
40
# File 'lib/metwit/weather.rb', line 38

def sources
  @sources
end

#sun_altitudeObject (readonly)

The altitude of the sun



35
36
37
# File 'lib/metwit/weather.rb', line 35

def sun_altitude
  @sun_altitude
end

#timestampTime (readonly)

Guaranteed. The metag timestamp.

Returns:

  • (Time)

    when the metag was created



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

def timestamp
  @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

Returns:

  • ({Symbol => String, Hash})

    weather data



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