added weather to places

This commit is contained in:
Aaro Koivukangas
2023-12-06 15:33:01 +02:00
parent 7f94ce7b88
commit 36a8355ef4
4 changed files with 50 additions and 1 deletions
+2
View File
@@ -4,6 +4,8 @@ class PlacesController < ApplicationController
def search
@places = BeermappingApi.places_in(params[:city])
@weather = Place.weather params[:city]
@city = params[:city]
if @places.empty?
redirect_to places_path, notice: "No locations in #{params[:city]}"
else
+37
View File
@@ -2,4 +2,41 @@ class Place < OpenStruct
def self.rendered_fields
[:id, :name, :status, :street, :city, :zip, :country, :overall]
end
def self.weather location
weather_api_key = self.api_key
weather_api_url = "http://api.weatherstack.com/current"
response = HTTParty.get "#{weather_api_url}?access_key=#{weather_api_key}&query=#{location}"
#if response["success"]
puts "#{weather_api_url}?access_key=#{weather_api_key}&query=#{location}"
puts response.parsed_response
data = response.parsed_response["current"]
if data.nil?
return
end
weather = self.parse_weather_data data
return weather
end
def self.parse_weather_data data
temperature = data["temperature"]
weather_icon = data["weather_icons"][0]
weather_descriptions = data["weather_descriptions"][0]
weather_icon = { image: weather_icon, description: weather_descriptions}
wind_direction = data["wind_dir"]
wind_speed = data["wind_speed"]
wind = { direction: wind_direction, speed: wind_speed }
weather = { temperature: temperature, wind: wind, weather_icon: weather_icon}
return weather
end
def self.api_key
return nil if Rails.env.test?
key = Rails.application.credentials.weatherstack_api_key
raise 'WEATHERSTACK_API_KEY env variable not defined in credentials' if key.nil?
return key
end
end
+10
View File
@@ -7,6 +7,16 @@
<%= notice %>
<% if @weather %>
<div>
<strong>
The current beer weather in <%= @city%>
</strong>
<p><%= @weather[:temperature] %> Celcius</p>
<img src=<%= @weather[:weather_icon][:image] %> alt=<%= @weather[:weather_icon][:description] %>>
<p><strong>Wind: </strong><%= @weather[:wind][:speed] %>, direction <%= @weather[:wind][:direction]%></p>
</div>
<% end %>
<% if @places %>
<table>
<thead>
+1 -1
View File
@@ -1 +1 @@
Gc10IM4hcI/b0NKtz5TTTCTVHrY0qLbRMMoJ0Bki0YGJ01VIFfII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