Class: MyWeatherForecast
- Inherits:
-
Object
- Object
- MyWeatherForecast
- Defined in:
- lib/myweatherforecast.rb
Overview
This gem is a wrapper of the forecast_io gem SI explained: en.wikipedia.org/wiki/SI_derived_unit
Defined Under Namespace
Instance Attribute Summary collapse
-
#coordinates ⇒ Object
readonly
Returns the value of attribute coordinates.
Instance Method Summary collapse
-
#days ⇒ Object
e.g.
- #friday ⇒ Object (also: #fri)
- #hours ⇒ Object
-
#initialize(*location, api_key: nil, units: :auto, timeout: 3, symbols: true) ⇒ MyWeatherForecast
constructor
A new instance of MyWeatherForecast.
- #monday ⇒ Object (also: #mon)
- #now ⇒ Object (also: #currently)
- #saturday ⇒ Object (also: #sat)
- #sunday ⇒ Object (also: #sun)
- #thursday ⇒ Object (also: #thu)
- #today ⇒ Object (also: #this)
- #tomorrow ⇒ Object
- #tonight ⇒ Object
- #tuesday ⇒ Object (also: #tue)
- #wednesday ⇒ Object (also: #wed)
Constructor Details
#initialize(*location, api_key: nil, units: :auto, timeout: 3, symbols: true) ⇒ MyWeatherForecast
Returns a new instance of MyWeatherForecast.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/myweatherforecast.rb', line 20 def initialize(*location, api_key: nil, units: :auto, timeout: 3, \ symbols: true) lat, lon = if location[0].is_a? Array then location[0] elsif location.any? results = Geocoder.search(location.first) return puts 'location not found' unless results.any? results[0].coordinates else h = JSON.parse open('http://jsonip.com/').read Geocoder.configure(timeout: timeout) results = Geocoder.search(h['ip']) return puts 'could not determine location from IP address' unless \ results.any? results[0].coordinates end ForecastIO.api_key = api_key params = { units: units.to_s } @forecast = ForecastIO.forecast(lat, lon, params: params) autounits = @forecast['flags']['units'] @tlabel = if symbols then autounits == 'us' ? '°F' : '°C' else autounits == 'us' ? 'degrees Farenheit' : 'degrees Celcius' end @coordinates = [lat, lon] @symbols = symbols end |
Instance Attribute Details
#coordinates ⇒ Object (readonly)
Returns the value of attribute coordinates.
18 19 20 |
# File 'lib/myweatherforecast.rb', line 18 def coordinates @coordinates end |
Instance Method Details
#days ⇒ Object
e.g. require ‘myweatherforecast’
w = MyWeatherForecast.new api_key: ‘465xxxxxxxxxxxxxx76ea01cbff4’ puts w.days.take 3
Fri: 8° - 14°, Mostly cloudy throughout the day. Sat: 6° - 13°, Light rain until afternoon. Sun: 5° - 12°, Mostly cloudy throughout the day.
287 288 289 |
# File 'lib/myweatherforecast.rb', line 287 def days() (@forecast['daily']['data'].length).times.map {|n| Daily.new(@forecast, @tlabel, n) } end |
#friday ⇒ Object Also known as: fri
322 |
# File 'lib/myweatherforecast.rb', line 322 def friday() day :friday end |
#hours ⇒ Object
291 292 293 294 295 296 |
# File 'lib/myweatherforecast.rb', line 291 def hours() len = @forecast['hourly']['data'].length len.times.map {|i| Hourly.new @forecast, @tlabel, i} end |
#monday ⇒ Object Also known as: mon
318 |
# File 'lib/myweatherforecast.rb', line 318 def monday() day :monday end |
#now ⇒ Object Also known as: currently
298 299 300 |
# File 'lib/myweatherforecast.rb', line 298 def now() Hourly.new(@forecast, @tlabel) end |
#saturday ⇒ Object Also known as: sat
323 |
# File 'lib/myweatherforecast.rb', line 323 def saturday() day :saturday end |
#sunday ⇒ Object Also known as: sun
324 |
# File 'lib/myweatherforecast.rb', line 324 def sunday() day :sunday end |
#thursday ⇒ Object Also known as: thu
321 |
# File 'lib/myweatherforecast.rb', line 321 def thursday() day :thursday end |
#today ⇒ Object Also known as: this
302 303 304 |
# File 'lib/myweatherforecast.rb', line 302 def today() Daily.new(@forecast, @tlabel) end |
#tomorrow ⇒ Object
314 315 316 |
# File 'lib/myweatherforecast.rb', line 314 def tomorrow() Daily.new(@forecast, @tlabel, 1) end |
#tonight ⇒ Object
308 309 310 |
# File 'lib/myweatherforecast.rb', line 308 def tonight() Daily.new(@forecast, @tlabel).night end |
#tuesday ⇒ Object Also known as: tue
319 |
# File 'lib/myweatherforecast.rb', line 319 def tuesday() day :tuesday end |
#wednesday ⇒ Object Also known as: wed
320 |
# File 'lib/myweatherforecast.rb', line 320 def wednesday() day :wednesday end |