Skip to content

Commit

Permalink
23 create basic gem structure (#24)
Browse files Browse the repository at this point in the history
* create the build for the calcpace gem

* refactor run convert to use as a module

* refactor class and modules to be in a gem structure

* switch rspec to minitest

* refactor tests in a minitest way

* rubocop minor changes

* remove manual tests

* remove old specs

* update gemfile

* update gemspec to 0.2.0 version

* ignore and remove old stuff

* update tests workflow

* adjust rakefile to fix pipeline tests

* add testtask reference

* add bundle exec to test pipeline

* remove test word of exec rake

* update readme with gem basic information
  • Loading branch information
0jonjo authored May 26, 2024
1 parent 35f4527 commit 18d0f2b
Show file tree
Hide file tree
Showing 23 changed files with 295 additions and 284 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ jobs:
with:
bundler-cache: true
- name: Run tests
run: bundle exec rspec
run: bundle exec rake
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/calcpace-*
2 changes: 0 additions & 2 deletions .rspec

This file was deleted.

7 changes: 6 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,9 @@ source 'https://rubygems.org'

ruby '3.0.5'

gem 'rspec', '~> 3.10'
gem 'minitest', '~> 5.14'
gem 'rake', '~> 13.0'
gem 'rake-compiler', '~> 1.0'
gem 'rdoc', '~> 6.2'
gem 'rubocop', '~> 0.79'
gem 'rubocop-minitest', '~> 0.11'
58 changes: 42 additions & 16 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,29 +1,55 @@
GEM
remote: https://rubygems.org/
specs:
diff-lcs (1.5.0)
rspec (3.11.0)
rspec-core (~> 3.11.0)
rspec-expectations (~> 3.11.0)
rspec-mocks (~> 3.11.0)
rspec-core (3.11.0)
rspec-support (~> 3.11.0)
rspec-expectations (3.11.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.11.0)
rspec-mocks (3.11.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.11.0)
rspec-support (3.11.0)
ast (2.4.2)
minitest (5.23.1)
parallel (1.24.0)
parser (3.3.1.0)
ast (~> 2.4.1)
racc
psych (5.1.2)
stringio
racc (1.8.0)
rainbow (3.1.1)
rake (13.2.1)
rake-compiler (1.2.7)
rake
rdoc (6.7.0)
psych (>= 4.0.0)
regexp_parser (2.9.2)
rexml (3.2.8)
strscan (>= 3.0.9)
rubocop (0.93.1)
parallel (~> 1.10)
parser (>= 2.7.1.5)
rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 1.8)
rexml
rubocop-ast (>= 0.6.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 1.4.0, < 2.0)
rubocop-ast (1.31.3)
parser (>= 3.3.1.0)
rubocop-minitest (0.27.0)
rubocop (>= 0.90, < 2.0)
ruby-progressbar (1.13.0)
stringio (3.1.0)
strscan (3.1.0)
unicode-display_width (1.8.0)

PLATFORMS
ruby

DEPENDENCIES
rspec (~> 3.10)
minitest (~> 5.14)
rake (~> 13.0)
rake-compiler (~> 1.0)
rdoc (~> 6.2)
rubocop (~> 0.79)
rubocop-minitest (~> 0.11)

RUBY VERSION
ruby 3.0.5p211

BUNDLED WITH
2.4.7
2.5.10
75 changes: 35 additions & 40 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,65 +1,60 @@
# CALCPACE
# Calcpace [![Gem Version](https://badge.fury.io/rb/calcpace.svg)](https://badge.fury.io/rb/calcpace)

A TDD Ruby study project to calculate the running pace, the predicted running time or distance. It's based on my original [Python Calcpace](https://github.com/0jonjo/calcpace-py).
Calcpace is a Ruby gem designed to assist with calculations related to running and cycling activities. It can calculate pace, total time, and distance, and also convert distances between miles and kilometers. Results are provided in a readable format, with times in HH:MM:SS and distances in X.X format.

## Install
## Installation

### Clone the repository
### Add to your Gemfile

```shell
git clone [email protected]:0jonjo/ruby-calcpace.git
cd ruby-calcpace
```ruby
gem 'calcpace', '~> 0.2.0'
```

### Install dependencies
Then run bundle install.

Using [Bundler](https://github.com/bundler/bundler)
### Usage

```shell
bundle install
```

## Use the calculator
### Calculate Pace

Use main.rb informs the calculation option and the running (jogging) data.
To calculate pace, provide the total time (in HH:MM:SS format) and distance (in X.X format, representing kilometers or miles).

Choose p to calculate pace and enter running time (HH:MM:SS) and distance (X.X) in kilometers or miles.

```shell
ruby lib/main.rb p 01:00:00 10
00:06:00
```ruby
Calcpace.new.pace('01:00:00', 12) # => "00:05:00"
```

Choose t to calculate running time and enter pace (HH:MM:SS) and distance (X.X) in kilometers or miles.
### 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).

```shell
ruby lib/main.rb t 00:05:00 12
01:00:00
```ruby
Calcpace.new.total_time('00:05:00', 12) # => "01:00:00"
```

Choose d to calculate distance in kilometers or miles and enter running time (HH:MM:SS) and pace (HH:MM:SS).
### Calculate Distance

```shell
ruby lib/main.rb d 01:30:00 00:05:00
18.0
To calculate distance, provide the running time (in HH:MM:SS format) and pace (in HH:MM:SS format).

```ruby
Calcpace.new.distance('01:30:00', '00:05:00') # => 18.0
```

Choose c to convert distance.
### Convert Distances

Enter km and the distance in kilometers to convert to miles.
To convert distances, provide the distance and the unit of measurement (either 'km' for kilometers or 'mi' for miles).

```shell
ruby lib/main.rb c km 10
6.21
```ruby
Calcpace.new.convert_distance(10, 'km') # => 6.21
Calcpace.new.convert_distance(10, 'mi') # => 16.09
```

Enter mi and the distance in miles to convert to kilometers.
### Error Handling

```shell
ruby lib/main.rb c mi 10
16.09
```
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.

## 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.

If some of the parameters are not in the standard that the program uses, it informs what the problem is in an error.
## License

It's important to input data using the same standard (kilometers or miles) to obtain the correct result.
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
11 changes: 11 additions & 0 deletions Rakefile.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

require "minitest/test_task"

Minitest::TestTask.create(:test) do |t|
t.libs << "test"
t.libs << "lib"
t.warning = false
end

task :default => :test
23 changes: 23 additions & 0 deletions calcpace.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

Gem::Specification.new do |s|
s.name = 'calcpace'
s.version = '0.2.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']
s.email = '[email protected]'
s.files = ['lib/calcpace.rb', 'lib/calcpace/calculator.rb', 'lib/calcpace/checker.rb', 'lib/calcpace/converter.rb']
s.test_files = ['test/calcpace/test_calculator.rb', 'test/calcpace/test_checker.rb', 'test/calcpace/test_converter.rb']
s.add_development_dependency 'minitest', '~> 5.14'
s.add_development_dependency 'rake', '~> 13.0'
s.add_development_dependency 'rake-compiler', '~> 1.0'
s.add_development_dependency 'rdoc', '~> 6.2'
s.add_development_dependency 'rubocop', '~> 0.79'
s.add_development_dependency 'rubocop-minitest', '~> 0.11'
s.required_ruby_version = '>= 2.7.0'
s.post_install_message = "It's time to grab your sneakers or hop on your bike and start exercising! Thank you for installing Calcpace!"
s.metadata = { 'source_code_uri' => 'https://github.com/0jonjo/calcpace' }
s.homepage = 'https://github.com/0jonjo/calcpace'
s.license = 'MIT'
end
13 changes: 13 additions & 0 deletions lib/calcpace.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

require_relative 'calcpace/calculator'
require_relative 'calcpace/checker'
require_relative 'calcpace/converter'

class Calcpace
include Calculator
include Checker
include Converter

def initialize; end
end
25 changes: 25 additions & 0 deletions lib/calcpace/calculator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true

module Calculator
def pace(time, distance)
check_digits(time, distance)
convert_to_clocktime(convert_to_seconds(time) / distance.to_f)
end

def total_time(pace, distance)
check_digits(pace, distance)
convert_to_clocktime(convert_to_seconds(pace) * distance.to_f)
end

def distance(time, pace)
check_digits_time(time)
check_digits_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
20 changes: 20 additions & 0 deletions lib/calcpace/checker.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# 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)
raise 'It must be a X.X positive number' unless distance.positive?
end

def check_digits_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_unit(unit)
raise 'It must be km or mi' unless %w[km mi].include?(unit)
end
end
22 changes: 22 additions & 0 deletions lib/calcpace/converter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

module Converter
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')
end

# tem que chamar as checagens de digitos antes de chamar o convert_distance
def convert(distance, unit)
case unit
when 'km'
(distance * 0.621371).round(2)
when 'mi'
(distance * 1.60934).round(2)
end
end
end
9 changes: 0 additions & 9 deletions lib/main.rb

This file was deleted.

23 changes: 0 additions & 23 deletions lib/run_calculate.rb

This file was deleted.

34 changes: 0 additions & 34 deletions lib/run_check.rb

This file was deleted.

10 changes: 0 additions & 10 deletions lib/run_convert.rb

This file was deleted.

Loading

0 comments on commit 18d0f2b

Please sign in to comment.