Skip to content

Commit

Permalink
26 rename and built in inputs checks in methods to convert distances …
Browse files Browse the repository at this point in the history
…and time (#27)

* update ruby version to 3.2.1

* add method to check seconds

* add name friendly methods to convert distance, clocktime and seconds

* add handle to more than 24h in conversions of seconds

* add round option to user in convert

* update Readme and gemspec
  • Loading branch information
0jonjo authored Jun 16, 2024
1 parent 21aec44 commit 75c74a5
Show file tree
Hide file tree
Showing 11 changed files with 123 additions and 74 deletions.
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.0.5
3.2.1
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

source 'https://rubygems.org'

ruby '3.0.5'
ruby '3.2.1'

gem 'minitest', '~> 5.14'
gem 'rake', '~> 13.0'
Expand Down
12 changes: 6 additions & 6 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)

Expand All @@ -49,7 +49,7 @@ DEPENDENCIES
rubocop-minitest (~> 0.11)

RUBY VERSION
ruby 3.0.5p211
ruby 3.2.1p31

BUNDLED WITH
2.5.10
55 changes: 40 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -20,20 +20,26 @@ 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

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
Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion calcpace.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down
16 changes: 6 additions & 10 deletions lib/calcpace/calculator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
13 changes: 6 additions & 7 deletions lib/calcpace/checker.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
27 changes: 22 additions & 5 deletions lib/calcpace/converter.rb
Original file line number Diff line number Diff line change
@@ -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
8 changes: 0 additions & 8 deletions test/calcpace/test_calculator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
27 changes: 11 additions & 16 deletions test/calcpace/test_checker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
33 changes: 29 additions & 4 deletions test/calcpace/test_converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 75c74a5

Please sign in to comment.