tcx_rb 0.1.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b2cccf049adc64faead1fa408b9e08baac75b980f118f49f4792b9b33d52c51f
4
- data.tar.gz: 25ad6a3d9969a5ca4c31012003729155a0ed213d059db636180589a03c41e506
3
+ metadata.gz: 57eac59c633ebe544e4fbb134a527121d261f66755575d9cdd966865a39a29fd
4
+ data.tar.gz: ec031953f8875c10db3cb3c866621bd0ee452ac0030e1d5f344e7c68f7d27212
5
5
  SHA512:
6
- metadata.gz: ceca00ba0a2b0a2abd7fcf5a023cc4b3528dd80289893a2ee89ea06a70d7c27584ab416fcae8653e7262ef5482d07b4776877725d5099d3021faf063d61da228
7
- data.tar.gz: 43f78661f597204a7c3e8337a6a53cf7f1c3764887201c12f0de26438c7b384617482efa2423de6e2e19c714638f2ea57749f490d1179da5550d216eb167158d
6
+ metadata.gz: 5c3ba59454afd34ea5e01b2066e0c731401d2b18f4ef61b4ec280918a849b5b6769cb07badeb3575cfb26e6fcb83e7d74c3b09483f456eb3a02c4409b3ae5369
7
+ data.tar.gz: 4c7083b701bbe4164a9a731356326caea924ff5b3047b12a58aa24754e12f82104009f9825dc18fc4ce27155e1ecac4586cc9d73dec3ab6e5c90ae25cc7b75ca
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- tcx_rb (0.1.0)
4
+ tcx_rb (0.2.0)
5
5
  nokogiri (~> 1.10)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -1,8 +1,8 @@
1
- # TcxRb
1
+ [![Build Status](https://travis-ci.org/Kdoggett887/tcx_rb.svg?branch=master)](https://travis-ci.org/Kdoggett887/tcx_rb)
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/tcx_rb`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+ # TcxRb
4
4
 
5
- TODO: Delete this and the text above, and describe your gem
5
+ TcxRb is a ruby library for parsing Garmin TCX files into sensible Ruby objects.
6
6
 
7
7
  ## Installation
8
8
 
@@ -22,7 +22,137 @@ Or install it yourself as:
22
22
 
23
23
  ## Usage
24
24
 
25
- TODO: Write usage instructions here
25
+ The simplest way to start using TcxRb is to create a workout from a file.
26
+
27
+ ```ruby
28
+ workout = TcxRb.workout_from_file('/path/to/file')
29
+ # => TcxRb::Workout
30
+
31
+ workout.max_heart_rate
32
+ # => 181
33
+
34
+ workout.min_altitude
35
+ # => -3.17
36
+
37
+ workout.avg_pace
38
+ # => 3.027
39
+
40
+ workout.activities
41
+ # => [TcxRb::Activity]
42
+
43
+ workout.activities[0].laps
44
+ # => [TcxRb::Lap]
45
+
46
+ workout.activities[0].laps[0].trackpoints.size
47
+ # => 3762
48
+
49
+ workout.activities[0].laps[0].trackpoints[0].lat
50
+ # => 39.965614
51
+
52
+ workout.activities[0].laps[0].trackpoints[0].lon
53
+ # => -75.180849
54
+ ```
55
+
56
+ ### Types
57
+
58
+ The main classes in TcxRb are `Workout`, `Activity`, `Lap`, and `Trackpoint`.
59
+
60
+ #### Workout
61
+
62
+ `Workout` is the highest level object and its only attribute is `activities` which is an array of `Activity` objects.
63
+
64
+ `Workout` also implements `+` and `-`.
65
+
66
+ #### Activity
67
+
68
+ `Activity` is the highest level object in the tcx schema. It has the following attributes:
69
+
70
+ - `sport` - Sport detailed by the tracker (ex. Running).
71
+ - `id` - ID given by the tracker.
72
+ - `creator` - Name of the tracker.
73
+ - `laps` - Array of `Lap` objects.
74
+
75
+ #### Lap
76
+
77
+ `Lap` represents one "session" in the schema. It has a collection of trackpoints and distance associated with it. It has the following attributes:
78
+
79
+ - `start_time` - Start time of lap (string).
80
+ - `total_time` - Total time of the lap (float).
81
+ - `distance` - Total distance traveled in meters.
82
+ - `calories` - Total calories burned.
83
+ - `intensity` - Classification of lap intensity.
84
+ - `trigger_method` - Method used to start the lap.
85
+ - `trackpoints` - Array of `Trackpoint` objects.
86
+
87
+ #### Trackpoint
88
+
89
+ `Trackpoint` reprsents a sample from the tracker. Lowest level object in the hierarchy that contains the data that the other objects use for computations. It has the following attributes:
90
+
91
+ - `time` - Time the trackpoint was sampled (string).
92
+ - `latitude` - Latitude of the trackpoint.
93
+ - `longitude` - Longitude of the trackpoint.
94
+ - `altitude` - Height from sea level, in meters.
95
+ - `distance` - Total distance since the beginning of the workout, in meters.
96
+ - `heart_rate` - Heart rate the tracker sampled.
97
+ - `lat` - alias for latitude.
98
+ - `lon` - alias for longitude.
99
+
100
+ #### Parser
101
+
102
+ `Parser` is the class used to parse the tcx file into a `Workout`. A `parser` can be instansiated by passing in tcx string. It exposes the `parse_trackpoints`, `parse_laps`, and `parse_activities` methods which will return an array of hashes of the corresponding object.
103
+
104
+ Ex:
105
+
106
+ ```ruby
107
+ tcx_str = File.read('/path/to/data')
108
+ parser = TcxRb::Parser.new(tcx_str)
109
+ parser.parse_trackpoints
110
+
111
+ # => [{:time=>"2020-02-11T21:44:03.000-05:00", :latitude=>"39.965614", :longitude=>"-75.180849", :altitude=>"8.071420612508698", :distance=>"55.1", :heart_rate=>"146"}]
112
+ ```
113
+
114
+ ### Methods
115
+
116
+ Instances of `Workout`, `Activity`, and `Lap` all implement the following methods:
117
+
118
+ - `max_heart_rate` - Max heart rate of all trackpoints.
119
+ - `min_heart_rate` - Min heart rate of all trackpoints.
120
+ - `avg_heart_rate` - Avg heart rate of all trackpoints.
121
+ - `max_altitude` - Max altitude of all trackpoints.
122
+ - `min_altitude` - Min altitude of all trackpoints.
123
+ - `avg_altitude` - Avg altitude of all trackpoints.
124
+ - `max_pace` - Max pace of all pairs of trackpoints (m/s).
125
+ - `min_pace` - Min pace of all pairs of trackpoints (m/s).
126
+ - `avg_pace` - Avg pace of all pairs of trackpoitns (m/s).
127
+
128
+ Additionally, `Workout` and `Activity` implement the following:
129
+
130
+ - `total_tps` - The sum of the size of trackpoints contained in the child laps.
131
+ - `time` - The total "active" time of all the child laps (discounts time spent paused).
132
+ - `distance` - Total distance of all the child laps.
133
+ - `calories` - Sum of the calories burned in each child lap.
134
+
135
+ ### Examples
136
+
137
+ Example using `+` and `-` with `Workout`:
138
+
139
+ ```ruby
140
+ sat_workout = TcxRb.workout_from_file('saturday.tcx')
141
+ sun_workout = TcxRb.workout_from_file('sunday.tcx')
142
+
143
+ sat_workout.total_distance
144
+ # => 5000
145
+
146
+ sun_workout.total_distance
147
+ # => 10000
148
+
149
+ weekend_workouts = sat_workout + sun_workout
150
+ weekend_workouts.total_distance
151
+ # => 15000
152
+
153
+ (weekend_workouts - sat_workout).total_distance
154
+ # => 10000
155
+ ```
26
156
 
27
157
  ## Development
28
158
 
@@ -32,8 +162,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
32
162
 
33
163
  ## Contributing
34
164
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/tcx_rb.
36
-
165
+ Bug reports and pull requests are welcome on GitHub at https://github.com/kdoggett887/tcx_rb.
37
166
 
38
167
  ## License
39
168
 
@@ -9,5 +9,61 @@ module TcxRb
9
9
  @laps = args[:laps]
10
10
  end
11
11
  attr_accessor :sport, :id, :creator, :laps
12
+
13
+ def max_heart_rate
14
+ @laps.map(&:max_heart_rate).max
15
+ end
16
+
17
+ def min_heart_rate
18
+ @laps.map(&:min_heart_rate).min
19
+ end
20
+
21
+ def avg_heart_rate
22
+ # weighted average of each laps avg_heart_rate based on
23
+ # trackpoints size
24
+ total = total_tps.to_f
25
+ @laps.sum { |lap| (lap.trackpoints.size / total) * lap.avg_heart_rate }
26
+ end
27
+
28
+ def max_altitude
29
+ @laps.map(&:max_altitude).max
30
+ end
31
+
32
+ def min_altitude
33
+ @laps.map(&:min_altitude).min
34
+ end
35
+
36
+ def avg_altitude
37
+ total = total_tps.to_f
38
+ @laps.sum { |lap| (lap.trackpoints.size / total) * lap.avg_altitude }
39
+ end
40
+
41
+ def max_pace
42
+ @laps.map(&:max_pace).max
43
+ end
44
+
45
+ def min_pace
46
+ @laps.map(&:min_pace).min
47
+ end
48
+
49
+ def avg_pace
50
+ distance / time
51
+ end
52
+
53
+ def total_tps
54
+ @laps.sum { |lap| lap.trackpoints.size }
55
+ end
56
+
57
+ def time
58
+ @laps.sum(&:active_time)
59
+ end
60
+
61
+ def distance
62
+ @laps.sum(&:distance)
63
+ end
64
+
65
+ def calories
66
+ @laps.sum(&:calories)
67
+ end
12
68
  end
13
69
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module TcxRb
4
- VERSION = '0.1.0'
4
+ VERSION = '0.2.1'
5
5
  end
@@ -6,5 +6,67 @@ module TcxRb
6
6
  @activities = activities
7
7
  end
8
8
  attr_accessor :activities
9
+
10
+ def +(other)
11
+ Workout.new(@activities + other.activities)
12
+ end
13
+
14
+ def -(other)
15
+ Workout.new(@activities - other.activities)
16
+ end
17
+
18
+ def max_heart_rate
19
+ @activities.map(&:max_heart_rate).max
20
+ end
21
+
22
+ def min_heart_rate
23
+ @activities.map(&:min_heart_rate).min
24
+ end
25
+
26
+ def avg_heart_rate
27
+ total = total_tps.to_f
28
+ @activities.sum { |act| (act.total_tps / total) * act.avg_heart_rate }
29
+ end
30
+
31
+ def max_altitude
32
+ @activities.map(&:max_altitude).max
33
+ end
34
+
35
+ def min_altitude
36
+ @activities.map(&:min_altitude).min
37
+ end
38
+
39
+ def avg_altitude
40
+ total = total_tps.to_f
41
+ @activities.sum { |act| (act.total_tps / total) * act.avg_altitude }
42
+ end
43
+
44
+ def max_pace
45
+ @activities.map(&:max_pace).max
46
+ end
47
+
48
+ def min_pace
49
+ @activities.map(&:min_pace).min
50
+ end
51
+
52
+ def avg_pace
53
+ distance / time
54
+ end
55
+
56
+ def total_tps
57
+ @activities.sum(&:total_tps)
58
+ end
59
+
60
+ def time
61
+ @activities.sum(&:time)
62
+ end
63
+
64
+ def distance
65
+ @activities.sum(&:distance)
66
+ end
67
+
68
+ def calories
69
+ @activities.sum(&:calories)
70
+ end
9
71
  end
10
72
  end
data/lib/tcx_rb.rb CHANGED
@@ -11,17 +11,22 @@ module TcxRb
11
11
  class Error < StandardError; end
12
12
  # Your code goes here...
13
13
 
14
- def self.workout_from_tcx(tcx_str)
14
+ def self.workout_from_str(tcx_str)
15
15
  parser = TcxRb::Parser.new(tcx_str)
16
- workout_tree = parser.parse_activities
16
+ workout = parser.parse_activities
17
17
 
18
- activities = workout_tree.map do |activity|
18
+ activities = workout.map do |activity|
19
19
  activity[:laps] = generate_laps(activity[:laps])
20
20
  TcxRb::Activity.new(activity)
21
21
  end
22
22
  TcxRb::Workout.new(activities)
23
23
  end
24
24
 
25
+ def self.workout_from_file(path)
26
+ str = File.read(path)
27
+ workout_from_str(str)
28
+ end
29
+
25
30
  class << self
26
31
  private
27
32
 
data/tcx_rb.gemspec CHANGED
@@ -8,7 +8,7 @@ Gem::Specification.new do |spec|
8
8
  spec.authors = ['Keith Doggett']
9
9
  spec.email = ['keith.doggett887@gmail.com']
10
10
 
11
- spec.summary = 'TcxRB is a Garminc TCX parsing library for Ruby.'
11
+ spec.summary = 'TcxRB is a Garmin TCX parsing library for Ruby.'
12
12
  spec.description =
13
13
  'TcxRB is a Garmin TCX parsing library for Ruby. '\
14
14
  'It gives access to raw data and has helpful aggregate '\
@@ -17,8 +17,6 @@ Gem::Specification.new do |spec|
17
17
  spec.license = 'MIT'
18
18
  spec.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
19
19
 
20
- # spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
21
-
22
20
  spec.metadata['homepage_uri'] = spec.homepage
23
21
  spec.metadata['source_code_uri'] = spec.homepage
24
22
  # spec.metadata['changelog_uri'] = "TODO: Put your gem's CHANGELOG.md URL here."
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tcx_rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Keith Doggett
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-02-21 00:00:00.000000000 Z
11
+ date: 2020-02-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nokogiri
@@ -101,5 +101,5 @@ requirements: []
101
101
  rubygems_version: 3.0.3
102
102
  signing_key:
103
103
  specification_version: 4
104
- summary: TcxRB is a Garminc TCX parsing library for Ruby.
104
+ summary: TcxRB is a Garmin TCX parsing library for Ruby.
105
105
  test_files: []