Class: WeatherCityForecast::Weather
- Inherits:
-
Object
- Object
- WeatherCityForecast::Weather
- Includes:
- WeatherUtils::Utils
- Defined in:
- lib/weather_city_forecast.rb
Instance Attribute Summary collapse
-
#appid ⇒ Object
readonly
Returns the value of attribute appid.
-
#base_url ⇒ Object
readonly
Returns the value of attribute base_url.
Instance Method Summary collapse
- #headers ⇒ Object
-
#initialize(appid) ⇒ Weather
constructor
A new instance of Weather.
- #weather_by_city(city = nil, state = nil) ⇒ Object
Methods included from WeatherUtils::Utils
Constructor Details
#initialize(appid) ⇒ Weather
Returns a new instance of Weather.
11 12 13 14 |
# File 'lib/weather_city_forecast.rb', line 11 def initialize appid @appid = appid @base_url = 'https://api.openweathermap.org/data/2.5/forecast' end |
Instance Attribute Details
#appid ⇒ Object (readonly)
Returns the value of attribute appid.
8 9 10 |
# File 'lib/weather_city_forecast.rb', line 8 def appid @appid end |
#base_url ⇒ Object (readonly)
Returns the value of attribute base_url.
8 9 10 |
# File 'lib/weather_city_forecast.rb', line 8 def base_url @base_url end |
Instance Method Details
#headers ⇒ Object
16 17 18 19 20 21 |
# File 'lib/weather_city_forecast.rb', line 16 def headers { 'Accept': 'application/json', 'Content-type': 'application/json' } end |
#weather_by_city(city = nil, state = nil) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/weather_city_forecast.rb', line 23 def weather_by_city(city = nil, state = nil) begin raise ArgumentError, 'City is required' unless city querystring = "?q=#{city}, #{state}&units=metric&lang=pt_br&appid=#{@appid}" url = @base_url + querystring response = RestClient.get(url, headers) format_data response.body rescue RestClient::Exception => e e. rescue Exception => e e. end end |