Class: WeatherCityForecast::Weather

Inherits:
Object
  • Object
show all
Includes:
WeatherUtils::Utils
Defined in:
lib/weather_city_forecast.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from WeatherUtils::Utils

format_data

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

#appidObject (readonly)

Returns the value of attribute appid.



8
9
10
# File 'lib/weather_city_forecast.rb', line 8

def appid
  @appid
end

#base_urlObject (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

#headersObject



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.message
  rescue Exception => e
    e.message
  end
end