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
132
133
134
135
136
137
138
139
140
141
142
143
|
# File 'app/models/weather.rb', line 21
def build_content
require 'json'
require 'net/http'
forecast_type = self.config['forecast_type']
font_name = self.config['font_name']
if forecast_type == 'forecast'
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}"
response = Net::HTTP.get_response(URI.parse(url)).body
data = JSON.parse(response)
if data['cod'].present? && !data['cod'].to_s.starts_with?('2')
Rails.logger.error("response (#{url}) = #{response}")
return nil
end
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)} °#{UNITS[params[:units]][0]}"
format_low = "#{data['list'][0]['temp']['min'].round(0)} °#{UNITS[params[:units]][0]}"
empty_html = "
<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
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}"
response = Net::HTTP.get_response(URI.parse(url)).body
data = JSON.parse(response)
if data['cod'].present? && !data['cod'].to_s.starts_with?('2')
Rails.logger.error("response (#{url}) = #{response}")
return nil
end
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)} °#{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]}"
empty_html = "
<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
htmltext = HtmlText.new()
htmltext.name = "Today's weather in #{format_city}"
htmltext.data = rawhtml
return [htmltext]
end
|