huginn_openweathermap_agent 0.1.11

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: ec6ffdd01d7f47ab353f20a8dd191269c1dbfb90891942d7185430c706d0645c
4
+ data.tar.gz: f7071091bdc5a1fa0be03ac49dbf194d8131b812a8267b1379f785164e281cb9
5
+ SHA512:
6
+ metadata.gz: 9dd85480fcef0ebfb0c436994063b2efce34ec4e44deb1f3ce6adc03d4cb8b30f662e10294f802db805a05c50e3b218e2e1c13d4654e2210d114e646914cf1c4
7
+ data.tar.gz: b6a381236fad69c8d3db626efeaf3a5559fff64ded5a6cf615effe093f1dacac7458af8776fad49e7d06f43a9b8f4dd60ff669dcbd168e5fea781685d3e338d1
data/LICENSE.txt ADDED
@@ -0,0 +1,7 @@
1
+ Copyright (c) 2022 Nicolas Germain
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,213 @@
1
+ module Agents
2
+ class OpenweathermapAgent < Agent
3
+ include FormConfigurable
4
+ can_dry_run!
5
+ no_bulk_receive!
6
+ default_schedule "never"
7
+
8
+ description <<-MD
9
+ The Weather Agent creates an event for the day's weather at a given `location`.
10
+
11
+ I created this agent because about [Dark Sky](https://darksky.net/dev/) -> "We are no longer accepting new signups".
12
+
13
+ The weather forecast information is provided by Openweathermap.
14
+
15
+ The `language` parameter to get the output in your language
16
+
17
+ The `lat` (latitude) and `lon` (longitude) must be configured for current_weather. For example, San Francisco would be `37.7771,-122.4196`.
18
+
19
+ You must set up an [API key for Openweathermap](https://home.openweathermap.org/api_keys) in order to use this Agent.
20
+
21
+ Set `expected_update_period_in_days` to the maximum amount of time that you'd expect to pass between Events being created by this Agent.
22
+
23
+ MD
24
+
25
+ event_description <<-MD
26
+ Events look like this:
27
+
28
+ {
29
+ "coord": {
30
+ "lon": 2.3333,
31
+ "lat": 48.8667
32
+ },
33
+ "weather": [
34
+ {
35
+ "id": 800,
36
+ "main": "Clear",
37
+ "description": "clear sky",
38
+ "icon": "01d"
39
+ }
40
+ ],
41
+ "base": "stations",
42
+ "main": {
43
+ "temp": 300.35,
44
+ "feels_like": 300.5,
45
+ "temp_min": 298.87,
46
+ "temp_max": 301.01,
47
+ "pressure": 1008,
48
+ "humidity": 46
49
+ },
50
+ "visibility": 10000,
51
+ "wind": {
52
+ "speed": 3.6,
53
+ "deg": 210
54
+ },
55
+ "clouds": {
56
+ "all": 0
57
+ },
58
+ "dt": 1656004271,
59
+ "sys": {
60
+ "type": 2,
61
+ "id": 2041230,
62
+ "country": "FR",
63
+ "sunrise": 1655956045,
64
+ "sunset": 1656014290
65
+ },
66
+ "timezone": 7200,
67
+ "id": 6545270,
68
+ "name": "Palais-Royal",
69
+ "cod": 200
70
+ }
71
+ MD
72
+
73
+ def default_options
74
+ {
75
+ 'type' => '',
76
+ 'token' => '',
77
+ 'limit' => '',
78
+ 'unit' => 'metric',
79
+ 'lat' => '',
80
+ 'lon' => '',
81
+ 'language' => 'en',
82
+ 'debug' => 'false',
83
+ 'emit_events' => 'true',
84
+ 'expected_receive_period_in_days' => '2',
85
+ }
86
+ end
87
+
88
+ form_configurable :debug, type: :boolean
89
+ form_configurable :emit_events, type: :boolean
90
+ form_configurable :expected_receive_period_in_days, type: :string
91
+ form_configurable :type, type: :array, values: ['current_weather', 'air_pollution', 'onecall', 'forecast5d']
92
+ form_configurable :unit, type: :array, values: ['metric', 'standard', 'imperial']
93
+ form_configurable :token, type: :string
94
+ form_configurable :limit, type: :string
95
+ form_configurable :language, type: :string
96
+ form_configurable :lat, type: :string
97
+ form_configurable :lon, type: :string
98
+ def validate_options
99
+ errors.add(:base, "type has invalid value: should be 'current_weather' 'air_pollution' 'onecall' 'forecast5d'") if interpolated['type'].present? && !%w(current_weather air_pollution onecall forecast5d).include?(interpolated['type'])
100
+
101
+ errors.add(:base, "unit has invalid value: should be 'metric' 'standard' 'imperial'") if interpolated['type'].present? && !%w(metric standard imperial).include?(interpolated['unit'])
102
+ errors.add(:base, "lon must be provided") if not interpolated['lon'].present? && ( interpolated['type'] == 'current_weather' or interpolated['type'] == 'air_pollution' or interpolated['type'] == 'onecall' or interpolated['type'] == 'forecast5d')
103
+
104
+ errors.add(:base, "lat must be provided") if not interpolated['lat'].present? && ( interpolated['type'] == 'current_weather' or interpolated['type'] == 'air_pollution' or interpolated['type'] == 'onecall' or interpolated['type'] == 'forecast5d')
105
+
106
+ unless options['language'].present?
107
+ errors.add(:base, "language is a required field")
108
+ end
109
+
110
+ unless options['token'].present?
111
+ errors.add(:base, "token is a required field")
112
+ end
113
+
114
+ if options.has_key?('emit_events') && boolify(options['emit_events']).nil?
115
+ errors.add(:base, "if provided, emit_events must be true or false")
116
+ end
117
+
118
+ if options.has_key?('debug') && boolify(options['debug']).nil?
119
+ errors.add(:base, "if provided, debug must be true or false")
120
+ end
121
+
122
+ unless options['expected_receive_period_in_days'].present? && options['expected_receive_period_in_days'].to_i > 0
123
+ errors.add(:base, "Please provide 'expected_receive_period_in_days' to indicate how many days can pass before this Agent is considered to be not working")
124
+ end
125
+ end
126
+
127
+ def working?
128
+ event_created_within?(options['expected_receive_period_in_days']) && !recent_error_logs?
129
+ end
130
+
131
+ def receive(incoming_events)
132
+ incoming_events.each do |event|
133
+ interpolate_with(event) do
134
+ log event
135
+ trigger_action
136
+ end
137
+ end
138
+ end
139
+
140
+ def check
141
+ trigger_action
142
+ end
143
+
144
+ private
145
+
146
+ def log_curl_output(code,body)
147
+
148
+ log "request status : #{code}"
149
+
150
+ if interpolated['debug'] == 'true'
151
+ log "body"
152
+ log body
153
+ end
154
+
155
+ if interpolated['emit_events'] == 'true'
156
+ create_event payload: body
157
+ end
158
+
159
+ end
160
+
161
+ def get_forecast5d()
162
+
163
+ uri = URI.parse("https://api.openweathermap.org/data/2.5/forecast?lat=#{interpolated['lat']}&units=#{interpolated['unit']}&lon=#{interpolated['lon']}&appid=#{interpolated['token']}&lang=#{interpolated['language']}")
164
+ response = Net::HTTP.get_response(uri)
165
+
166
+ log_curl_output(response.code,response.body)
167
+
168
+ end
169
+
170
+ def get_current_weather()
171
+
172
+ uri = URI.parse("https://api.openweathermap.org/data/2.5/weather?lat=#{interpolated['lat']}&units=#{interpolated['unit']}&lon=#{interpolated['lon']}&appid=#{interpolated['token']}&lang=#{interpolated['language']}")
173
+ response = Net::HTTP.get_response(uri)
174
+
175
+ log_curl_output(response.code,response.body)
176
+
177
+ end
178
+
179
+ def get_air_pollution()
180
+
181
+ uri = URI.parse("https://api.openweathermap.org/data/2.5/air_pollution?lat=#{interpolated['lat']}&units=#{interpolated['unit']}&lon=#{interpolated['lon']}&appid=#{interpolated['token']}&lang=#{interpolated['language']}")
182
+ response = Net::HTTP.get_response(uri)
183
+
184
+ log_curl_output(response.code,response.body)
185
+
186
+ end
187
+
188
+ def get_onecall()
189
+
190
+ uri = URI.parse("https://api.openweathermap.org/data/3.0/onecall?lat=#{interpolated['lat']}&units=#{interpolated['unit']}&lon=#{interpolated['lon']}&appid=#{interpolated['token']}&lang=#{interpolated['language']}")
191
+ response = Net::HTTP.get_response(uri)
192
+
193
+ log_curl_output(response.code,response.body)
194
+
195
+ end
196
+
197
+ def trigger_action
198
+
199
+ case interpolated['type']
200
+ when "current_weather"
201
+ get_current_weather()
202
+ when "air_pollution"
203
+ get_air_pollution()
204
+ when "onecall"
205
+ get_onecall()
206
+ when "forecast5d"
207
+ get_forecast5d()
208
+ else
209
+ log "Error: type has an invalid value (#{type})"
210
+ end
211
+ end
212
+ end
213
+ end
@@ -0,0 +1,4 @@
1
+ require 'huginn_agent'
2
+
3
+ #HuginnAgent.load 'huginn_openweathermap_agent/concerns/my_agent_concern'
4
+ HuginnAgent.register 'huginn_openweathermap_agent/openweathermap_agent'
@@ -0,0 +1,13 @@
1
+ require 'rails_helper'
2
+ require 'huginn_agent/spec_helper'
3
+
4
+ describe Agents::OpenweathermapAgent do
5
+ before(:each) do
6
+ @valid_options = Agents::OpenweathermapAgent.new.default_options
7
+ @checker = Agents::OpenweathermapAgent.new(:name => "OpenweathermapAgent", :options => @valid_options)
8
+ @checker.user = users(:bob)
9
+ @checker.save!
10
+ end
11
+
12
+ pending "add specs here"
13
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: huginn_openweathermap_agent
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.11
5
+ platform: ruby
6
+ authors:
7
+ - Nicolas Germain
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-10-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 2.1.0
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 2.1.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 12.3.3
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 12.3.3
41
+ - !ruby/object:Gem::Dependency
42
+ name: huginn_agent
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Write a longer description or delete this line.
56
+ email:
57
+ - ngermain@hihouhou.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - LICENSE.txt
63
+ - lib/huginn_openweathermap_agent.rb
64
+ - lib/huginn_openweathermap_agent/openweathermap_agent.rb
65
+ - spec/openweathermap_agent_spec.rb
66
+ homepage: https://github.com/hihouhou/huginn_openweathermap_agent
67
+ licenses:
68
+ - MIT
69
+ metadata: {}
70
+ post_install_message:
71
+ rdoc_options: []
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ requirements: []
85
+ rubygems_version: 3.3.3
86
+ signing_key:
87
+ specification_version: 4
88
+ summary: Write a short summary, because Rubygems requires one.
89
+ test_files:
90
+ - spec/openweathermap_agent_spec.rb