huginn_airparif_agent 0.1.10

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: 0404c2a71258b58c9a2616f186494619bf4419ad331d3352220e4f6e2b393ff7
4
+ data.tar.gz: 8637bc1dbb3ed449ba4126c646b3815df77c91e2464e3f6148ad72d668818433
5
+ SHA512:
6
+ metadata.gz: f3eebe5ce06dca8dec547bc95380015788acbbe5812280d995d35f69ef4306bbc810c80894fc8e89edc5a53675808a5830f590a98e9733254cda7dd172ecf141
7
+ data.tar.gz: aaf4614cbdbb8f986eba9f6afc6d6661eb7f10ebf2ef216ff6b224f4016878bb8fc95e554ba1b81ecaca471ac684c0549a9289a9317cea8f20219a16a70b1d61
data/LICENSE.txt ADDED
@@ -0,0 +1,7 @@
1
+ Copyright (c) 2023 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,264 @@
1
+ module Agents
2
+ class AirparifAgent < Agent
3
+ include FormConfigurable
4
+ can_dry_run!
5
+ no_bulk_receive!
6
+ default_schedule 'every_1h'
7
+
8
+ description do
9
+ <<-MD
10
+ The Airparif Agent interacts with Airparif API.
11
+
12
+ `debug` is used for verbose mode.
13
+
14
+ `apikey` is needed for authenticated endpoint.
15
+
16
+ `insee` is needed for the city ( see `https://www.dcode.fr/code-commune-insee`).
17
+
18
+ `type` is for the wanted action like planned_pollution_indices.
19
+
20
+ `expected_receive_period_in_days` is used to determine if the Agent is working. Set it to the maximum number of days
21
+ that you anticipate passing without this Agent receiving an incoming Event.
22
+ MD
23
+ end
24
+
25
+ event_description <<-MD
26
+ Events look like this:
27
+
28
+ {
29
+ "75101": [
30
+ {
31
+ "date": "2021-01-15",
32
+ "no2": "Bon",
33
+ "o3": "Mauvais",
34
+ "pm10": "Moyen",
35
+ "pm25": "Dégradé",
36
+ "so2": "Bon",
37
+ "indice": "Dégradé"
38
+ },
39
+ {
40
+ "date": "2021-01-16",
41
+ "no2": "Bon",
42
+ "o3": "Mauvais",
43
+ "pm10": "Moyen",
44
+ "pm25": "Dégradé",
45
+ "so2": "Bon",
46
+ "indice": "Dégradé"
47
+ }
48
+ ]
49
+ }
50
+ MD
51
+
52
+ def default_options
53
+ {
54
+ 'apikey' => '',
55
+ 'insee' => '',
56
+ 'type' => 'planned_pollution_indices',
57
+ 'debug' => 'false',
58
+ 'emit_events' => 'true',
59
+ 'expected_receive_period_in_days' => '2',
60
+ }
61
+ end
62
+
63
+ form_configurable :apikey, type: :string
64
+ form_configurable :insee, type: :string
65
+ form_configurable :debug, type: :boolean
66
+ form_configurable :emit_events, type: :boolean
67
+ form_configurable :expected_receive_period_in_days, type: :string
68
+ form_configurable :type, type: :array, values: ['planned_pollution_indices', 'version', 'prevision_bulletin', 'pollens_bulletin']
69
+ def validate_options
70
+ errors.add(:base, "type has invalid value: should be 'planned_pollution_indices', 'version', 'prevision_bulletin', 'pollens_bulletin'") if interpolated['type'].present? && !%w(planned_pollution_indices version prevision_bulletin pollens_bulletin).include?(interpolated['type'])
71
+
72
+ unless options['insee'].present? || !['planned_pollution_indices'].include?(options['type'])
73
+ errors.add(:base, "insee is a required field")
74
+ end
75
+
76
+ unless options['apikey'].present? || !['planned_pollution_indices', 'prevision_bulletin', 'pollens_bulletin'].include?(options['type'])
77
+ errors.add(:base, "apikey is a required field")
78
+ end
79
+
80
+ if options.has_key?('emit_events') && boolify(options['emit_events']).nil?
81
+ errors.add(:base, "if provided, emit_events must be true or false")
82
+ end
83
+
84
+ if options.has_key?('debug') && boolify(options['debug']).nil?
85
+ errors.add(:base, "if provided, debug must be true or false")
86
+ end
87
+
88
+ unless options['expected_receive_period_in_days'].present? && options['expected_receive_period_in_days'].to_i > 0
89
+ 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")
90
+ end
91
+ end
92
+
93
+ def working?
94
+ event_created_within?(options['expected_receive_period_in_days']) && !recent_error_logs?
95
+ end
96
+
97
+ def receive(incoming_events)
98
+ incoming_events.each do |event|
99
+ interpolate_with(event) do
100
+ log event
101
+ trigger_action
102
+ end
103
+ end
104
+ end
105
+
106
+ def check
107
+ trigger_action
108
+ end
109
+
110
+ private
111
+
112
+ def log_curl_output(code,body)
113
+
114
+ log "request status : #{code}"
115
+
116
+ if interpolated['debug'] == 'true'
117
+ log "body"
118
+ log body
119
+ end
120
+
121
+ end
122
+
123
+ def version()
124
+
125
+ uri = URI.parse("https://api.airparif.asso.fr/version")
126
+ request = Net::HTTP::Get.new(uri)
127
+ request["Accept"] = "application/json"
128
+
129
+ req_options = {
130
+ use_ssl: uri.scheme == "https",
131
+ }
132
+
133
+ response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
134
+ http.request(request)
135
+ end
136
+
137
+ log_curl_output(response.code,response.body)
138
+ payload = JSON.parse(response.body)
139
+
140
+ if !memory['version']
141
+ if interpolated['emit_events'] == 'true'
142
+ create_event payload: payload
143
+ end
144
+ else
145
+ last_status = memory['version']
146
+ if payload != last_status
147
+ create_event payload: response.body
148
+ end
149
+ end
150
+ memory['version'] = payload
151
+
152
+ end
153
+
154
+ def planned_pollution_colors()
155
+
156
+ uri = URI.parse("https://api.airparif.asso.fr/indices/prevision/couleurs")
157
+ request = Net::HTTP::Get.new(uri)
158
+ request["Accept"] = "application/json"
159
+ request["X-Api-Key"] = interpolated['apikey']
160
+
161
+ req_options = {
162
+ use_ssl: uri.scheme == "https",
163
+ }
164
+
165
+ response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
166
+ http.request(request)
167
+ end
168
+
169
+ log_curl_output(response.code,response.body)
170
+ payload = JSON.parse(response.body)
171
+
172
+ return payload
173
+
174
+ end
175
+
176
+ def prevision_bulletin()
177
+
178
+ uri = URI.parse("https://api.airparif.asso.fr/indices/prevision/bulletin")
179
+ request = Net::HTTP::Get.new(uri)
180
+ request["Accept"] = "application/json"
181
+ request["X-Api-Key"] = interpolated['apikey']
182
+
183
+ req_options = {
184
+ use_ssl: uri.scheme == "https",
185
+ }
186
+
187
+ response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
188
+ http.request(request)
189
+ end
190
+
191
+ log_curl_output(response.code,response.body)
192
+ payload = JSON.parse(response.body)
193
+
194
+ if interpolated['emit_events'] == 'true'
195
+ create_event payload: payload
196
+ end
197
+
198
+ end
199
+
200
+ def pollens_bulletin()
201
+
202
+ uri = URI.parse("https://api.airparif.asso.fr/pollens/bulletin")
203
+ request = Net::HTTP::Get.new(uri)
204
+ request["Accept"] = "application/json"
205
+ request["X-Api-Key"] = interpolated['apikey']
206
+
207
+ req_options = {
208
+ use_ssl: uri.scheme == "https",
209
+ }
210
+
211
+ response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
212
+ http.request(request)
213
+ end
214
+
215
+ log_curl_output(response.code,response.body)
216
+ payload = JSON.parse(response.body)
217
+
218
+ if interpolated['emit_events'] == 'true'
219
+ create_event payload: payload
220
+ end
221
+
222
+ end
223
+
224
+ def planned_pollution_indices()
225
+
226
+ uri = URI.parse("https://api.airparif.asso.fr/indices/prevision/commune?insee=#{interpolated['insee']}")
227
+ request = Net::HTTP::Get.new(uri)
228
+ request["Accept"] = "application/json"
229
+ request["X-Api-Key"] = interpolated['apikey']
230
+
231
+ req_options = {
232
+ use_ssl: uri.scheme == "https",
233
+ }
234
+
235
+ response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
236
+ http.request(request)
237
+ end
238
+
239
+ log_curl_output(response.code,response.body)
240
+ payload = JSON.parse(response.body)
241
+
242
+ if interpolated['emit_events'] == 'true'
243
+ create_event payload: payload
244
+ end
245
+
246
+ end
247
+
248
+ def trigger_action
249
+
250
+ case interpolated['type']
251
+ when "planned_pollution_indices"
252
+ planned_pollution_indices()
253
+ when "version"
254
+ version()
255
+ when "prevision_bulletin"
256
+ prevision_bulletin()
257
+ when "pollens_bulletin"
258
+ pollens_bulletin()
259
+ else
260
+ log "Error: type has an invalid value (#{interpolated['type']})"
261
+ end
262
+ end
263
+ end
264
+ end
@@ -0,0 +1,4 @@
1
+ require 'huginn_agent'
2
+
3
+ #HuginnAgent.load 'huginn_airparif_agent/concerns/my_agent_concern'
4
+ HuginnAgent.register 'huginn_airparif_agent/airparif_agent'
@@ -0,0 +1,13 @@
1
+ require 'rails_helper'
2
+ require 'huginn_agent/spec_helper'
3
+
4
+ describe Agents::AirparifAgent do
5
+ before(:each) do
6
+ @valid_options = Agents::AirparifAgent.new.default_options
7
+ @checker = Agents::AirparifAgent.new(:name => "AirparifAgent", :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_airparif_agent
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.10
5
+ platform: ruby
6
+ authors:
7
+ - Nicolas Germain
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-02-11 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_airparif_agent.rb
64
+ - lib/huginn_airparif_agent/airparif_agent.rb
65
+ - spec/airparif_agent_spec.rb
66
+ homepage: https://github.com/hihouhou/huginn_airparif_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/airparif_agent_spec.rb