concerto_weather 0.5 → 0.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 71f297c5a33be038f4b200406aa29a80b9fb6d18
4
- data.tar.gz: 153d8f18b91219bc5592e5d3e36e97bff4498ba6
3
+ metadata.gz: 4cc51a447fc74de85eb7c5a0cb6044786aca27c1
4
+ data.tar.gz: 2338b3c08deb9118e1583005ec31ad590aa773c6
5
5
  SHA512:
6
- metadata.gz: 83d44d494a800265ec646e4af6f699ac910e42be7ade1ec1514dcd98f2be18898fe672307d2a9471e399d8ce106e48394b937c4b8a5a543b1cfbe248f71b65b7
7
- data.tar.gz: 3db8207e0d7cd9cdf60f8f80809aedf66c6830ed88ed4fe2fb69689fc821d9487d7a9844c3c862f8b304540e5adf04d1ddf4eb919f453af969cab1dbb4600cff
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=#{query}&type=like&mode=json&appid=#{appid}"
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
@@ -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
- if forecast_type == 'forecast'
29
- # Full day forecast
30
- # Build request url
31
- params = {
32
- lat: self.config['lat'],
33
- lon: self.config['lng'],
34
- units: self.config['units'],
35
- cnt: 1,
36
- mode: 'json',
37
- appid: ConcertoConfig['open_weather_map_api_key']
38
- }
39
-
40
- url = "http://api.openweathermap.org/data/2.5/forecast/daily?#{params.to_query}"
41
-
42
- # Make request to OpenWeatherMapAPI
43
- response = Net::HTTP.get_response(URI.parse(url)).body
44
- data = JSON.parse(response)
45
-
46
- # if there was an error, then return nil
47
- if data['cod'].present? && !data['cod'].to_s.starts_with?('2')
48
- Rails.logger.error("response (#{url}) = #{response}")
49
- return nil
50
- end
51
-
52
- # Build HTML using API data
53
-
54
- self.config["location_name"] = data["city"]["name"]
55
-
56
- format_city = data['city']['name']
57
- format_iconid = "#{data['list'][0]['weather'][0]['id']}"
58
-
59
- if font_name=='wi'
60
- format_icon = "<i style=\'font-size:calc(min(80vh,80vw));' class=\'wi wi-owm-#{format_iconid}\'></i>"
61
- else
62
- format_icon = "<i class=\'owf owf-#{format_iconid} owf-5x\'></i>"
63
- end
64
-
65
- format_high = "#{data['list'][0]['temp']['max'].round(0)} &deg;#{UNITS[params[:units]][0]}"
66
- format_low = "#{data['list'][0]['temp']['min'].round(0)} &deg;#{UNITS[params[:units]][0]}"
67
- empty_html = "
68
- <h1> Today in #{format_city} </h1>
69
- <div style='float: left; width: 50%'>
70
- #{format_icon}
71
- </div>
72
- <div style='float: left; width: 50%'>
73
- <p> High </p>
74
- <h1> #{format_high} </h1>
75
- <p> Low </p>
76
- <h1> #{format_low}</h1>
77
- </div>
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)} &deg;#{UNITS[params[:units]][0]}"
76
+ format_low = "#{data['list'].last['temp']['min'].round(0)} &deg;#{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
- # We're using realtime weather forecast
81
- # Build request url
82
- params = {
83
- lat: self.config['lat'],
84
- lon: self.config['lng'],
85
- units: self.config['units'],
86
- mode: 'json',
87
- appid: ConcertoConfig['open_weather_map_api_key']
88
- }
89
-
90
- url = "http://api.openweathermap.org/data/2.5/weather?#{params.to_query}"
91
-
92
- # Make request to OpenWeatherMapAPI
93
- response = Net::HTTP.get_response(URI.parse(url)).body
94
- data = JSON.parse(response)
95
-
96
- # if there was an error, then return nil
97
- if data['cod'].present? && !data['cod'].to_s.starts_with?('2')
98
- Rails.logger.error("response (#{url}) = #{response}")
99
- return nil
100
- end
101
-
102
- # Build HTML using API data
103
-
104
- self.config["location_name"] = data["name"]
105
-
106
- format_city = data['name']
107
- format_iconid = "#{data['weather'][0]['id']}"
108
-
109
- if font_name=='wi'
110
- format_icon = "<i style=\'font-size:calc(min(80vh,80vw));' class=\'wi wi-owm-#{format_iconid}\'></i>"
111
- else
112
- format_icon = "<i class=\'owf owf-#{format_iconid} owf-5x\'></i>"
113
- end
114
-
115
- format_high = "#{data['main']['temp_max'].round(0)} &deg;#{UNITS[params[:units]][0]}"
116
- format_low = "#{data['main']['temp_min'].round(0)} &deg;#{UNITS[params[:units]][0]}"
117
- format_current = "#{data['main']['temp'].round(0)} &deg;#{UNITS[params[:units]][0]}"
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)} &deg;#{UNITS[params[:units]][0]}"
116
+ format_low = "#{data['main']['temp_min'].round(0)} &deg;#{UNITS[params[:units]][0]}"
117
+ format_current = "#{data['main']['temp'].round(0)} &deg;#{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
- rawhtml = empty_html
133
+ result_html = default_html
134
134
  else
135
- rawhtml = eval("\"" + format_string + "\"")
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 = "Today's weather in #{format_city}"
141
- htmltext.data = rawhtml
142
- return [htmltext]
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([:config => [:lat, :lng, :units, :font_name, :location_name, :format_string, :forecast_type]])
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
@@ -1,3 +1,3 @@
1
1
  module ConcertoWeather
2
- VERSION = "0.5"
2
+ VERSION = '0.6'.freeze
3
3
  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.5'
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-02-17 00:00:00.000000000 Z
11
+ date: 2017-12-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails