huginn_strava_agent 0.1.0

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: a55c6b9aa8da90dfc00921d4abdb3d6d4d2fbb5eb4be4d900f2d6b190212cd2f
4
+ data.tar.gz: f0b692a9c4f0b4190bca1127548d7d8c47309c94577399e4d52696fbcc59baaa
5
+ SHA512:
6
+ metadata.gz: 6068f85287987fd86efd7596d8b2b926eca883375ae47c55b561dd86e47f358b3c5c51da2ab5f010717040f3a26527b9f80f4dbc3a6c36ac3172d73b18af7383
7
+ data.tar.gz: 256fca42c3a56f193a4c2f3bfb436f7b6772448c9cd24ceb2c0c46ffd020751c238d039a709b6a70cbf7c99411839925aab7e501309291ca79f22a8109edce26
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,283 @@
1
+ module Agents
2
+ class StravaAgent < 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 Strava Agent interacts with Strava's api.
11
+
12
+ The `type` can be like get_activities.
13
+
14
+ `debug` is used for verbose mode.
15
+
16
+ `bearer_token` is mandatory for authentication.
17
+
18
+ `refresh_token` is needed to refresh your token.
19
+
20
+ `client_id` is mandatory for authentication.
21
+
22
+ `client_secret` is mandatory for authentication.
23
+
24
+ `expected_receive_period_in_days` is used to determine if the Agent is working. Set it to the maximum number of days
25
+ that you anticipate passing without this Agent receiving an incoming Event.
26
+ MD
27
+ end
28
+
29
+ event_description <<-MD
30
+ Events look like this:
31
+
32
+ {
33
+ "resource_state": 2,
34
+ "athlete": {
35
+ "id": XXXXXXXXX,
36
+ "resource_state": 1
37
+ },
38
+ "name": "Afternoon Walk",
39
+ "distance": 5091.0,
40
+ "moving_time": 2454,
41
+ "elapsed_time": 2472,
42
+ "total_elevation_gain": 56.0,
43
+ "type": "Walk",
44
+ "sport_type": "Walk",
45
+ "id": 10406902206,
46
+ "start_date": "2023-11-11T13:45:53Z",
47
+ "start_date_local": "2023-11-11T14:45:53Z",
48
+ "timezone": "(GMT+01:00) Europe/Paris",
49
+ "utc_offset": 3600.0,
50
+ "location_city": null,
51
+ "location_state": null,
52
+ "location_country": "XXXXXX",
53
+ "achievement_count": 0,
54
+ "kudos_count": 0,
55
+ "comment_count": 0,
56
+ "athlete_count": 1,
57
+ "photo_count": 0,
58
+ "map": {
59
+ "id": "XXXXXXXXXXXX",
60
+ "summary_polyline": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
61
+ "resource_state": 2
62
+ },
63
+ "trainer": false,
64
+ "commute": false,
65
+ "manual": false,
66
+ "private": false,
67
+ "visibility": "followers_only",
68
+ "flagged": false,
69
+ "gear_id": null,
70
+ "start_latlng": [
71
+ XXXXXXXXXXXXXXXXX,
72
+ XXXXXXXXXXXXXXXXXX
73
+ ],
74
+ "end_latlng": [
75
+ XXXXXXXXXXXXXXXXX,
76
+ XXXXXXXXXXXXXXXXXX
77
+ ],
78
+ "average_speed": 2.075,
79
+ "max_speed": 2.466,
80
+ "average_cadence": 64.8,
81
+ "average_temp": 20,
82
+ "has_heartrate": false,
83
+ "heartrate_opt_out": false,
84
+ "display_hide_heartrate_option": false,
85
+ "elev_high": 69.2,
86
+ "elev_low": 23.4,
87
+ "upload_id": XXXXXXXXXXX,
88
+ "upload_id_str": "XXXXXXXXXXX",
89
+ "external_id": "XXXXXXXXXXXXXXXXXXXXXXXX",
90
+ "from_accepted_tag": false,
91
+ "pr_count": 0,
92
+ "total_photo_count": 0,
93
+ "has_kudoed": false
94
+ }
95
+ MD
96
+
97
+ def default_options
98
+ {
99
+ 'type' => 'get_activities',
100
+ 'client_id' => '',
101
+ 'client_secret' => '',
102
+ 'refresh_token' => '',
103
+ 'bearer_token' => '',
104
+ 'debug' => 'false',
105
+ 'expected_receive_period_in_days' => '2',
106
+ }
107
+ end
108
+
109
+ form_configurable :type, type: :array, values: ['token_refresh', 'get_activities']
110
+ form_configurable :client_id, type: :string
111
+ form_configurable :client_secret, type: :string
112
+ form_configurable :refresh_token, type: :string
113
+ form_configurable :bearer_token, type: :string
114
+ form_configurable :debug, type: :boolean
115
+ form_configurable :expected_receive_period_in_days, type: :string
116
+ def validate_options
117
+ errors.add(:base, "type has invalid value: should be 'token_refresh' 'get_activities'") if interpolated['type'].present? && !%w(token_refresh get_activities).include?(interpolated['type'])
118
+
119
+ unless options['client_id'].present?
120
+ errors.add(:base, "client_id is a required field")
121
+ end
122
+
123
+ unless options['client_secret'].present?
124
+ errors.add(:base, "client_secret is a required field")
125
+ end
126
+
127
+ unless options['refresh_token'].present?
128
+ errors.add(:base, "refresh_token is a required field")
129
+ end
130
+
131
+ unless options['bearer_token'].present?
132
+ errors.add(:base, "bearer_token is a required field")
133
+ end
134
+
135
+ if options.has_key?('debug') && boolify(options['debug']).nil?
136
+ errors.add(:base, "if provided, debug must be true or false")
137
+ end
138
+
139
+ unless options['expected_receive_period_in_days'].present? && options['expected_receive_period_in_days'].to_i > 0
140
+ 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")
141
+ end
142
+ end
143
+
144
+ def working?
145
+ event_created_within?(options['expected_receive_period_in_days']) && !recent_error_logs?
146
+ end
147
+
148
+ def receive(incoming_events)
149
+ incoming_events.each do |event|
150
+ interpolate_with(event) do
151
+ log event
152
+ trigger_action
153
+ end
154
+ end
155
+ end
156
+
157
+ def check
158
+ trigger_action
159
+ end
160
+
161
+ private
162
+
163
+ def log_curl_output(code,body)
164
+
165
+ log "request status : #{code}"
166
+
167
+ if interpolated['debug'] == 'true'
168
+ log "request status : #{code}"
169
+ log "body"
170
+ log body
171
+ end
172
+
173
+ end
174
+
175
+ def token_refresh()
176
+
177
+ uri = URI.parse("https://www.strava.com/api/v3/oauth/token")
178
+ request = Net::HTTP::Post.new(uri)
179
+ request.set_form_data(
180
+ "client_id" => interpolated['client_id'],
181
+ "client_secret" => interpolated['client_secret'],
182
+ "grant_type" => "refresh_token",
183
+ "refresh_token" => interpolated['refresh_token'],
184
+ )
185
+
186
+ req_options = {
187
+ use_ssl: uri.scheme == "https",
188
+ }
189
+
190
+ response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
191
+ http.request(request)
192
+ end
193
+
194
+ log_curl_output(response.code,response.body)
195
+
196
+ payload = JSON.parse(response.body)
197
+ memory['expires_at'] = payload['expires_at']
198
+
199
+ end
200
+
201
+ def check_token_validity()
202
+
203
+ timestamp_to_compare = memory['expires_at']
204
+
205
+ current_timestamp = Time.now.to_i
206
+
207
+ difference_in_hours = (timestamp_to_compare - current_timestamp) / 3600.0
208
+
209
+ if difference_in_hours < 2
210
+ token_refresh()
211
+ else
212
+ log "refresh not needed"
213
+ end
214
+
215
+ end
216
+
217
+ def get_activities()
218
+
219
+ check_token_validity()
220
+ uri = URI.parse("https://www.strava.com/api/v3/athlete/activities")
221
+ request = Net::HTTP::Get.new(uri)
222
+ request["Authorization"] = "Bearer #{interpolated['bearer_token']}"
223
+
224
+ req_options = {
225
+ use_ssl: uri.scheme == "https",
226
+ }
227
+
228
+ response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
229
+ http.request(request)
230
+ end
231
+
232
+ log_curl_output(response.code,response.body)
233
+
234
+ payload = JSON.parse(response.body)
235
+
236
+ if payload != memory['last_status']
237
+ payload.each do |activity|
238
+ found = false
239
+ if !memory['last_status'].nil? and memory['last_status']['activity'].present?
240
+ last_status = memory['last_status']
241
+ if interpolated['debug'] == 'true'
242
+ log "last_status"
243
+ log last_status
244
+ end
245
+ last_status.each do |activitybis|
246
+ if activity == activitybis
247
+ found = true
248
+ if interpolated['debug'] == 'true'
249
+ log "found is #{found}"
250
+ end
251
+ end
252
+ end
253
+ end
254
+ if found == false
255
+ create_event payload: activity
256
+ else
257
+ if interpolated['debug'] == 'true'
258
+ log "found is #{found}"
259
+ end
260
+ end
261
+ end
262
+ memory['last_status'] = payload
263
+ else
264
+ if interpolated['debug'] == 'true'
265
+ log "no diff"
266
+ end
267
+ end
268
+
269
+ end
270
+
271
+ def trigger_action
272
+
273
+ case interpolated['type']
274
+ when "get_activities"
275
+ get_activities()
276
+ when "token_refresh"
277
+ token_refresh()
278
+ else
279
+ log "Error: type has an invalid value (#{type})"
280
+ end
281
+ end
282
+ end
283
+ end
@@ -0,0 +1,4 @@
1
+ require 'huginn_agent'
2
+
3
+ #HuginnAgent.load 'huginn_strava_agent/concerns/my_agent_concern'
4
+ HuginnAgent.register 'huginn_strava_agent/strava_agent'
@@ -0,0 +1,13 @@
1
+ require 'rails_helper'
2
+ require 'huginn_agent/spec_helper'
3
+
4
+ describe Agents::StravaAgent do
5
+ before(:each) do
6
+ @valid_options = Agents::StravaAgent.new.default_options
7
+ @checker = Agents::StravaAgent.new(:name => "StravaAgent", :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_strava_agent
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Nicolas Germain
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-12-25 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_strava_agent.rb
64
+ - lib/huginn_strava_agent/strava_agent.rb
65
+ - spec/strava_agent_spec.rb
66
+ homepage: https://github.com/hihouhou/huginn_strava_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/strava_agent_spec.rb