Class: MyWeatherForecast

Inherits:
Object
  • Object
show all
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

Classes: Daily, Hourly

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#coordinatesObject (readonly)

Returns the value of attribute coordinates.



18
19
20
# File 'lib/myweatherforecast.rb', line 18

def coordinates
  @coordinates
end

Instance Method Details

#daysObject

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

#fridayObject Also known as: fri



322
# File 'lib/myweatherforecast.rb', line 322

def friday()    day :friday    end

#hoursObject



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

#mondayObject Also known as: mon



318
# File 'lib/myweatherforecast.rb', line 318

def monday()    day :monday    end

#nowObject Also known as: currently



298
299
300
# File 'lib/myweatherforecast.rb', line 298

def now()
  Hourly.new(@forecast, @tlabel)
end

#saturdayObject Also known as: sat



323
# File 'lib/myweatherforecast.rb', line 323

def saturday()  day :saturday  end

#sundayObject Also known as: sun



324
# File 'lib/myweatherforecast.rb', line 324

def sunday()    day :sunday    end

#thursdayObject Also known as: thu



321
# File 'lib/myweatherforecast.rb', line 321

def thursday()  day :thursday  end

#todayObject Also known as: this



302
303
304
# File 'lib/myweatherforecast.rb', line 302

def today()
  Daily.new(@forecast, @tlabel)
end

#tomorrowObject



314
315
316
# File 'lib/myweatherforecast.rb', line 314

def tomorrow()    
  Daily.new(@forecast, @tlabel, 1)
end

#tonightObject



308
309
310
# File 'lib/myweatherforecast.rb', line 308

def tonight()
  Daily.new(@forecast, @tlabel).night
end

#tuesdayObject Also known as: tue



319
# File 'lib/myweatherforecast.rb', line 319

def tuesday()   day :tuesday   end

#wednesdayObject Also known as: wed



320
# File 'lib/myweatherforecast.rb', line 320

def wednesday() day :wednesday end