Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ build-iPhoneSimulator/

# for a library or gem, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# Gemfile.lock
Gemfile.lock
# .ruby-version
# .ruby-gemset

Expand Down
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ gemspec

gem 'rake', '~> 13.0'
gem 'nokogiri'
gem 'minitest'
39 changes: 0 additions & 39 deletions Gemfile.lock

This file was deleted.

4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ $ gem install tcxread
```ruby
require 'tcxread'

data = TCXRead.new('23.tcx')
data = TCXRead.load_file('23.tcx')

puts "Distance meters: #{data.total_distance_meters}, " \
"Time seconds: #{data.total_time_seconds}, " \
Expand All @@ -56,6 +56,8 @@ puts "Distance meters: #{data.total_distance_meters}, " \

```

Use `TCXRead.parse(data)` to parse raw TCX data.

## 💾 Datasets

Datasets available and used in the examples on the following links: [DATASET1](http://iztok-jr-fister.eu/static/publications/Sport5.zip), [DATASET2](http://iztok-jr-fister.eu/static/css/datasets/Sport.zip), [DATASET3](https://github.com/firefly-cpp/tcx-test-files).
Expand Down
23 changes: 17 additions & 6 deletions lib/tcxread.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,9 @@ class TCXRead
:average_speed_all, :average_speed_moving

# Initializes the TCXRead object and parses the TCX file.
# @param file_path [String] The file path of the TCX file to process.
def initialize(file_path)
@file_path = file_path
@doc = Nokogiri::XML(File.open(file_path))
# @param file_path_or_xml [String] The file path of the TCX file to process or a Nokogiri::XML document.
def initialize(file_path_or_xml)
@doc = file_path_or_xml.is_a?(Nokogiri::XML::Document) ? file_path_or_xml : Nokogiri::XML(File.open(file_path_or_xml))
@doc.root.add_namespace_definition('ns3', 'http://www.garmin.com/xmlschemas/ActivityExtension/v2')

@total_distance_meters = 0
Expand All @@ -66,6 +65,20 @@ def initialize(file_path)
parse
end

# Returns a TCXRead object from a File or file path.
# @param file_or_path [String, File] The file path or a Fule of the TCX file to process.
def self.load_file(file_or_path)
TCXRead.new(Nokogiri::XML(file_or_path.is_a?(File) ? file_or_path : File.open(file_or_path)))
end

# Returns a TCXRead object from raw TCX data.
# @param data [String] TCX data to load.
def self.parse(data)
TCXRead.new(Nokogiri::XML(data))
end

private

# Parses the TCX file and computes metrics for all activities.
def parse
activities = parse_activities
Expand All @@ -88,8 +101,6 @@ def parse
@average_speed_moving = speed_results[:average_speed_moving]
end

private

# Parses activities from the TCX file.
# @return [Array<Hash>] An array of parsed activity data.
def parse_activities
Expand Down
File renamed without changes.
File renamed without changes.
25 changes: 7 additions & 18 deletions test/test_tcxread.rb
Original file line number Diff line number Diff line change
@@ -1,41 +1,30 @@
require_relative '../lib/tcxread'
require 'minitest/autorun'
require 'rubygems'

class TCXReadTest < Minitest::Test
def setup
# tests for file 2.tcx
@data1 = TCXRead.new('test/2.tcx')

# tests for TCX file where watts exist
@data3 = TCXRead.new('test/23.tcx')

@data = TCXRead.new('test/fixtures/2.tcx')
end

def test_total_calories
assert_equal @data1.total_calories, 924
assert_equal @data.total_calories, 924
end

def test_total_distance
assert_equal @data1.total_distance_meters, 24732.34

assert_equal @data.total_distance_meters, 24732.34
end

def test_total_duration
assert_equal @data1.total_time_seconds, 3876.0
assert_equal @data.total_time_seconds, 3876.0
end

def test_total_ascent
assert_equal @data1.total_ascent, 452.5999946594238
assert_equal @data.total_ascent, 452.5999946594238
end

def test_NA_watts
assert_equal @data1.average_watts, 0.0
assert_equal @data1.max_watts, 0.0
assert_equal @data.average_watts, 0.0
assert_equal @data.max_watts, 0.0
end

def test_watts
assert_equal @data3.average_watts, 226.8091263216472
assert_equal @data3.max_watts, 587
end
end
12 changes: 12 additions & 0 deletions test/test_tcxread_load_file.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require_relative '../lib/tcxread'
require 'minitest/autorun'

class TCXReadTestLoadFile < Minitest::Test
def setup
@data = TCXRead.load_file('test/fixtures/2.tcx')
end

def test_total_distance
assert_equal @data.total_distance_meters, 24732.34
end
end
12 changes: 12 additions & 0 deletions test/test_tcxread_load_open_file.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require_relative '../lib/tcxread'
require 'minitest/autorun'

class TCXReadTestLoadOpenFile < Minitest::Test
def setup
@data = TCXRead.load_file(File.open('test/fixtures/2.tcx'))
end

def test_total_distance
assert_equal @data.total_distance_meters, 24732.34
end
end
12 changes: 12 additions & 0 deletions test/test_tcxread_parse.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require_relative '../lib/tcxread'
require 'minitest/autorun'

class TCXReadTestParse < Minitest::Test
def setup
@data = TCXRead.parse(File.read('test/fixtures/2.tcx'))
end

def test_total_distance
assert_equal @data.total_distance_meters, 24732.34
end
end
13 changes: 13 additions & 0 deletions test/test_tcxread_watts.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require_relative '../lib/tcxread'
require 'minitest/autorun'

class TCXReadTestWatts < Minitest::Test
def setup
@data_with_watts = TCXRead.new('test/fixtures/23.tcx')
end

def test_watts_present
assert_equal @data_with_watts.average_watts, 226.8091263216472
assert_equal @data_with_watts.max_watts, 587
end
end
Loading