huginn_alimconfiance_agent 0.1

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: ed6e5b12461d2fab6c34f72aa680e6cdc8f7d5f634a471efbac201f0d4952dc8
4
+ data.tar.gz: f7f16cc8fc3fc3a5ab68fb44b9716f9b75874f4902c4ea3eafdc1dc32de32a0c
5
+ SHA512:
6
+ metadata.gz: 7a1907df54c98f9283352f87fcd95bf964459ef41e6b15c495ecabc00432f3748876cb43d4987f1f6100d5d4de5bb42be0c2db693d995713ca1562008ea828b8
7
+ data.tar.gz: e1bc1011b86d6b250097daea17da208006aaf3e35993182dd2989aba1310ee4802ce753f9c1f277953768b44b1a15bf68456bf86646e1e7b9bda06fe170bc3c3
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,164 @@
1
+ module Agents
2
+ class AlimconfianceAgent < Agent
3
+ include FormConfigurable
4
+
5
+ can_dry_run!
6
+ no_bulk_receive!
7
+ default_schedule 'every_1h'
8
+
9
+ description do
10
+ <<-MD
11
+ The Alimconfiance Agent checks results of official food safety controls carried out since March 1, 2017.
12
+
13
+ `debug` is used to verbose mode.
14
+
15
+ `zip_code` is needed to limit the research.
16
+
17
+ `number_of_result` is for limiting result output.
18
+
19
+ `changes_only` is only used to emit event about a currency's change.
20
+
21
+ `expected_receive_period_in_days` is used to determine if the Agent is working. Set it to the maximum number of days
22
+ that you anticipate passing without this Agent receiving an incoming Event.
23
+ MD
24
+ end
25
+
26
+ event_description <<-MD
27
+ Events look like this:
28
+
29
+ {
30
+ "app_libelle_etablissement": "AU PETIT BRIERES (NICOLAS VINCENT)",
31
+ "siret": "83492327800016",
32
+ "adresse_2_ua": "2 RUE DU PETIT BRIERES",
33
+ "code_postal": "91150",
34
+ "libelle_commune": "BRIERES LES SCELLES",
35
+ "numero_inspection": "16616330",
36
+ "date_inspection": "2022-10-07T00:00:00+00:00",
37
+ "app_libelle_activite_etablissement": [
38
+ "_"
39
+ ],
40
+ "synthese_eval_sanit": "Satisfaisant",
41
+ "agrement": null,
42
+ "geores": {
43
+ "lon": 2.170879,
44
+ "lat": 48.451185
45
+ },
46
+ "filtre": null,
47
+ "ods_type_activite": "Autres"
48
+ }
49
+ MD
50
+
51
+ def default_options
52
+ {
53
+ 'debug' => 'false',
54
+ 'zip_code' => '',
55
+ 'expected_receive_period_in_days' => '2',
56
+ 'number_of_result' => '10',
57
+ 'changes_only' => 'true'
58
+ }
59
+ end
60
+
61
+ form_configurable :expected_receive_period_in_days, type: :string
62
+ form_configurable :number_of_result, type: :number
63
+ form_configurable :zip_code, type: :number
64
+ form_configurable :changes_only, type: :boolean
65
+ form_configurable :debug, type: :boolean
66
+
67
+ def validate_options
68
+ if options.has_key?('changes_only') && boolify(options['changes_only']).nil?
69
+ errors.add(:base, "if provided, changes_only must be true or false")
70
+ end
71
+
72
+ if options.has_key?('debug') && boolify(options['debug']).nil?
73
+ errors.add(:base, "if provided, debug must be true or false")
74
+ end
75
+
76
+ unless options['expected_receive_period_in_days'].present? && options['expected_receive_period_in_days'].to_i > 0
77
+ 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")
78
+ end
79
+
80
+ unless options['zip_code'].present? && options['zip_code'].to_i > 0
81
+ errors.add(:base, "Please provide 'zip_code'")
82
+ end
83
+
84
+ unless options['number_of_result'].present? && options['number_of_result'].to_i > 0
85
+ errors.add(:base, "Please provide 'number_of_result' to limit the result's number")
86
+ end
87
+ end
88
+
89
+ def working?
90
+ event_created_within?(options['expected_receive_period_in_days']) && !recent_error_logs?
91
+ end
92
+
93
+ def check
94
+ fetch
95
+ end
96
+
97
+ private
98
+
99
+ def log_curl_output(code,body)
100
+
101
+ log "request status : #{code}"
102
+
103
+ if interpolated['debug'] == 'true'
104
+ log "body"
105
+ log body
106
+ end
107
+
108
+ end
109
+
110
+ def fetch
111
+
112
+ uri = URI.parse("https://dgal.opendatasoft.com/api/explore/v2.1/catalog/datasets/export_alimconfiance/records?where=code_postal%3D#{interpolated['zip_code']}&limit=#{interpolated['number_of_result']}")
113
+ response = Net::HTTP.get_response(uri)
114
+
115
+ log_curl_output(response.code,response.body)
116
+
117
+ payload = JSON.parse(response.body)
118
+
119
+ if interpolated['changes_only'] == 'true'
120
+ if payload != memory['last_status']
121
+ if "#{memory['last_status']}" == ''
122
+ payload['results'].each do |result|
123
+ create_event payload: result
124
+ end
125
+ else
126
+ log "before"
127
+ last_status = memory['last_status']
128
+ log "after"
129
+ payload['results'].each do |result|
130
+ found = false
131
+ if interpolated['debug'] == 'true'
132
+ log "result"
133
+ log result
134
+ end
135
+ last_status['results'].each do |resultbis|
136
+ if result == resultbis
137
+ found = true
138
+ end
139
+ if interpolated['debug'] == 'true'
140
+ log "resultbis"
141
+ log resultbis
142
+ log "found is #{found}!"
143
+ end
144
+ end
145
+ if found == false
146
+ if interpolated['debug'] == 'true'
147
+ log "found is #{found}! so event created"
148
+ log result
149
+ end
150
+ create_event payload: result
151
+ end
152
+ end
153
+ end
154
+ memory['last_status'] = payload
155
+ end
156
+ else
157
+ create_event payload: payload
158
+ if payload != memory['last_status']
159
+ memory['last_status'] = payload
160
+ end
161
+ end
162
+ end
163
+ end
164
+ end
@@ -0,0 +1,4 @@
1
+ require 'huginn_agent'
2
+
3
+ #HuginnAgent.load 'huginn_alimconfiance_agent/concerns/my_agent_concern'
4
+ HuginnAgent.register 'huginn_alimconfiance_agent/alimconfiance_agent'
@@ -0,0 +1,13 @@
1
+ require 'rails_helper'
2
+ require 'huginn_agent/spec_helper'
3
+
4
+ describe Agents::AlimconfianceAgent do
5
+ before(:each) do
6
+ @valid_options = Agents::AlimconfianceAgent.new.default_options
7
+ @checker = Agents::AlimconfianceAgent.new(:name => "AlimconfianceAgent", :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_alimconfiance_agent
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ platform: ruby
6
+ authors:
7
+ - Nicolas Germain
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-09-12 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_alimconfiance_agent.rb
64
+ - lib/huginn_alimconfiance_agent/alimconfiance_agent.rb
65
+ - spec/alimconfiance_agent_spec.rb
66
+ homepage: https://github.com/hihouhou/huginn_alimconfiance_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/alimconfiance_agent_spec.rb