huginn_rappel_conso_gouv_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: ff64343a33207eec7ed8154aa9981a08ae16edf8e79e47b8483b86a69960c2fd
4
+ data.tar.gz: b1b937956df380e6195019e2e8527db9a6a94ba4fdb6815454ade37fa93c464e
5
+ SHA512:
6
+ metadata.gz: 8245bbb8b4a7917b1c85c0ed03e00d9c826230c5fdd1dbecefbe0d5849726a8eec7da1a910a9c9bce53c5ff9b46b6aed7df3b5ae36e3e58e991f82fda6e24968
7
+ data.tar.gz: 10a037abcf3f88a3edbdc4314c75571bdf6309accab5b384e1a116bc17c89c792d1fce8072f0d4ab6454614a79544806dab0c446608dfa3e126cf42bae5b90fd
data/LICENSE.txt ADDED
@@ -0,0 +1,7 @@
1
+ Copyright (c) 2021 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,173 @@
1
+ module Agents
2
+ class RappelConsoGouvAgent < Agent
3
+ include FormConfigurable
4
+
5
+ can_dry_run!
6
+ no_bulk_receive!
7
+ default_schedule '12h'
8
+
9
+ description do
10
+ <<-MD
11
+ The huginn catalog agent checks if new campaign is available.
12
+
13
+ `debug` is used to verbose mode.
14
+
15
+ `number_of_result` is for limiting result output.
16
+
17
+ `changes_only` is only used to emit event about a currency's change.
18
+
19
+ `expected_receive_period_in_days` is used to determine if the Agent is working. Set it to the maximum number of days
20
+ that you anticipate passing without this Agent receiving an incoming Event.
21
+ MD
22
+ end
23
+
24
+ event_description <<-MD
25
+ Events look like this:
26
+
27
+ {
28
+ "datasetid": "rappelconso0",
29
+ "recordid": "2eec1943f5368c400d8e7def8264ca40e4c7a3e2",
30
+ "fields": {
31
+ "conditionnements": "barquette 125g x2",
32
+ "motif_du_rappel": "présence de morceaux de plastiques bleus",
33
+ "distributeurs": "Novoviande",
34
+ "temperature_de_conservation": "Produit à conserver au réfrigérateur",
35
+ "zone_geographique_de_vente": "France entière",
36
+ "date_de_fin_de_la_procedure_de_rappel": "mercredi 7 juillet 2021",
37
+ "ndeg_de_version": 1,
38
+ "marque_de_salubrite": "FR 13.097.003 CE",
39
+ "nature_juridique_du_rappel": "Volontaire",
40
+ "identification_des_produits": "21151-4509 Date limite de consommation 08/06/2021",
41
+ "nom_de_la_marque_du_produit": "La belle nature",
42
+ "preconisations_sanitaires": "En raison du risque de blessures / effets indésirables suite à l'ingestion de ce produit, par précaution il est recommandé aux personnes qui détiendraient des produits appartenant au(x) lot(s) décrit(s) ci-dessus de ne pas les consommer.",
43
+ "date_debut_fin_de_commercialisation": "Du 31/05/2021 au 08/06/2021",
44
+ "conduites_a_tenir_par_le_consommateur": "Ne plus consommer",
45
+ "modalites_de_compensation": "Remboursement",
46
+ "noms_des_modeles_ou_references": "Steak haché 5% X2 125G",
47
+ "categorie_de_produit": "Alimentation",
48
+ "sous_categorie_de_produit": "Viandes",
49
+ "reference_fiche": "2021-06-0295",
50
+ "risques_encourus_par_le_consommateur": "Inertes (verre, métal, plastique, papier, textile…)",
51
+ "date_ref": "2021-06",
52
+ "numero_de_contact": "0490478930"
53
+ },
54
+ "record_timestamp": "2021-06-15T01:00:00.756+02:00"
55
+ }
56
+ MD
57
+
58
+ def default_options
59
+ {
60
+ 'debug' => 'false',
61
+ 'expected_receive_period_in_days' => '2',
62
+ 'number_of_result' => '10',
63
+ 'changes_only' => 'true'
64
+ }
65
+ end
66
+
67
+ form_configurable :expected_receive_period_in_days, type: :string
68
+ form_configurable :number_of_result, type: :string
69
+ form_configurable :changes_only, type: :boolean
70
+ form_configurable :debug, type: :boolean
71
+
72
+ def validate_options
73
+ if options.has_key?('changes_only') && boolify(options['changes_only']).nil?
74
+ errors.add(:base, "if provided, changes_only must be true or false")
75
+ end
76
+
77
+ if options.has_key?('debug') && boolify(options['debug']).nil?
78
+ errors.add(:base, "if provided, debug must be true or false")
79
+ end
80
+
81
+ unless options['expected_receive_period_in_days'].present? && options['expected_receive_period_in_days'].to_i > 0
82
+ 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")
83
+ end
84
+
85
+ unless options['number_of_result'].present? && options['number_of_result'].to_i > 0
86
+ errors.add(:base, "Please provide 'number_of_result' to limit the result's number")
87
+ end
88
+ end
89
+
90
+ def working?
91
+ event_created_within?(options['expected_receive_period_in_days']) && !recent_error_logs?
92
+ end
93
+
94
+ def check
95
+ fetch
96
+ end
97
+
98
+ private
99
+
100
+ def fetch
101
+ uri = URI.parse("https://data.economie.gouv.fr/api/records/1.0/search/?dataset=rappelconso0&q=&rows=#{interpolated['number_of_result']}&sort=date_ref")
102
+ request = Net::HTTP::Get.new(uri)
103
+ request["Authority"] = "data.economie.gouv.fr"
104
+ request["Accept"] = "application/json, text/plain, */*"
105
+ # request["X-Csrftoken"] = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
106
+ request["User-Agent"] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.101 Safari/537.36"
107
+ request["Sec-Gpc"] = "1"
108
+ request["Sec-Fetch-Site"] = "same-origin"
109
+ request["Sec-Fetch-Mode"] = "cors"
110
+ request["Sec-Fetch-Dest"] = "empty"
111
+ request["Accept-Language"] = "fr,en-US;q=0.9,en;q=0.8"
112
+ # request["Cookie"] = "csrftoken=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
113
+
114
+ req_options = {
115
+ use_ssl: uri.scheme == "https",
116
+ }
117
+
118
+ response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
119
+ http.request(request)
120
+ end
121
+
122
+ log "request status : #{response.code}"
123
+
124
+ payload = JSON.parse(response.body)
125
+
126
+ if interpolated['debug'] == 'true'
127
+ log payload
128
+ end
129
+ if interpolated['changes_only'] == 'true'
130
+ if payload.to_s != memory['last_status']
131
+ if "#{memory['last_status']}" == ''
132
+ payload['records'].each do |recordid|
133
+ create_event payload: recordid
134
+ end
135
+ else
136
+ last_status = memory['last_status'].gsub("=>", ": ").gsub(": nil,", ": null,")
137
+ last_status = JSON.parse(last_status)
138
+ payload['records'].each do |recordid|
139
+ found = false
140
+ if interpolated['debug'] == 'true'
141
+ log "recordid"
142
+ log recordid
143
+ end
144
+ last_status['records'].each do |recordidbis|
145
+ if recordid['recordid'] == recordidbis['recordid']
146
+ found = true
147
+ end
148
+ if interpolated['debug'] == 'true'
149
+ log "recordidbis"
150
+ log recordidbis
151
+ log "found is #{found}!"
152
+ end
153
+ end
154
+ if found == false
155
+ if interpolated['debug'] == 'true'
156
+ log "found is #{found}! so event created"
157
+ log recordid
158
+ end
159
+ create_event payload: recordid
160
+ end
161
+ end
162
+ end
163
+ memory['last_status'] = payload.to_s
164
+ end
165
+ else
166
+ create_event payload: payload
167
+ if payload.to_s != memory['last_status']
168
+ memory['last_status'] = payload.to_s
169
+ end
170
+ end
171
+ end
172
+ end
173
+ end
@@ -0,0 +1,4 @@
1
+ require 'huginn_agent'
2
+
3
+ #HuginnAgent.load 'huginn_rappel_conso_gouv_agent/concerns/my_agent_concern'
4
+ HuginnAgent.register 'huginn_rappel_conso_gouv_agent/rappel_conso_gouv_agent'
@@ -0,0 +1,13 @@
1
+ require 'rails_helper'
2
+ require 'huginn_agent/spec_helper'
3
+
4
+ describe Agents::RappelConsoGouvAgent do
5
+ before(:each) do
6
+ @valid_options = Agents::RappelConsoGouvAgent.new.default_options
7
+ @checker = Agents::RappelConsoGouvAgent.new(:name => "RappelConsoGouvAgent", :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_rappel_conso_gouv_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: 2022-04-23 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_rappel_conso_gouv_agent.rb
64
+ - lib/huginn_rappel_conso_gouv_agent/rappel_conso_gouv_agent.rb
65
+ - spec/rappel_conso_gouv_agent_spec.rb
66
+ homepage: https://github.com/hihouhou/huginn_rappel_conso_gouv_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.1.6
86
+ signing_key:
87
+ specification_version: 4
88
+ summary: Write a short summary, because Rubygems requires one.
89
+ test_files:
90
+ - spec/rappel_conso_gouv_agent_spec.rb