diff --git a/.gitignore b/.gitignore index e3200e0..e93407a 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/Gemfile b/Gemfile index d1565e5..d327db3 100644 --- a/Gemfile +++ b/Gemfile @@ -6,3 +6,4 @@ gemspec gem 'rake', '~> 13.0' gem 'nokogiri' +gem 'minitest' \ No newline at end of file diff --git a/Gemfile.lock b/Gemfile.lock deleted file mode 100644 index 0da698c..0000000 --- a/Gemfile.lock +++ /dev/null @@ -1,39 +0,0 @@ -PATH - remote: . - specs: - tcxread (0.1.0) - nokogiri (~> 1.11) - -GEM - remote: https://rubygems.org/ - specs: - nokogiri (1.16.6-aarch64-linux) - racc (~> 1.4) - nokogiri (1.16.6-arm-linux) - racc (~> 1.4) - nokogiri (1.16.6-arm64-darwin) - racc (~> 1.4) - nokogiri (1.16.6-x86-linux) - racc (~> 1.4) - nokogiri (1.16.6-x86_64-darwin) - racc (~> 1.4) - nokogiri (1.16.6-x86_64-linux) - racc (~> 1.4) - racc (1.8.0) - rake (13.2.1) - -PLATFORMS - aarch64-linux - arm-linux - arm64-darwin - x86-linux - x86_64-darwin - x86_64-linux - -DEPENDENCIES - nokogiri - rake (~> 13.0) - tcxread! - -BUNDLED WITH - 2.5.11 diff --git a/README.md b/README.md index e2d0958..93cc0c4 100644 --- a/README.md +++ b/README.md @@ -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}, " \ @@ -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). diff --git a/lib/tcxread.rb b/lib/tcxread.rb index 07ee14b..37db23b 100644 --- a/lib/tcxread.rb +++ b/lib/tcxread.rb @@ -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 @@ -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 @@ -88,8 +101,6 @@ def parse @average_speed_moving = speed_results[:average_speed_moving] end - private - # Parses activities from the TCX file. # @return [Array] An array of parsed activity data. def parse_activities diff --git a/test/2.tcx b/test/fixtures/2.tcx similarity index 100% rename from test/2.tcx rename to test/fixtures/2.tcx diff --git a/test/23.tcx b/test/fixtures/23.tcx similarity index 100% rename from test/23.tcx rename to test/fixtures/23.tcx diff --git a/test/test_tcxread.rb b/test/test_tcxread.rb index 70b7478..68b8f8f 100644 --- a/test/test_tcxread.rb +++ b/test/test_tcxread.rb @@ -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 diff --git a/test/test_tcxread_load_file.rb b/test/test_tcxread_load_file.rb new file mode 100644 index 0000000..764dabf --- /dev/null +++ b/test/test_tcxread_load_file.rb @@ -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 diff --git a/test/test_tcxread_load_open_file.rb b/test/test_tcxread_load_open_file.rb new file mode 100644 index 0000000..fb4584a --- /dev/null +++ b/test/test_tcxread_load_open_file.rb @@ -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 diff --git a/test/test_tcxread_parse.rb b/test/test_tcxread_parse.rb new file mode 100644 index 0000000..b534bd3 --- /dev/null +++ b/test/test_tcxread_parse.rb @@ -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 diff --git a/test/test_tcxread_watts.rb b/test/test_tcxread_watts.rb new file mode 100644 index 0000000..d022b2d --- /dev/null +++ b/test/test_tcxread_watts.rb @@ -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