concerto_weather 0.5 → 0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +4 -0
- data/app/controllers/concerto_weather/search_controller.rb +3 -1
- data/app/models/weather.rb +121 -116
- data/lib/concerto_weather/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 4cc51a447fc74de85eb7c5a0cb6044786aca27c1
|
|
4
|
+
data.tar.gz: 2338b3c08deb9118e1583005ec31ad590aa773c6
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 0ebee68781d71c7ae64b2e0fb2f3ad6dea08720ab4c15cac999d5cfb3f34214d8dd5aa4e10f885cf71d3ab301ab379694e3b4bb15ac58b7fb1402040128a09a6
|
|
7
|
+
data.tar.gz: 6f8bff58c5e4121f09856b0d4a428787a72d58147f144020b0913c1508c95ceb6bb6b6922b2e36307d183c9539de8a009c087c5aee67838e491ddf9ed8b365a1
|
data/README.md
CHANGED
|
@@ -28,6 +28,8 @@ It uses ruby's variable substitution. The following variables are supported:
|
|
|
28
28
|
* #{format_icon} - the html code to inject the weather icon (<i...> </i>) - uses OWF or WI font based on the setting
|
|
29
29
|
* #{format_high} - High temperature (including units) for the day
|
|
30
30
|
* #{format_low} - Low temperature (including units) for the day
|
|
31
|
+
* #{format_current} - Current temperature (including units). Updated every 5 minutes by default.
|
|
32
|
+
|
|
31
33
|
|
|
32
34
|
It can be used for example as follows:
|
|
33
35
|
|
|
@@ -57,4 +59,6 @@ If no format is specified the following is used:
|
|
|
57
59
|
</div>
|
|
58
60
|
````
|
|
59
61
|
|
|
62
|
+
Current Temperature only available if forecast type selected to be "Realtime".
|
|
63
|
+
Conversely, high/low temperatures for the day are available if forecast is "Max and Min for the day"
|
|
60
64
|
|
|
@@ -12,9 +12,11 @@ module ConcertoWeather
|
|
|
12
12
|
def getOpenWeatherCities(query)
|
|
13
13
|
require 'net/http'
|
|
14
14
|
require 'json'
|
|
15
|
+
require 'erb'
|
|
15
16
|
|
|
17
|
+
query_escaped=ERB::Util.url_encode(query)
|
|
16
18
|
appid = ConcertoConfig["open_weather_map_api_key"]
|
|
17
|
-
url = "http://api.openweathermap.org/data/2.5/find?q=#{
|
|
19
|
+
url = "http://api.openweathermap.org/data/2.5/find?q=#{query_escaped}&type=like&mode=json&appid=#{appid}"
|
|
18
20
|
return Net::HTTP.get(URI(url))
|
|
19
21
|
end
|
|
20
22
|
end
|
data/app/models/weather.rb
CHANGED
|
@@ -1,20 +1,21 @@
|
|
|
1
1
|
class Weather < DynamicContent
|
|
2
|
-
DISPLAY_NAME = 'Weather'
|
|
2
|
+
DISPLAY_NAME = 'Weather'.freeze
|
|
3
3
|
|
|
4
4
|
UNITS = {
|
|
5
5
|
'metric' => 'Celsius',
|
|
6
6
|
'imperial' => 'Fahrenheit'
|
|
7
|
-
}
|
|
7
|
+
}.freeze
|
|
8
8
|
|
|
9
9
|
FONTS = {
|
|
10
10
|
'owf' => 'Open Weather Font',
|
|
11
11
|
'wi' => 'Weather Icons'
|
|
12
|
-
}
|
|
12
|
+
}.freeze
|
|
13
13
|
|
|
14
14
|
FORECAST = {
|
|
15
15
|
'realtime' => 'Realtime Weather',
|
|
16
|
-
'forecast' => 'Max and Min temps forecast for the day'
|
|
17
|
-
|
|
16
|
+
'forecast' => 'Max and Min temps forecast for the day',
|
|
17
|
+
'nextday' => 'Max and Min temps forecast for the next day'
|
|
18
|
+
}.freeze
|
|
18
19
|
|
|
19
20
|
validate :validate_config
|
|
20
21
|
|
|
@@ -22,135 +23,139 @@ class Weather < DynamicContent
|
|
|
22
23
|
require 'json'
|
|
23
24
|
require 'net/http'
|
|
24
25
|
|
|
26
|
+
# initialize replacement vars incase they are net set by the chosen forecast
|
|
27
|
+
format_city = ''
|
|
28
|
+
format_current = ''
|
|
29
|
+
format_high = ''
|
|
30
|
+
format_icon = ''
|
|
31
|
+
format_iconid = ''
|
|
32
|
+
format_low = ''
|
|
33
|
+
|
|
25
34
|
forecast_type = self.config['forecast_type']
|
|
35
|
+
format_string = self.config['format_string']
|
|
26
36
|
font_name = self.config['font_name']
|
|
27
37
|
|
|
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
|
-
|
|
38
|
+
# set command api parameters
|
|
39
|
+
params = {
|
|
40
|
+
lat: self.config['lat'],
|
|
41
|
+
lon: self.config['lng'],
|
|
42
|
+
units: self.config['units'],
|
|
43
|
+
mode: 'json',
|
|
44
|
+
appid: ConcertoConfig['open_weather_map_api_key']
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if forecast_type == 'forecast' || forecast_type == 'nextday'
|
|
48
|
+
if forecast_type == 'forecast'
|
|
49
|
+
title = "Today's"
|
|
50
|
+
params[:cnt] = 1
|
|
51
|
+
else
|
|
52
|
+
title = "Tomorrow's"
|
|
53
|
+
params[:cnt] = 2
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
url = "http://api.openweathermap.org/data/2.5/forecast/daily?#{params.to_query}"
|
|
57
|
+
response = Net::HTTP.get_response(URI.parse(url)).body
|
|
58
|
+
data = JSON.parse(response)
|
|
59
|
+
|
|
60
|
+
# if there was an error, then return nil
|
|
61
|
+
if data['cod'].present? && !data['cod'].to_s.starts_with?('2')
|
|
62
|
+
Rails.logger.error("response (#{url}) = #{response}")
|
|
63
|
+
return nil
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# Build HTML using API data
|
|
67
|
+
self.config["location_name"] = data["city"]["name"]
|
|
68
|
+
format_city = data['city']['name']
|
|
69
|
+
format_iconid = "#{data['list'].last['weather'][0]['id']}"
|
|
70
|
+
if font_name == 'wi'
|
|
71
|
+
format_icon = "<i style=\'font-size:calc(min(80vh,80vw));' class=\'wi wi-owm-#{format_iconid}\'></i>"
|
|
72
|
+
else
|
|
73
|
+
format_icon = "<i class=\'owf owf-#{format_iconid} owf-5x\'></i>"
|
|
74
|
+
end
|
|
75
|
+
format_high = "#{data['list'].last['temp']['max'].round(0)} °#{UNITS[params[:units]][0]}"
|
|
76
|
+
format_low = "#{data['list'].last['temp']['min'].round(0)} °#{UNITS[params[:units]][0]}"
|
|
77
|
+
|
|
78
|
+
# the Redcarpet gem will assume leading spaces indicate an indented code block
|
|
79
|
+
default_html = "
|
|
80
|
+
<h1> #{forecast_type == 'forecast' ? 'Today' : 'Tomorrow'} in #{format_city} </h1>
|
|
81
|
+
<div style='float: left; width: 50%'>
|
|
82
|
+
#{format_icon}
|
|
83
|
+
</div>
|
|
84
|
+
<div style='float: left; width: 50%'>
|
|
85
|
+
<p> High </p>
|
|
86
|
+
<h1> #{format_high} </h1>
|
|
87
|
+
<p> Low </p>
|
|
88
|
+
<h1> #{format_low}</h1>
|
|
89
|
+
</div>
|
|
90
|
+
"
|
|
79
91
|
else
|
|
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
|
-
empty_html = "
|
|
119
|
-
<h1> Today in #{format_city} </h1>
|
|
120
|
-
<div style='float: left; width: 50%'>
|
|
121
|
-
#{format_icon}
|
|
122
|
-
</div>
|
|
123
|
-
<div style='float: left; width: 50%'>
|
|
124
|
-
<p> Current </p>
|
|
125
|
-
<h1> #{format_current} </h1>
|
|
126
|
-
</div>
|
|
127
|
-
"
|
|
92
|
+
# We're using realtime weather forecast
|
|
93
|
+
|
|
94
|
+
title = 'Current'
|
|
95
|
+
url = "http://api.openweathermap.org/data/2.5/weather?#{params.to_query}"
|
|
96
|
+
response = Net::HTTP.get_response(URI.parse(url)).body
|
|
97
|
+
data = JSON.parse(response)
|
|
98
|
+
|
|
99
|
+
# if there was an error, then return nil
|
|
100
|
+
if data['cod'].present? && !data['cod'].to_s.starts_with?('2')
|
|
101
|
+
Rails.logger.error("response (#{url}) = #{response}")
|
|
102
|
+
return nil
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
# Build HTML using API data
|
|
106
|
+
self.config["location_name"] = data["name"]
|
|
107
|
+
|
|
108
|
+
format_city = data['name']
|
|
109
|
+
format_iconid = "#{data['weather'][0]['id']}"
|
|
110
|
+
if font_name == 'wi'
|
|
111
|
+
format_icon = "<i style=\'font-size:calc(min(80vh,80vw));' class=\'wi wi-owm-#{format_iconid}\'></i>"
|
|
112
|
+
else
|
|
113
|
+
format_icon = "<i class=\'owf owf-#{format_iconid} owf-5x\'></i>"
|
|
114
|
+
end
|
|
115
|
+
format_high = "#{data['main']['temp_max'].round(0)} °#{UNITS[params[:units]][0]}"
|
|
116
|
+
format_low = "#{data['main']['temp_min'].round(0)} °#{UNITS[params[:units]][0]}"
|
|
117
|
+
format_current = "#{data['main']['temp'].round(0)} °#{UNITS[params[:units]][0]}"
|
|
118
|
+
|
|
119
|
+
# the Redcarpet gem will assume leading spaces indicate an indented code block
|
|
120
|
+
default_html = "
|
|
121
|
+
<h1> Today in #{format_city} </h1>
|
|
122
|
+
<div style='float: left; width: 50%'>
|
|
123
|
+
#{format_icon}
|
|
124
|
+
</div>
|
|
125
|
+
<div style='float: left; width: 50%'>
|
|
126
|
+
<p> Current </p>
|
|
127
|
+
<h1> #{format_current} </h1>
|
|
128
|
+
</div>
|
|
129
|
+
"
|
|
128
130
|
end
|
|
129
131
|
|
|
130
|
-
format_string = self.config['format_string']
|
|
131
|
-
|
|
132
132
|
if format_string.blank?
|
|
133
|
-
|
|
133
|
+
result_html = default_html
|
|
134
134
|
else
|
|
135
|
-
|
|
135
|
+
result_html = format_string
|
|
136
|
+
result_html.sub! '#{format_city}', format_city
|
|
137
|
+
result_html.sub! '#{format_iconid}', format_iconid
|
|
138
|
+
result_html.sub! '#{format_icon}', format_icon
|
|
139
|
+
result_html.sub! '#{format_high}', format_high
|
|
140
|
+
result_html.sub! '#{format_low}', format_low
|
|
141
|
+
result_html.sub! '#{format_current}', format_current
|
|
136
142
|
end
|
|
137
143
|
|
|
138
144
|
# Create HtmlText content
|
|
139
|
-
htmltext = HtmlText.new
|
|
140
|
-
htmltext.name = "
|
|
141
|
-
htmltext.data =
|
|
142
|
-
|
|
145
|
+
htmltext = HtmlText.new
|
|
146
|
+
htmltext.name = "#{title} weather in #{format_city}"
|
|
147
|
+
htmltext.data = result_html
|
|
148
|
+
|
|
149
|
+
[htmltext]
|
|
143
150
|
end
|
|
144
151
|
|
|
145
152
|
# Weather needs a location. Also allow specification of units
|
|
146
153
|
def self.form_attributes
|
|
147
154
|
attributes = super()
|
|
148
|
-
attributes.concat([:
|
|
155
|
+
attributes.concat([config: [:lat, :lng, :units, :font_name, :location_name, :format_string, :forecast_type]])
|
|
149
156
|
end
|
|
150
157
|
|
|
151
158
|
def validate_config
|
|
152
|
-
if self.config['lat'].blank? || self.config['lng'].blank?
|
|
153
|
-
errors.add(:base, 'A city must be selected')
|
|
154
|
-
end
|
|
159
|
+
errors.add(:base, 'A city must be selected') if self.config['lat'].blank? || self.config['lng'].blank?
|
|
155
160
|
end
|
|
156
161
|
end
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: concerto_weather
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: '0.
|
|
4
|
+
version: '0.6'
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Brian Michalski
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2017-
|
|
11
|
+
date: 2017-12-31 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: rails
|