class WeatherCityForecast::Weather

Attributes

appid[R]
base_url[R]

Public Class Methods

new(appid) click to toggle source
# File lib/weather_city_forecast.rb, line 11
def initialize appid
      @appid = appid
      @base_url = 'https://api.openweathermap.org/data/2.5/forecast'
end

Public Instance Methods

headers() click to toggle source
# 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) click to toggle source
# 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