huginn_swile_balance_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: 32ec50a29d2907388c8b48141864f98af34e6d76a05544779439d2f2b005c2c2
4
+ data.tar.gz: 745433d45bd7f0a699cb6470b2bbf4678fcb947cbf57049ff6fcbeeeef63906e
5
+ SHA512:
6
+ metadata.gz: a25fc0b32811b1dc10638d18cce8a0b68f66cc8a8799dfcf9bd3a563083467fd4ca5d8382423614d1b36a527a7fbbba601e4b302fc1dedee6066b5b2395611ce
7
+ data.tar.gz: fba4fe423564b88331e40824cc63ab9cd41515b60fb26683017705fdb75d7d9d094d8827253bc0c1ebdffeb47e6afab65a4c5b69cfe2931b8c4404af28cd6832
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,263 @@
1
+ module Agents
2
+ class SwileBalanceAgent < 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 huginn catalog agent checks the balance on Swile.
12
+
13
+ `debug` is used to verbose mode.
14
+
15
+ `bearer_token` is needed for auth.
16
+
17
+ `client_id` is needed for auth.
18
+
19
+ `refresh_token` is needed for auth (first launch).
20
+
21
+ `api_key` is needed for auth.
22
+
23
+ `changes_only` is only used to emit event about a currency's change.
24
+
25
+ `expected_receive_period_in_days` is used to determine if the Agent is working. Set it to the maximum number of days
26
+ that you anticipate passing without this Agent receiving an incoming Event.
27
+ MD
28
+ end
29
+
30
+ event_description <<-MD
31
+ Events look like this:
32
+
33
+ {
34
+ "id": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
35
+ "type": "meal_voucher",
36
+ "label": "Titres-resto",
37
+ "balance": {
38
+ "text": "540,00 €",
39
+ "value": 540
40
+ },
41
+ "giftType": null,
42
+ "networks": [
43
+ "meal_voucher_default_fr",
44
+ "meal_voucher_restaurant_fr"
45
+ ]
46
+ }
47
+ MD
48
+
49
+ def default_options
50
+ {
51
+ 'bearer_token' => '',
52
+ 'api_key' => '',
53
+ 'client_id' => '',
54
+ 'refresh_token' => '',
55
+ 'debug' => 'false',
56
+ 'expected_receive_period_in_days' => '2',
57
+ 'changes_only' => 'true'
58
+ }
59
+ end
60
+
61
+ form_configurable :expected_receive_period_in_days, type: :string
62
+ form_configurable :bearer_token, type: :string
63
+ form_configurable :api_key, type: :string
64
+ form_configurable :client_id, type: :string
65
+ form_configurable :refresh_token, type: :string
66
+ form_configurable :changes_only, type: :boolean
67
+ form_configurable :debug, type: :boolean
68
+
69
+ def validate_options
70
+ unless options['bearer_token'].present?
71
+ errors.add(:base, "bearer_token is a required field")
72
+ end
73
+
74
+ unless options['api_key'].present?
75
+ errors.add(:base, "api_key is a required field")
76
+ end
77
+
78
+ unless options['client_id'].present?
79
+ errors.add(:base, "client_id is a required field")
80
+ end
81
+
82
+ unless options['refresh_token'].present?
83
+ errors.add(:base, "refresh_token is a required field")
84
+ end
85
+
86
+ if options.has_key?('changes_only') && boolify(options['changes_only']).nil?
87
+ errors.add(:base, "if provided, changes_only must be true or false")
88
+ end
89
+
90
+ if options.has_key?('debug') && boolify(options['debug']).nil?
91
+ errors.add(:base, "if provided, debug must be true or false")
92
+ end
93
+
94
+ unless options['expected_receive_period_in_days'].present? && options['expected_receive_period_in_days'].to_i > 0
95
+ 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")
96
+ end
97
+ end
98
+
99
+ def working?
100
+ event_created_within?(options['expected_receive_period_in_days']) && !recent_error_logs?
101
+ end
102
+
103
+ def check
104
+ handle
105
+ end
106
+
107
+ private
108
+
109
+ def refresh(token)
110
+
111
+ uri = URI.parse("https://directory.swile.co/oauth/token")
112
+ request = Net::HTTP::Post.new(uri)
113
+ request.content_type = "application/json"
114
+ request["Authority"] = "directory.swile.co"
115
+ request["X-Lunchr-App-Version"] = "0.1.0"
116
+ request["Authorization"] = "Bearer #{interpolated['bearer_token']}"
117
+ request["Accept-Language"] = "fr"
118
+ request["X-Lunchr-Platform"] = "web"
119
+ request["User-Agent"] = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36"
120
+ request["X-Api-Key"] = interpolated['api_key']
121
+ request["Accept"] = "*/*"
122
+ request["Sec-Gpc"] = "1"
123
+ request["Origin"] = "https://team.swile.co"
124
+ request["Sec-Fetch-Site"] = "same-site"
125
+ request["Sec-Fetch-Mode"] = "cors"
126
+ request["Sec-Fetch-Dest"] = "empty"
127
+ request["Referer"] = "https://team.swile.co/"
128
+ request.body = JSON.dump({
129
+ "client_id" => interpolated['client_id'],
130
+ "grant_type" => "refresh_token",
131
+ "refresh_token" => token
132
+ })
133
+
134
+ req_options = {
135
+ use_ssl: uri.scheme == "https",
136
+ }
137
+
138
+ response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
139
+ http.request(request)
140
+ end
141
+
142
+ log "request status : #{response.code}"
143
+
144
+ if interpolated['debug'] == 'true'
145
+ log "response.body"
146
+ log response.body
147
+ end
148
+
149
+ if response.is_a?(Net::HTTPSuccess)
150
+ memory['last_refresh_token'] = response.body
151
+ else
152
+ log "refresh failed"
153
+ end
154
+
155
+ end
156
+
157
+ def handle
158
+
159
+ if "#{memory['last_refresh_token']}" == ''
160
+ used_token = interpolated['refresh_token']
161
+ bearer = interpolated['bearer_token']
162
+ refresh(used_token)
163
+ bearer = JSON.parse(memory['last_refresh_token'])['access_token']
164
+ else
165
+ used_token = JSON.parse(memory['last_refresh_token'])['refresh_token']
166
+ refresh(used_token)
167
+ bearer = JSON.parse(memory['last_refresh_token'])['access_token']
168
+ end
169
+ if interpolated['debug'] == 'true'
170
+ log "used_token #{used_token}"
171
+ log "bearer #{bearer}"
172
+ end
173
+
174
+ uri = URI.parse("https://neobank-api.swile.co/api/v0/wallets")
175
+ request = Net::HTTP::Get.new(uri)
176
+ request.content_type = "application/json"
177
+ request["User-Agent"] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/112.0"
178
+ request["Accept"] = "*/*"
179
+ request["Accept-Language"] = "fr"
180
+ request["Referer"] = "https://team.swile.co/"
181
+ request["X-Lunchr-Platform"] = "web"
182
+ request["Authorization"] = "Bearer #{bearer}"
183
+ request["X-Api-Key"] = interpolated['api_key']
184
+ request["Origin"] = "https://team.swile.co"
185
+ request["Connection"] = "keep-alive"
186
+ request["Sec-Fetch-Dest"] = "empty"
187
+ request["Sec-Fetch-Mode"] = "cors"
188
+ request["Sec-Fetch-Site"] = "same-site"
189
+ request["Dnt"] = "1"
190
+ request["Pragma"] = "no-cache"
191
+ request["Cache-Control"] = "no-cache"
192
+ request["Te"] = "trailers"
193
+
194
+ req_options = {
195
+ use_ssl: uri.scheme == "https",
196
+ }
197
+
198
+ response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
199
+ http.request(request)
200
+ end
201
+
202
+ log "request status : #{response.code}"
203
+
204
+ if interpolated['debug'] == 'true'
205
+ log "response.body"
206
+ log response.body
207
+ end
208
+
209
+ payload = JSON.parse(response.body)
210
+
211
+ if interpolated['debug'] == 'true'
212
+ log "payload"
213
+ log payload
214
+ end
215
+
216
+ if interpolated['changes_only'] == 'true'
217
+ if payload.to_s != memory['last_status']
218
+ if "#{memory['last_status']}" == ''
219
+ payload["wallets"].each do |wallet|
220
+ create_event payload: wallet
221
+ end
222
+ else
223
+ last_status = memory['last_status']
224
+ if interpolated['debug'] == 'true'
225
+ log "last_status"
226
+ log last_status
227
+ end
228
+
229
+ payload["wallets"].each do |wallet|
230
+ found = false
231
+ last_status["wallets"].each do |walletbis|
232
+ if wallet["id"] == walletbis["id"] && wallet["balance"] == walletbis["balance"]
233
+ found = true
234
+ if interpolated['debug'] == 'true'
235
+ log "found is #{found}"
236
+ end
237
+ end
238
+ end
239
+ if found == false
240
+ create_event payload: wallet
241
+ else
242
+ if interpolated['debug'] == 'true'
243
+ log "found is #{found}"
244
+ end
245
+
246
+ end
247
+ end
248
+ end
249
+ memory['last_status'] = payload
250
+ else
251
+ if interpolated['debug'] == 'true'
252
+ log "no diff"
253
+ end
254
+ end
255
+ else
256
+ create_event payload: payload
257
+ if payload != memory['last_status']
258
+ memory['last_status'] = payload
259
+ end
260
+ end
261
+ end
262
+ end
263
+ end
@@ -0,0 +1,4 @@
1
+ require 'huginn_agent'
2
+
3
+ #HuginnAgent.load 'huginn_swile_balance_agent/concerns/my_agent_concern'
4
+ HuginnAgent.register 'huginn_swile_balance_agent/swile_balance_agent'
@@ -0,0 +1,13 @@
1
+ require 'rails_helper'
2
+ require 'huginn_agent/spec_helper'
3
+
4
+ describe Agents::SwileBalanceAgent do
5
+ before(:each) do
6
+ @valid_options = Agents::SwileBalanceAgent.new.default_options
7
+ @checker = Agents::SwileBalanceAgent.new(:name => "SwileBalanceAgent", :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_swile_balance_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_swile_balance_agent.rb
64
+ - lib/huginn_swile_balance_agent/swile_balance_agent.rb
65
+ - spec/swile_balance_agent_spec.rb
66
+ homepage: https://github.com/hihouhou/huginn_swile_balance_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/swile_balance_agent_spec.rb