diff --git a/.ruby-version b/.ruby-version index eca690e..e4604e3 100644 --- a/.ruby-version +++ b/.ruby-version @@ -1 +1 @@ -3.0.5 +3.2.1 diff --git a/Gemfile b/Gemfile index 9e0a6ac..be3b698 100644 --- a/Gemfile +++ b/Gemfile @@ -2,7 +2,7 @@ source 'https://rubygems.org' -ruby '3.0.5' +ruby '3.2.1' gem 'minitest', '~> 5.14' gem 'rake', '~> 13.0' diff --git a/Gemfile.lock b/Gemfile.lock index 2336e5c..58dc804 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -3,8 +3,8 @@ GEM specs: ast (2.4.2) minitest (5.23.1) - parallel (1.24.0) - parser (3.3.1.0) + parallel (1.25.1) + parser (3.3.3.0) ast (~> 2.4.1) racc psych (5.1.2) @@ -17,8 +17,8 @@ GEM rdoc (6.7.0) psych (>= 4.0.0) regexp_parser (2.9.2) - rexml (3.2.8) - strscan (>= 3.0.9) + rexml (3.3.0) + strscan rubocop (0.93.1) parallel (~> 1.10) parser (>= 2.7.1.5) @@ -33,7 +33,7 @@ GEM rubocop-minitest (0.27.0) rubocop (>= 0.90, < 2.0) ruby-progressbar (1.13.0) - stringio (3.1.0) + stringio (3.1.1) strscan (3.1.0) unicode-display_width (1.8.0) @@ -49,7 +49,7 @@ DEPENDENCIES rubocop-minitest (~> 0.11) RUBY VERSION - ruby 3.0.5p211 + ruby 3.2.1p31 BUNDLED WITH 2.5.10 diff --git a/README.md b/README.md index c873692..b3e4b7d 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ Calcpace is a Ruby gem designed to assist with calculations related to running a ### Add to your Gemfile ```ruby -gem 'calcpace', '~> 0.2.0' +gem 'calcpace', '~> 1.0.0' ``` Then run bundle install. @@ -20,12 +20,16 @@ gem install calcpace ### Usage + Before calculate or convert any value, you must create a new instance of Calcpace. When you call a method, it checks the digits of the time or distance to ensure that they are in the correct format. The gem always returns data using the same distance unit (kilometers or miles) that was used as input. + ### Calculate Pace To calculate pace, provide the total time (in HH:MM:SS format) and distance (in X.X format, representing kilometers or miles). ```ruby -Calcpace.new.pace('01:00:00', 12) # => "00:05:00" +calculate = Calcpace.new +calculate.pace('01:00:00', 12) # => "00:05:00" +calculate.pace('string', 12) # It must be a time (RuntimeError) ``` ### Calculate Total Time @@ -33,7 +37,9 @@ Calcpace.new.pace('01:00:00', 12) # => "00:05:00" To calculate total time, provide the pace (in HH:MM:SS format) and distance (in X.X format, representing kilometers or miles). ```ruby -Calcpace.new.total_time('00:05:00', 12) # => "01:00:00" +calculate = Calcpace.new +calculate.total_time('00:05:00', 12) # => "01:00:00" +calculate.total_time('00:05:00', 'string') # It must be a XX:XX:XX time (RuntimeError) ``` ### Calculate Distance @@ -49,29 +55,48 @@ Calcpace.new.distance('01:30:00', '00:05:00') # => 18.0 To convert distances, provide the distance and the unit of measurement (either 'km' for kilometers or 'mi' for miles). ```ruby -Calcpace.new.convert_distance(10, 'km') # => 6.21 -Calcpace.new.convert_distance(10, 'mi') # => 16.09 +converter = Calcpace.new +converter.convert(10, 'km') # => 6.21 +converter.convert_distance(10, 'mi') # => 16.09 ``` -### Other Useful Methods +If want to change the default round of the result (2), you can pass a second parameter to the method. + +```ruby +converter = Calcpace.new +converter.convert(10, 'km', 1) # => 6.2 +converter.convert(10, 'mi', 3) # => 16.093 +``` + +### Obtain the seconds of a time OR convert seconds to a time -Calcpace also provides other useful methods to convert values and check the validity of input. +To obtain the seconds of a time, provide the time in HH:MM:SS format. ```ruby -calcpace = Calcpace.new -calcpace.convert_to_seconds('01:00:00') # => 3600 -calcpace.convert_to_clock_time(3600) # => "01:00:00" -calcpace.check_digits_time('01:00:00') # => nil -calcpace.check_digits_time('01-00-00') # => It must be a XX:XX:XX time (RuntimeError) +converter = Calcpace.new +converter.to_seconds('01:00:00') # => 3600 +converter.to_seconds('string') # => It must be a time (RuntimeError) +converter.to_clocktime(3600) # => '01:00:00' +converter.to_clocktime('string') # => It must be a number (RuntimeError) ``` -### Error Handling +### Other Useful Methods + +Calcpace also provides other useful methods to check the validity of input. -If the provided parameters do not meet the expected formats, Calcpace will raise an error detailing the issue. The gem always returns data using the same distance unit (kilometers or miles) that was used as input. +```ruby +calcpace = Calcpace.new +calcpace.check_time('01:00:00') # => nil +calcpace.check_time('01-00-00') # => It must be a XX:XX:XX time (RuntimeError) +calcpace.check_distance(10) # => nil +calcpace.check_distance('-10') # => It must be a positive number (RuntimeError) +calcpace.check_unit('km') # => nil +calcpace.check_unit('string') # => It must be a valid unit (RuntimeError) +``` ## Contributing -We welcome contributions to Calcpace! To contribute, you can clone this repository and submit a pull request. Please ensure that your code adheres to our style guidelines and includes tests where appropriate. +We welcome contributions to Calcpace! To contribute, you can clone this repository and submit a pull request. Please ensure that your code adheres to our style and includes tests where appropriate. ## License diff --git a/calcpace.gemspec b/calcpace.gemspec index 9c8e50a..456c06d 100644 --- a/calcpace.gemspec +++ b/calcpace.gemspec @@ -2,7 +2,7 @@ Gem::Specification.new do |s| s.name = 'calcpace' - s.version = '0.2.0' + s.version = '1.0.0' s.summary = 'Calcpace: calculate time and distances for activities such as running and cycling.' s.description = 'Calculate pace, total time, distance and easily convert distances (kilometers and miles) for activities like running and cycling. Get readable results in HH:MM:SS or X.X format for time and distance calculations.' s.authors = ['Joao Gilberto Saraiva'] diff --git a/lib/calcpace/calculator.rb b/lib/calcpace/calculator.rb index 518b402..e71a5d3 100644 --- a/lib/calcpace/calculator.rb +++ b/lib/calcpace/calculator.rb @@ -2,24 +2,20 @@ module Calculator def pace(time, distance) - check_digits(time, distance) + check_time(time) + check_distance(distance) convert_to_clocktime(convert_to_seconds(time) / distance.to_f) end def total_time(pace, distance) - check_digits(pace, distance) + check_time(pace) + check_distance(distance) convert_to_clocktime(convert_to_seconds(pace) * distance.to_f) end def distance(time, pace) - check_digits_time(time) - check_digits_time(pace) + check_time(time) + check_time(pace) convert_to_seconds(time).to_f / convert_to_seconds(pace).round(2) end - - def convert_distance(distance, unit) - check_digits_distance(distance) - check_unit(unit) - convert(distance, unit) - end end diff --git a/lib/calcpace/checker.rb b/lib/calcpace/checker.rb index 6eb740d..4135bfd 100644 --- a/lib/calcpace/checker.rb +++ b/lib/calcpace/checker.rb @@ -1,19 +1,18 @@ # frozen_string_literal: true module Checker - def check_digits(time, distance) - check_digits_time(time) - check_digits_distance(distance) - end - - def check_digits_distance(distance) + def check_distance(distance) raise 'It must be a X.X positive number' unless distance.positive? end - def check_digits_time(time_string) + def check_time(time_string) raise 'It must be a XX:XX:XX time' unless time_string =~ /\d{0,2}(:)*?\d{1,2}(:)\d{1,2}/ end + def check_integer(second) + raise 'It must be a positive number' unless second.is_a?(Integer) && second.positive? + end + def check_unit(unit) raise 'It must be km or mi' unless %w[km mi].include?(unit) end diff --git a/lib/calcpace/converter.rb b/lib/calcpace/converter.rb index c9c2974..0d243a1 100644 --- a/lib/calcpace/converter.rb +++ b/lib/calcpace/converter.rb @@ -1,22 +1,39 @@ # frozen_string_literal: true module Converter + def to_seconds(time) + check_time(time) + convert_to_seconds(time) + end + + def to_clocktime(seconds) + check_integer(seconds) + convert_to_clocktime(seconds) + end + + def convert(distance, unit, round_limit = 2) + check_distance(distance) + check_unit(unit) + check_integer(round_limit) + convert_the_distance(distance, unit, round_limit) + end + def convert_to_seconds(time) hour, minute, seconds = time.split(':').map(&:to_i) (hour * 3600) + (minute * 60) + seconds end def convert_to_clocktime(seconds) - Time.at(seconds).utc.strftime('%H:%M:%S') + seconds >= 86_400 ? time = '%d %H:%M:%S' : time = '%H:%M:%S' + Time.at(seconds).utc.strftime(time) end - # tem que chamar as checagens de digitos antes de chamar o convert_distance - def convert(distance, unit) + def convert_the_distance(distance, unit, round_limit = 2) case unit when 'km' - (distance * 0.621371).round(2) + (distance * 0.621371).round(round_limit) when 'mi' - (distance * 1.60934).round(2) + (distance * 1.60934).round(round_limit) end end end diff --git a/test/calcpace/test_calculator.rb b/test/calcpace/test_calculator.rb index 16eb974..10a32ad 100644 --- a/test/calcpace/test_calculator.rb +++ b/test/calcpace/test_calculator.rb @@ -29,12 +29,4 @@ def test_distance assert_raises(RuntimeError) { @checker.distance('01:00:00', '') } assert_equal 18.0, @checker.distance('01:30:00', '00:05:00') end - - def test_convert_distance - assert_raises(RuntimeError) { @checker.convert_distance(10, 'invalid') } - assert_raises(RuntimeError) { @checker.convert_distance(-1, 'km') } - assert_raises(RuntimeError) { @checker.convert_distance(0, 'km') } - assert_equal 6.21, @checker.convert_distance(10, 'km') - assert_equal 16.09, @checker.convert_distance(10, 'mi') - end end diff --git a/test/calcpace/test_checker.rb b/test/calcpace/test_checker.rb index 3c32e71..1d7569a 100644 --- a/test/calcpace/test_checker.rb +++ b/test/calcpace/test_checker.rb @@ -8,25 +8,20 @@ def setup @checker = Calcpace.new end - def test_check_digits_distance - assert_raises(RuntimeError) { @checker.check_digits_distance(-1) } - assert_raises(RuntimeError) { @checker.check_digits_distance(0) } - assert_nil @checker.check_digits_distance(1) + def test_check_distance + assert_raises(RuntimeError) { @checker.check_distance(-1) } + assert_raises(RuntimeError) { @checker.check_distance(0) } + assert_nil @checker.check_distance(1) end - def test_check_digits_time - assert_raises(RuntimeError) { @checker.check_digits_time('') } - assert_nil @checker.check_digits_time('00:00:00') + def test_check_time + assert_raises(RuntimeError) { @checker.check_time('') } + assert_nil @checker.check_time('00:00:00') end - def test_check_digits - assert_raises(RuntimeError) { @checker.check_digits('00:00:00', 0) } - assert_raises(RuntimeError) { @checker.check_digits('', 1) } - assert_nil @checker.check_digits('00:00:00', 1) - end - - def test_convert - assert_equal 6.21, @checker.convert(10, 'km') - assert_equal 16.09, @checker.convert(10, 'mi') + def test_check_integer + assert_raises(RuntimeError) { @checker.check_integer(-1) } + assert_raises(RuntimeError) { @checker.check_integer(0) } + assert_nil @checker.check_integer(1) end end diff --git a/test/calcpace/test_converter.rb b/test/calcpace/test_converter.rb index c0b5664..57a400d 100644 --- a/test/calcpace/test_converter.rb +++ b/test/calcpace/test_converter.rb @@ -12,13 +12,38 @@ def test_convert_to_seconds assert_equal 4262, @checker.convert_to_seconds('01:11:02') end + def test_to_seconds + assert_equal 3600, @checker.to_seconds('01:00:00') + assert_raises(RuntimeError) { @checker.to_seconds('invalid') } + end + def test_convert_to_clocktime assert_equal '01:11:02', @checker.convert_to_clocktime(4262) end - def test_convert_distance_km - assert_equal 6.21, @checker.convert_distance(10, 'km') - assert_equal 16.09, @checker.convert_distance(10, 'mi') - assert_raises(RuntimeError) { @checker.convert_distance(10, 'invalid') } + def test_to_clocktime + assert_equal '01:00:00', @checker.to_clocktime(3600) + assert_equal '02 01:00:00', @checker.to_clocktime(90000) + assert_raises(RuntimeError) { @checker.to_clocktime(-1) } + assert_raises(RuntimeError) { @checker.to_clocktime(0) } + assert_raises(RuntimeError) { @checker.to_clocktime('invalid') } + end + + def test_convert_the_distance + assert_equal 6.21, @checker.convert_the_distance(10, 'km') + assert_equal 6.2, @checker.convert_the_distance(10, 'km', 1) + assert_equal 6.214, @checker.convert_the_distance(10, 'km', 3) + assert_equal 16.09, @checker.convert_the_distance(10, 'mi') + end + + def test_convert + assert_equal 6.21, @checker.convert(10, 'km') + assert_equal 16.09, @checker.convert(10, 'mi') + assert_equal 6.2, @checker.convert(10, 'km', 1) + assert_raises(RuntimeError) { @checker.convert(10, 'invalid') } + assert_raises(RuntimeError) { @checker.convert(-1, 'km') } + assert_raises(RuntimeError) { @checker.convert(10, 'km', -2) } + assert_raises(RuntimeError) { @checker.convert(10, 'km', 0) } + assert_raises(RuntimeError) { @checker.convert(10, 'km', 'invalid') } end end