Skip to content

Commit

Permalink
Merge pull request #36 from daniel-sousa-me/master
Browse files Browse the repository at this point in the history
Next day forecast and removing unsafe eval
  • Loading branch information
mfrederickson authored May 25, 2017
2 parents d80d05d + 1943cbe commit 56e3617
Showing 1 changed file with 61 additions and 2 deletions.
63 changes: 61 additions & 2 deletions app/models/weather.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ class Weather < DynamicContent

FORECAST = {
'realtime' => 'Realtime Weather',
'forecast' => 'Max and Min temps forecast for the day'
'forecast' => 'Max and Min temps forecast for the day',
'nextday' => 'Max and Min temps forecast for the next day'
}

validate :validate_config
Expand Down Expand Up @@ -76,6 +77,58 @@ def build_content
<p> Low </p>
<h1> #{format_low}</h1>
</div>
"
elsif forecast_type == 'nextday'
# Next day forecast
# Build request url
params = {
lat: self.config['lat'],
lon: self.config['lng'],
units: self.config['units'],
cnt: 2,
mode: 'json',
appid: ConcertoConfig['open_weather_map_api_key']
}

url = "http://api.openweathermap.org/data/2.5/forecast/daily?#{params.to_query}"

# Make request to OpenWeatherMapAPI
response = Net::HTTP.get_response(URI.parse(url)).body
data = JSON.parse(response)

# if there was an error, then return nil
if data['cod'].present? && !data['cod'].to_s.starts_with?('2')
Rails.logger.error("response (#{url}) = #{response}")
return nil
end

# Build HTML using API data

self.config["location_name"] = data["city"]["name"]

format_city = data['city']['name']
format_iconid = "#{data['list'][0]['weather'][0]['id']}"

if font_name=='wi'
format_icon = "<i style=\'font-size:calc(min(80vh,80vw));' class=\'wi wi-owm-#{format_iconid}\'></i>"
else
format_icon = "<i class=\'owf owf-#{format_iconid} owf-5x\'></i>"
end

format_high = "#{data['list'][1]['temp']['max'].round(0)} &deg;#{UNITS[params[:units]][0]}"
format_low = "#{data['list'][1]['temp']['min'].round(0)} &deg;#{UNITS[params[:units]][0]}"
# the Redcarpet gem will assume leading spaces indicate an indented code block
empty_html = "
<h1> Tomorrow in #{format_city} </h1>
<div style='float: left; width: 50%'>
#{format_icon}
</div>
<div style='float: left; width: 50%'>
<p> High </p>
<h1> #{format_high} </h1>
<p> Low </p>
<h1> #{format_low}</h1>
</div>
"
else
# We're using realtime weather forecast
Expand Down Expand Up @@ -134,7 +187,13 @@ def build_content
if format_string.blank?
rawhtml = empty_html
else
rawhtml = eval("\"" + format_string + "\"")
rawhtml = "\"" + format_string + "\""
format_string.sub! '#{format_city}' format_city
format_string.sub! '#{format_iconid}' format_iconid
format_string.sub! '#{format_icon}' format_icon
format_string.sub! '#{format_high}' format_high
format_string.sub! '#{format_low}' format_low
format_string.sub! '#{format_current}' format_current
end

# Create HtmlText content
Expand Down

0 comments on commit 56e3617

Please sign in to comment.