Skip to content

Commit ee0db41

Browse files
authored
Merge pull request #7 from dblock/load-data
Refactored TCXRead initializer into load_file and parse.
2 parents 9f99a78 + cc65ab3 commit ee0db41

12 files changed

Lines changed: 78 additions & 65 deletions

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ build-iPhoneSimulator/
4545

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

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ gemspec
66

77
gem 'rake', '~> 13.0'
88
gem 'nokogiri'
9+
gem 'minitest'

Gemfile.lock

Lines changed: 0 additions & 39 deletions
This file was deleted.

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ $ gem install tcxread
3838
```ruby
3939
require 'tcxread'
4040

41-
data = TCXRead.new('23.tcx')
41+
data = TCXRead.load_file('23.tcx')
4242

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

5757
```
5858

59+
Use `TCXRead.parse(data)` to parse raw TCX data.
60+
5961
## 💾 Datasets
6062

6163
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).

lib/tcxread.rb

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,9 @@ class TCXRead
4343
:average_speed_all, :average_speed_moving
4444

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

5251
@total_distance_meters = 0
@@ -66,6 +65,20 @@ def initialize(file_path)
6665
parse
6766
end
6867

68+
# Returns a TCXRead object from a File or file path.
69+
# @param file_or_path [String, File] The file path or a Fule of the TCX file to process.
70+
def self.load_file(file_or_path)
71+
TCXRead.new(Nokogiri::XML(file_or_path.is_a?(File) ? file_or_path : File.open(file_or_path)))
72+
end
73+
74+
# Returns a TCXRead object from raw TCX data.
75+
# @param data [String] TCX data to load.
76+
def self.parse(data)
77+
TCXRead.new(Nokogiri::XML(data))
78+
end
79+
80+
private
81+
6982
# Parses the TCX file and computes metrics for all activities.
7083
def parse
7184
activities = parse_activities
@@ -88,8 +101,6 @@ def parse
88101
@average_speed_moving = speed_results[:average_speed_moving]
89102
end
90103

91-
private
92-
93104
# Parses activities from the TCX file.
94105
# @return [Array<Hash>] An array of parsed activity data.
95106
def parse_activities
File renamed without changes.
File renamed without changes.

test/test_tcxread.rb

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,30 @@
11
require_relative '../lib/tcxread'
22
require 'minitest/autorun'
3-
require 'rubygems'
43

54
class TCXReadTest < Minitest::Test
65
def setup
7-
# tests for file 2.tcx
8-
@data1 = TCXRead.new('test/2.tcx')
9-
10-
# tests for TCX file where watts exist
11-
@data3 = TCXRead.new('test/23.tcx')
12-
6+
@data = TCXRead.new('test/fixtures/2.tcx')
137
end
148

159
def test_total_calories
16-
assert_equal @data1.total_calories, 924
10+
assert_equal @data.total_calories, 924
1711
end
1812

1913
def test_total_distance
20-
assert_equal @data1.total_distance_meters, 24732.34
21-
14+
assert_equal @data.total_distance_meters, 24732.34
2215
end
2316

2417
def test_total_duration
25-
assert_equal @data1.total_time_seconds, 3876.0
18+
assert_equal @data.total_time_seconds, 3876.0
2619
end
2720

2821
def test_total_ascent
29-
assert_equal @data1.total_ascent, 452.5999946594238
22+
assert_equal @data.total_ascent, 452.5999946594238
3023
end
3124

3225
def test_NA_watts
33-
assert_equal @data1.average_watts, 0.0
34-
assert_equal @data1.max_watts, 0.0
26+
assert_equal @data.average_watts, 0.0
27+
assert_equal @data.max_watts, 0.0
3528
end
3629

37-
def test_watts
38-
assert_equal @data3.average_watts, 226.8091263216472
39-
assert_equal @data3.max_watts, 587
40-
end
4130
end

test/test_tcxread_load_file.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
require_relative '../lib/tcxread'
2+
require 'minitest/autorun'
3+
4+
class TCXReadTestLoadFile < Minitest::Test
5+
def setup
6+
@data = TCXRead.load_file('test/fixtures/2.tcx')
7+
end
8+
9+
def test_total_distance
10+
assert_equal @data.total_distance_meters, 24732.34
11+
end
12+
end
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
require_relative '../lib/tcxread'
2+
require 'minitest/autorun'
3+
4+
class TCXReadTestLoadOpenFile < Minitest::Test
5+
def setup
6+
@data = TCXRead.load_file(File.open('test/fixtures/2.tcx'))
7+
end
8+
9+
def test_total_distance
10+
assert_equal @data.total_distance_meters, 24732.34
11+
end
12+
end

0 commit comments

Comments
 (0)