Class: OpenWeatherRaoni::OpenWeatherMapApi
- Inherits:
-
Object
- Object
- OpenWeatherRaoni::OpenWeatherMapApi
- Defined in:
- lib/open_weather_raoni/open_weather_map_api.rb
Instance Method Summary collapse
- #api_current_weather(city) ⇒ Object
- #api_five_day_weather(city) ⇒ Object
- #calculate_temperature_average(list) ⇒ Object
- #call_open_weather_api(composed_url) ⇒ Object
- #current_weather(city) ⇒ Object
-
#initialize(key) ⇒ OpenWeatherMapApi
constructor
A new instance of OpenWeatherMapApi.
- #next_five_days_forecast(city) ⇒ Object
- #parse_current_weather(response) ⇒ Object
- #parse_five_days_result(list) ⇒ Object
Constructor Details
#initialize(key) ⇒ OpenWeatherMapApi
Returns a new instance of OpenWeatherMapApi.
15 16 17 18 |
# File 'lib/open_weather_raoni/open_weather_map_api.rb', line 15 def initialize key @api_key = key @api_link = "https://api.openweathermap.org/data/2.5" end |
Instance Method Details
#api_current_weather(city) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/open_weather_raoni/open_weather_map_api.rb', line 48 def api_current_weather city endpoint = "/weather" encoded_city = URI.encode_www_form_component(city) city_param = "?q=#{encoded_city}" unit_param = "&units=metric" language = "&lang=pt_br" api_key_param = "&appid=#{@api_key}" composed_url = @api_link + endpoint + city_param + unit_param + language + api_key_param call_open_weather_api composed_url end |
#api_five_day_weather(city) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/open_weather_raoni/open_weather_map_api.rb', line 61 def api_five_day_weather city endpoint = "/forecast" encoded_city = URI.encode_www_form_component(city) city_param = "?q=#{encoded_city}" unit_param = "&units=metric" api_key_param = "&appid=#{@api_key}" composed_url = @api_link + endpoint + city_param + unit_param + api_key_param call_open_weather_api composed_url end |
#calculate_temperature_average(list) ⇒ Object
133 134 135 136 |
# File 'lib/open_weather_raoni/open_weather_map_api.rb', line 133 def calculate_temperature_average list temperature_sum = list.sum {|item| item["main"]["temp"]} return (temperature_sum / list.size).floor(2) end |
#call_open_weather_api(composed_url) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/open_weather_raoni/open_weather_map_api.rb', line 73 def call_open_weather_api composed_url url = URI(composed_url) http = Net::HTTP.new(url.host, url.port) http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE request = Net::HTTP::Get.new(url) response = http.request(request) body = "" if response.code != "500" body = JSON.parse(response.read_body) if response.code != "200" raise OpenWeatherRaoni::OpenWeatherApiError.new body["message"] end else raise OpenWeatherRaoni::OpenWeatherApiError.new "Could not reach server" end return body end |
#current_weather(city) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/open_weather_raoni/open_weather_map_api.rb', line 35 def current_weather city response = "" begin response = api_current_weather city rescue OpenWeatherApiError => e return { :error => true, :message => e. } end parse_current_weather response end |
#next_five_days_forecast(city) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/open_weather_raoni/open_weather_map_api.rb', line 20 def next_five_days_forecast city response = "" begin response = api_five_day_weather city rescue OpenWeatherRaoni::OpenWeatherApiError => e e.inspect return { :error => true, :message => e. } end forecasts = response["list"] parse_five_days_result forecasts end |
#parse_current_weather(response) ⇒ Object
98 99 100 101 102 103 104 |
# File 'lib/open_weather_raoni/open_weather_map_api.rb', line 98 def parse_current_weather response return { :error => false, :temperature => response["main"]["temp"], :weather => response["weather"][0]["description"] } end |
#parse_five_days_result(list) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/open_weather_raoni/open_weather_map_api.rb', line 106 def parse_five_days_result list forecasts = [] grouped_list = list.group_by { |g| g["dt_txt"].split[0] } # ignore today if (grouped_list.size > 5) grouped_list.shift end grouped_list.each do |date, list| temperature_average = calculate_temperature_average list forecast_hash = { :temperature_average => temperature_average, :date => date } forecasts << forecast_hash end response = { :error => false, :list => forecasts } return response end |