tcxread 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/tcxread.rb +43 -6
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: dc0fbb7ddc6ca33b8c42dd160de524513ed81570dcf9f82c2b28410af1e036c9
4
- data.tar.gz: 9ee1726006b6f3a267bd3cc72f6accae4c7d275058a0998febdb039d365d8b98
3
+ metadata.gz: 209745c7c980f590adf7944373f33951373b2c000d3a6a1bcfaed33b63cd59ad
4
+ data.tar.gz: 8abbe15da4a5a3ef688d531fa2d00c32dbfc19066a3c1baabe9de825202d6041
5
5
  SHA512:
6
- metadata.gz: a5fcce73beceff1cfd7dadfaf6c5c9afa4c8e599ac2be1cf09181d505140b2d48c464f981705b20cf98b808023cd5bf04d442bdebf671d3b4c529c18270ab3d5
7
- data.tar.gz: 9537cdffd7f6b8973ca497841e8b818b330f318fcbebee83a2120a87a75f6a6b797438f7f074579cb02b811a980968d6307907b552c42597d3da5ae52266009b
6
+ metadata.gz: 9c135f57bc1f24d640afdb59550301e491595652acafa18884d1df0b9f67547780aaffefd8f42cefec74ebb610aeeb8199b70697deff2f596fce1f4e601c3b92
7
+ data.tar.gz: de5960e78c0ba9a569778a17a066410fed531a07d359cbe1a7c5e85eea9edae8f3826ef721b410ce0223fd1f9cd32e1a327871f92bbe5c20b7d48e57d08881ab
data/lib/tcxread.rb CHANGED
@@ -4,16 +4,14 @@ require "nokogiri"
4
4
  # workout data such as activities, laps, tracks, trackpoints, and integral metrics.
5
5
  class TCXRead
6
6
  attr_reader :total_distance_meters, :total_time_seconds, :total_calories,
7
- :total_ascent, :total_descent, :max_altitude, :average_heart_rate
7
+ :total_ascent, :total_descent, :max_altitude, :average_heart_rate,
8
+ :max_watts, :average_watts
8
9
 
9
- # Initializes the TCXRead with the path to the TCX file.
10
- #
11
- # @param file_path [String] the path to the TCX file.
12
10
  def initialize(file_path)
13
11
  @file_path = file_path
14
12
  @doc = Nokogiri::XML(File.open(file_path))
13
+ @doc.root.add_namespace_definition('ns3', 'http://www.garmin.com/xmlschemas/ActivityExtension/v2')
15
14
 
16
- # Init the properties
17
15
  @total_distance_meters = 0
18
16
  @total_time_seconds = 0
19
17
  @total_calories = 0
@@ -21,6 +19,9 @@ class TCXRead
21
19
  @total_descent = 0
22
20
  @max_altitude = 0
23
21
  @average_heart_rate = 0
22
+ # use NA if no watts exist in the TCX file
23
+ @max_watts = 'NA'
24
+ @average_watts = 'NA'
24
25
 
25
26
  parse
26
27
  end
@@ -36,6 +37,7 @@ class TCXRead
36
37
  @total_calories = activities.sum { |activity| activity[:total_calories] }
37
38
  @total_ascent, @total_descent, @max_altitude = calculate_ascent_descent_and_max_altitude_from_activities(activities)
38
39
  @average_heart_rate = calculate_average_heart_rate_from_activities(activities)
40
+ @max_watts, @average_watts = calculate_watts_from_activities(activities)
39
41
  end
40
42
 
41
43
  { activities: activities }
@@ -112,7 +114,8 @@ class TCXRead
112
114
  distance_meters: trackpoint.xpath('xmlns:DistanceMeters').text.to_f,
113
115
  heart_rate: trackpoint.xpath('xmlns:HeartRateBpm/xmlns:Value').text.to_i,
114
116
  cadence: trackpoint.xpath('xmlns:Cadence').text.to_i,
115
- sensor_state: trackpoint.xpath('xmlns:SensorState').text
117
+ sensor_state: trackpoint.xpath('xmlns:SensorState').text,
118
+ watts: trackpoint.xpath('xmlns:Extensions/ns3:TPX/ns3:Watts').text.to_f
116
119
  }
117
120
  end
118
121
  tracks << trackpoints
@@ -165,6 +168,7 @@ class TCXRead
165
168
  [total_ascent, total_descent, max_altitude]
166
169
  end
167
170
 
171
+
168
172
  # Calculates the total ascent, total descent, and maximum altitude from the activities.
169
173
  #
170
174
  # @param activities [Array<Hash>] an array of activity hashes.
@@ -226,4 +230,37 @@ class TCXRead
226
230
 
227
231
  heart_rate_count > 0 ? total_heart_rate.to_f / heart_rate_count : 0.0
228
232
  end
233
+
234
+ # Calculates the maximum and average watts from the activities.
235
+ #
236
+ # @param activities [Array<Hash>] an array of activity hashes.
237
+ # @return [Array<Float, Float>] an array containing maximum watts and average watts. Returns 'NA' for both if no watts data is available.
238
+ def calculate_watts_from_activities(activities)
239
+ max_watts = 0
240
+ total_watts = 0
241
+ watts_count = 0
242
+
243
+ activities.each do |activity|
244
+ activity[:laps].each do |lap|
245
+ lap[:tracks].flatten.each do |trackpoint|
246
+ watts = trackpoint[:watts]
247
+ if watts > 0
248
+ total_watts += watts
249
+ watts_count += 1
250
+ max_watts = watts if watts > max_watts
251
+ end
252
+ end
253
+ end
254
+ end
255
+
256
+ if watts_count > 0
257
+ average_watts = total_watts.to_f / watts_count
258
+ max_watts = max_watts
259
+ else
260
+ average_watts = 'NA'
261
+ max_watts = 'NA'
262
+ end
263
+
264
+ [max_watts, average_watts]
265
+ end
229
266
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tcxread
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - firefly-cpp
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-07-20 00:00:00.000000000 Z
11
+ date: 2024-07-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nokogiri