Class: Weather

Inherits:
DynamicContent
  • Object
show all
Defined in:
app/models/weather.rb

Constant Summary collapse

DISPLAY_NAME =
'Weather'
UNITS =
{
  'metric' => 'Celsius',
  'imperial' => 'Fahrenheit'
}
FONTS =
{
  'owf' => 'Open Weather Font',
  'wi' => 'Weather Icons' 
}
FORECAST =
{
  'realtime' => 'Realtime Weather',
  'forecast' => 'Max and Min temps forecast for the day'
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.form_attributesObject

Weather needs a location. Also allow specification of units



134
135
136
137
# File 'app/models/weather.rb', line 134

def self.form_attributes
  attributes = super()
  attributes.concat([:config => [:lat, :lng, :units, :font_name, :location_name, :format_string, :forecast_type]])
end

Instance Method Details

#build_contentObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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 'app/models/weather.rb', line 19

def build_content
  require 'json'
  require 'net/http'

  forecast_type = self.config['forecast_type']
  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)

     # 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'][0]['temp']['max'].round(0)} &deg;#{UNITS[params[:units]][0]}"
     format_low = "#{data['list'][0]['temp']['min'].round(0)} &deg;#{UNITS[params[:units]][0]}"
     emptyhtml = "
              <h1> Today 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
     # 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)

     # 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 = "<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['main']['temp_max'].round(0)} &deg;#{UNITS[params[:units]][0]}"
     format_low = "#{data['main']['temp_min'].round(0)} &deg;#{UNITS[params[:units]][0]}"
     format_current = "#{data['main']['temp'].round(0)} &deg;#{UNITS[params[:units]][0]}"
     emptyhtml = "
              <h1> Today in #{format_city} </h1>
              <div style='float: left; width: 50%'>
                 #{format_icon}
              </div>
              <div style='float: left; width: 50%'>
                <p> Current </p>
                <h1> #{format_current} </h1>
              </div>
            "

  end


  format_string = self.config['format_string']

  if format_string.blank? 
     rawhtml = empty_html
  else 
     rawhtml = eval("\"" + format_string + "\"")
  end

  # Create HtmlText content
  htmltext = HtmlText.new()
  htmltext.name = "Today's weather in #{format_city}"
  htmltext.data = rawhtml
  return [htmltext]
end