tcx_rb 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: b2cccf049adc64faead1fa408b9e08baac75b980f118f49f4792b9b33d52c51f
4
+ data.tar.gz: 25ad6a3d9969a5ca4c31012003729155a0ed213d059db636180589a03c41e506
5
+ SHA512:
6
+ metadata.gz: ceca00ba0a2b0a2abd7fcf5a023cc4b3528dd80289893a2ee89ea06a70d7c27584ab416fcae8653e7262ef5482d07b4776877725d5099d3021faf063d61da228
7
+ data.tar.gz: 43f78661f597204a7c3e8337a6a53cf7f1c3764887201c12f0de26438c7b384617482efa2423de6e2e19c714638f2ea57749f490d1179da5550d216eb167158d
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /test/data/full_data.tcx
9
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,8 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.7
6
+ - 2.6
7
+ - 2.5
8
+ before_install: gem install bundler -v 2.1.4
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in tcx_rb.gemspec
4
+ gemspec
5
+
data/Gemfile.lock ADDED
@@ -0,0 +1,25 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ tcx_rb (0.1.0)
5
+ nokogiri (~> 1.10)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ mini_portile2 (2.4.0)
11
+ minitest (5.14.0)
12
+ nokogiri (1.10.8)
13
+ mini_portile2 (~> 2.4.0)
14
+ rake (12.3.3)
15
+
16
+ PLATFORMS
17
+ ruby
18
+
19
+ DEPENDENCIES
20
+ minitest (~> 5.0)
21
+ rake (~> 12.0)
22
+ tcx_rb!
23
+
24
+ BUNDLED WITH
25
+ 2.1.4
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Keith Doggett
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,40 @@
1
+ # TcxRb
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.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'tcx_rb'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install tcx_rb
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/tcx_rb.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "tcx_rb"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TcxRb
4
+ class Activity
5
+ def initialize(args = {})
6
+ @sport = args[:sport]
7
+ @id = args[:id]
8
+ @creator = args[:creator]
9
+ @laps = args[:laps]
10
+ end
11
+ attr_accessor :sport, :id, :creator, :laps
12
+ end
13
+ end
data/lib/tcx_rb/lap.rb ADDED
@@ -0,0 +1,101 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'time'
4
+
5
+ module TcxRb
6
+ class Lap
7
+ def initialize(args = {})
8
+ @start_time = args[:start_time].to_s
9
+ @total_time = args[:total_time].to_f
10
+ @distance = args[:distance].to_f
11
+ @calories = args[:calories].to_i
12
+ @intensity = args[:intensity]
13
+ @trigger_method = args[:trigger_method]
14
+ @trackpoints = args[:trackpoints]
15
+ end
16
+ attr_accessor :start_time, :total_time, :distance, :calories, :intensity,
17
+ :trigger_method, :trackpoints
18
+
19
+ def max_heart_rate
20
+ @trackpoints.map(&:heart_rate).max
21
+ end
22
+
23
+ def min_heart_rate
24
+ @trackpoints.map(&:heart_rate).min
25
+ end
26
+
27
+ def avg_heart_rate
28
+ tot = @trackpoints.inject(0.0) { |sum, el| sum + el.heart_rate }
29
+ tot / @trackpoints.size
30
+ end
31
+
32
+ def max_altitude
33
+ @trackpoints.map(&:altitude).max
34
+ end
35
+
36
+ def min_altitude
37
+ @trackpoints.map(&:altitude).min
38
+ end
39
+
40
+ def avg_altitude
41
+ tot = @trackpoints.inject(0.0) { |sum, el| sum + el.altitude }
42
+ tot / @trackpoints.size
43
+ end
44
+
45
+ def max_pace
46
+ max = 0.0
47
+ @trackpoints.each_with_index do |tp, i|
48
+ # skip first cause we need to refrence previous
49
+ # and if the current trackpoint is 0.0 in distance,
50
+ # we will skip it because that means we're stopped
51
+ next if i.zero? || tp.distance.zero?
52
+
53
+ prev_tp = @trackpoints[i - 1]
54
+
55
+ # also skip to the next one if i isn't 1 and the previous is 0
56
+ # because we could get a ridiculously high pace.
57
+ next if i != 1 && prev_tp.distance.zero?
58
+
59
+ # now we can perform the calculation
60
+ d_dist = tp.distance - prev_tp.distance
61
+ d_t = Time.parse(tp.time) - Time.parse(prev_tp.time)
62
+ pace = d_dist / d_t
63
+ max = pace if pace > max
64
+ end
65
+ max
66
+ end
67
+
68
+ def min_pace
69
+ min = Float::INFINITY
70
+ @trackpoints.each_with_index do |tp, i|
71
+ next if i.zero? || tp.distance.zero?
72
+
73
+ prev_tp = @trackpoints[i - 1]
74
+ next if i != 1 && prev_tp.distance.zero?
75
+
76
+ d_dist = tp.distance - prev_tp.distance
77
+ d_t = Time.parse(tp.time) - Time.parse(prev_tp.time)
78
+ pace = d_dist / d_t
79
+ min = pace if pace < min
80
+ end
81
+ min
82
+ end
83
+
84
+ def active_time
85
+ # aggregation of time spent moving
86
+ active = 0.0
87
+ @trackpoints.each_with_index do |tp, i|
88
+ next if i.zero? || tp.distance.zero?
89
+
90
+ prev_tp = @trackpoints[i - 1]
91
+ d_t = Time.parse(tp.time) - Time.parse(prev_tp.time)
92
+ active += d_t
93
+ end
94
+ active
95
+ end
96
+
97
+ def avg_pace
98
+ distance / active_time
99
+ end
100
+ end
101
+ end
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'nokogiri'
4
+
5
+ module TcxRb
6
+ class Parser
7
+ def initialize(tcx_str)
8
+ @doc = Nokogiri::XML(tcx_str)
9
+ end
10
+ attr_reader :doc
11
+
12
+ def parse_trackpoints(doc = @doc)
13
+ # parse trackpoints from document and return hashes in the form of
14
+ # {time, latitude, longitude, altitude, distance, heart_rate}
15
+ doc.css('Trackpoint').map do |tp|
16
+ tp_hash = {}
17
+ tp_hash[:time] = tp.css('Time').text
18
+ tp_hash[:latitude] = tp.css('Position/LatitudeDegrees').text
19
+ tp_hash[:longitude] = tp.css('Position/LongitudeDegrees').text
20
+ tp_hash[:altitude] = tp.css('AltitudeMeters').text
21
+ tp_hash[:distance] = tp.css('DistanceMeters').text
22
+ tp_hash[:heart_rate] = tp.css('HeartRateBpm/Value').text
23
+ tp_hash
24
+ end
25
+ end
26
+
27
+ def parse_laps(doc = @doc)
28
+ # parse laps from document and returns hashes in the form of
29
+ # {start_time, total_time, distance, calories, intensity, trigger_method,
30
+ # trackpoints}
31
+ doc.css('Lap').map do |lap|
32
+ lap_hash = {}
33
+ lap_hash[:start_time] = lap['StartTime']
34
+ lap_hash[:total_time] = lap.css('TotalTimeSeconds').text
35
+ lap_hash[:distance] = lap.css('DistanceMeters').first.text
36
+ lap_hash[:calories] = lap.css('Calories').text
37
+ lap_hash[:intensity] = lap.css('Intensity').text
38
+ lap_hash[:trigger_method] = lap.css('TriggerMethod').text
39
+ lap_hash[:trackpoints] = parse_trackpoints(lap)
40
+ lap_hash
41
+ end
42
+ end
43
+
44
+ def parse_activities
45
+ doc.css('Activity').map do |activity|
46
+ act_hash = {}
47
+ act_hash[:sport] = activity['Sport']
48
+ act_hash[:id] = activity.css('Id').text
49
+ act_hash[:creator] = activity.css('Creator/Name').text
50
+ act_hash[:laps] = parse_laps(activity)
51
+ act_hash
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'time'
4
+
5
+ module TcxRb
6
+ class Trackpoint
7
+ def initialize(args = {})
8
+ @time = args[:time].to_s
9
+ @latitude = args[:latitude].to_f
10
+ @longitude = args[:longitude].to_f
11
+ @altitude = args[:altitude].to_f
12
+ @distance = args[:distance].to_f
13
+ @heart_rate = args[:heart_rate].to_i
14
+ end
15
+ attr_accessor :time, :latitude, :longitude, :altitude, :distance, :heart_rate
16
+ alias lat latitude
17
+ alias lon longitude
18
+ end
19
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TcxRb
4
+ VERSION = '0.1.0'
5
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TcxRb
4
+ class Workout
5
+ def initialize(activities = [])
6
+ @activities = activities
7
+ end
8
+ attr_accessor :activities
9
+ end
10
+ end
data/lib/tcx_rb.rb ADDED
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'tcx_rb/version'
4
+ require 'tcx_rb/parser'
5
+ require 'tcx_rb/trackpoint'
6
+ require 'tcx_rb/lap'
7
+ require 'tcx_rb/activity'
8
+ require 'tcx_rb/workout'
9
+
10
+ module TcxRb
11
+ class Error < StandardError; end
12
+ # Your code goes here...
13
+
14
+ def self.workout_from_tcx(tcx_str)
15
+ parser = TcxRb::Parser.new(tcx_str)
16
+ workout_tree = parser.parse_activities
17
+
18
+ activities = workout_tree.map do |activity|
19
+ activity[:laps] = generate_laps(activity[:laps])
20
+ TcxRb::Activity.new(activity)
21
+ end
22
+ TcxRb::Workout.new(activities)
23
+ end
24
+
25
+ class << self
26
+ private
27
+
28
+ def generate_laps(laps)
29
+ laps.map do |lap|
30
+ lap[:trackpoints] = generate_trackpoints(lap[:trackpoints])
31
+ TcxRb::Lap.new(lap)
32
+ end
33
+ end
34
+
35
+ def generate_trackpoints(trackpoints)
36
+ trackpoints.map do |tp|
37
+ TcxRb::Trackpoint.new(tp)
38
+ end
39
+ end
40
+ end
41
+ end
data/tcx_rb.gemspec ADDED
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/tcx_rb/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'tcx_rb'
7
+ spec.version = TcxRb::VERSION
8
+ spec.authors = ['Keith Doggett']
9
+ spec.email = ['keith.doggett887@gmail.com']
10
+
11
+ spec.summary = 'TcxRB is a Garminc TCX parsing library for Ruby.'
12
+ spec.description =
13
+ 'TcxRB is a Garmin TCX parsing library for Ruby. '\
14
+ 'It gives access to raw data and has helpful aggregate '\
15
+ 'functions for groups of data.'
16
+ spec.homepage = 'https://github.com/Kdoggett887/tcx_rb'
17
+ spec.license = 'MIT'
18
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
19
+
20
+ # spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
21
+
22
+ spec.metadata['homepage_uri'] = spec.homepage
23
+ spec.metadata['source_code_uri'] = spec.homepage
24
+ # spec.metadata['changelog_uri'] = "TODO: Put your gem's CHANGELOG.md URL here."
25
+
26
+ # Specify which files should be added to the gem when it is released.
27
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
28
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
29
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
30
+ end
31
+
32
+ spec.add_dependency 'nokogiri', '~>1.10'
33
+ spec.add_development_dependency 'minitest', '~>5.0'
34
+ spec.add_development_dependency 'rake', '~>12.0'
35
+
36
+ spec.bindir = 'exe'
37
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
38
+ spec.require_paths = ['lib']
39
+ end
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tcx_rb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Keith Doggett
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-02-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: nokogiri
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: minitest
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '5.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '5.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '12.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '12.0'
55
+ description: TcxRB is a Garmin TCX parsing library for Ruby. It gives access to raw
56
+ data and has helpful aggregate functions for groups of data.
57
+ email:
58
+ - keith.doggett887@gmail.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - ".travis.yml"
65
+ - Gemfile
66
+ - Gemfile.lock
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - bin/console
71
+ - bin/setup
72
+ - lib/tcx_rb.rb
73
+ - lib/tcx_rb/activity.rb
74
+ - lib/tcx_rb/lap.rb
75
+ - lib/tcx_rb/parser.rb
76
+ - lib/tcx_rb/trackpoint.rb
77
+ - lib/tcx_rb/version.rb
78
+ - lib/tcx_rb/workout.rb
79
+ - tcx_rb.gemspec
80
+ homepage: https://github.com/Kdoggett887/tcx_rb
81
+ licenses:
82
+ - MIT
83
+ metadata:
84
+ homepage_uri: https://github.com/Kdoggett887/tcx_rb
85
+ source_code_uri: https://github.com/Kdoggett887/tcx_rb
86
+ post_install_message:
87
+ rdoc_options: []
88
+ require_paths:
89
+ - lib
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: 2.3.0
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ requirements: []
101
+ rubygems_version: 3.0.3
102
+ signing_key:
103
+ specification_version: 4
104
+ summary: TcxRB is a Garminc TCX parsing library for Ruby.
105
+ test_files: []