From b6f9bf08b04d6e58a0a3851766423e88e13d2826 Mon Sep 17 00:00:00 2001 From: Marvin Frederickson Date: Thu, 25 May 2017 17:09:25 -0800 Subject: [PATCH] dry, rubocop placations --- app/models/weather.rb | 256 +++++++++++++++++------------------------- 1 file changed, 100 insertions(+), 156 deletions(-) diff --git a/app/models/weather.rb b/app/models/weather.rb index 40587cb..4bd09ec 100644 --- a/app/models/weather.rb +++ b/app/models/weather.rb @@ -1,21 +1,21 @@ class Weather < DynamicContent - DISPLAY_NAME = 'Weather' + DISPLAY_NAME = 'Weather'.freeze UNITS = { 'metric' => 'Celsius', 'imperial' => 'Fahrenheit' - } + }.freeze FONTS = { 'owf' => 'Open Weather Font', 'wi' => 'Weather Icons' - } + }.freeze FORECAST = { 'realtime' => 'Realtime Weather', 'forecast' => 'Max and Min temps forecast for the day', 'nextday' => 'Max and Min temps forecast for the next day' - } + }.freeze validate :validate_config @@ -23,103 +23,61 @@ def build_content require 'json' require 'net/http' + # initialize replacement vars incase they are net set by the chosen forecast + format_city = '' + format_current = '' + format_high = '' + format_icon = '' + format_iconid = '' + format_low = '' + forecast_type = self.config['forecast_type'] + format_string = self.config['format_string'] font_name = self.config['font_name'] - if forecast_type == 'forecast' - # Full day forecast - # Build request url - params = { - lat: self.config['lat'], - lon: self.config['lng'], - units: self.config['units'], - cnt: 1, - 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 = "" - else - format_icon = "" - end - - format_high = "#{data['list'][0]['temp']['max'].round(0)} °#{UNITS[params[:units]][0]}" - format_low = "#{data['list'][0]['temp']['min'].round(0)} °#{UNITS[params[:units]][0]}" - # the Redcarpet gem will assume leading spaces indicate an indented code block - empty_html = " -

Today in #{format_city}

-
- #{format_icon} -
-
-

High

-

#{format_high}

-

Low

-

#{format_low}

-
-" - 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 = "" - else - format_icon = "" - end - - format_high = "#{data['list'][1]['temp']['max'].round(0)} °#{UNITS[params[:units]][0]}" - format_low = "#{data['list'][1]['temp']['min'].round(0)} °#{UNITS[params[:units]][0]}" - # the Redcarpet gem will assume leading spaces indicate an indented code block - empty_html = " -

Tomorrow in #{format_city}

+ # set command api parameters + params = { + lat: self.config['lat'], + lon: self.config['lng'], + units: self.config['units'], + mode: 'json', + appid: ConcertoConfig['open_weather_map_api_key'] + } + + if forecast_type == 'forecast' || forecast_type == 'nextday' + if forecast_type == 'forecast' + title = "Today's" + params[:cnt] = 1 + else + title = "Tomorrow's" + params[:cnt] = 2 + end + + url = "http://api.openweathermap.org/data/2.5/forecast/daily?#{params.to_query}" + 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'].last['weather'][0]['id']}" + if font_name == 'wi' + format_icon = "" + else + format_icon = "" + end + format_high = "#{data['list'].last['temp']['max'].round(0)} °#{UNITS[params[:units]][0]}" + format_low = "#{data['list'].last['temp']['min'].round(0)} °#{UNITS[params[:units]][0]}" + + # the Redcarpet gem will assume leading spaces indicate an indented code block + default_html = " +

#{forecast_type == 'forecast' ? 'Today' : 'Tomorrow'} in #{format_city}

#{format_icon}
@@ -131,46 +89,35 @@ def build_content " else - # We're using realtime weather forecast - # Build request url - params = { - lat: self.config['lat'], - lon: self.config['lng'], - units: self.config['units'], - mode: 'json', - appid: ConcertoConfig['open_weather_map_api_key'] - } - - url = "http://api.openweathermap.org/data/2.5/weather?#{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["name"] - - format_city = data['name'] - format_iconid = "#{data['weather'][0]['id']}" - - if font_name=='wi' - format_icon = "" - else - format_icon = "" - end - - format_high = "#{data['main']['temp_max'].round(0)} °#{UNITS[params[:units]][0]}" - format_low = "#{data['main']['temp_min'].round(0)} °#{UNITS[params[:units]][0]}" - format_current = "#{data['main']['temp'].round(0)} °#{UNITS[params[:units]][0]}" - # the Redcarpet gem will assume leading spaces indicate an indented code block - empty_html = " + # We're using realtime weather forecast + + title = 'Current' + url = "http://api.openweathermap.org/data/2.5/weather?#{params.to_query}" + 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["name"] + + format_city = data['name'] + format_iconid = "#{data['weather'][0]['id']}" + if font_name == 'wi' + format_icon = "" + else + format_icon = "" + end + format_high = "#{data['main']['temp_max'].round(0)} °#{UNITS[params[:units]][0]}" + format_low = "#{data['main']['temp_min'].round(0)} °#{UNITS[params[:units]][0]}" + format_current = "#{data['main']['temp'].round(0)} °#{UNITS[params[:units]][0]}" + + # the Redcarpet gem will assume leading spaces indicate an indented code block + default_html = "

Today in #{format_city}

#{format_icon} @@ -182,36 +129,33 @@ def build_content " end - format_string = self.config['format_string'] - if format_string.blank? - rawhtml = empty_html + result_html = default_html else - 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 + result_html = format_string + result_html.sub! '#{format_city}', format_city + result_html.sub! '#{format_iconid}', format_iconid + result_html.sub! '#{format_icon}', format_icon + result_html.sub! '#{format_high}', format_high + result_html.sub! '#{format_low}', format_low + result_html.sub! '#{format_current}', format_current end # Create HtmlText content - htmltext = HtmlText.new() - htmltext.name = "Today's weather in #{format_city}" - htmltext.data = rawhtml - return [htmltext] + htmltext = HtmlText.new + htmltext.name = "#{title} weather in #{format_city}" + htmltext.data = result_html + + [htmltext] end # Weather needs a location. Also allow specification of units def self.form_attributes attributes = super() - attributes.concat([:config => [:lat, :lng, :units, :font_name, :location_name, :format_string, :forecast_type]]) + attributes.concat([config: [:lat, :lng, :units, :font_name, :location_name, :format_string, :forecast_type]]) end def validate_config - if self.config['lat'].blank? || self.config['lng'].blank? - errors.add(:base, 'A city must be selected') - end + errors.add(:base, 'A city must be selected') if self.config['lat'].blank? || self.config['lng'].blank? end end