1
0

First commit

Includes

- Spec setup with FactoryBot
- A basic shipping option data structure
- A shipping option repository to have one central place with shipping options
- Github actions pipeline
This commit is contained in:
Tim Kächele 2021-03-05 21:00:31 +01:00
commit 5f3974331c
19 changed files with 493 additions and 0 deletions

23
.github/workflows/rspec.yml vendored Normal file
View File

@ -0,0 +1,23 @@
name: CI RSpec Tests
on:
push:
paths:
- '*'
pull_request:
paths:
- '*'
jobs:
test:
name: CI
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: actions/setup-ruby@v1
with:
ruby-version: 2.7
- name: Install dependencies
run: bundle install
- name: Run specs
run: bundle exec rspec

11
.gitignore vendored Normal file
View File

@ -0,0 +1,11 @@
/.bundle/
/.yardoc
/_yardoc/
/coverage/
/doc/
/pkg/
/spec/reports/
/tmp/
# rspec failure tracking
.rspec_status

3
.rspec Normal file
View File

@ -0,0 +1,3 @@
--format documentation
--color
--require spec_helper

74
CODE_OF_CONDUCT.md Normal file
View File

@ -0,0 +1,74 @@
# Contributor Covenant Code of Conduct
## Our Pledge
In the interest of fostering an open and welcoming environment, we as
contributors and maintainers pledge to making participation in our project and
our community a harassment-free experience for everyone, regardless of age, body
size, disability, ethnicity, gender identity and expression, level of experience,
nationality, personal appearance, race, religion, or sexual identity and
orientation.
## Our Standards
Examples of behavior that contributes to creating a positive environment
include:
* Using welcoming and inclusive language
* Being respectful of differing viewpoints and experiences
* Gracefully accepting constructive criticism
* Focusing on what is best for the community
* Showing empathy towards other community members
Examples of unacceptable behavior by participants include:
* The use of sexualized language or imagery and unwelcome sexual attention or
advances
* Trolling, insulting/derogatory comments, and personal or political attacks
* Public or private harassment
* Publishing others' private information, such as a physical or electronic
address, without explicit permission
* Other conduct which could reasonably be considered inappropriate in a
professional setting
## Our Responsibilities
Project maintainers are responsible for clarifying the standards of acceptable
behavior and are expected to take appropriate and fair corrective action in
response to any instances of unacceptable behavior.
Project maintainers have the right and responsibility to remove, edit, or
reject comments, commits, code, wiki edits, issues, and other contributions
that are not aligned to this Code of Conduct, or to ban temporarily or
permanently any contributor for other behaviors that they deem inappropriate,
threatening, offensive, or harmful.
## Scope
This Code of Conduct applies both within project spaces and in public spaces
when an individual is representing the project or its community. Examples of
representing a project or community include using an official project e-mail
address, posting via an official social media account, or acting as an appointed
representative at an online or offline event. Representation of a project may be
further defined and clarified by project maintainers.
## Enforcement
Instances of abusive, harassing, or otherwise unacceptable behavior may be
reported by contacting the project team at tim.kaechele@finlink.de. All
complaints will be reviewed and investigated and will result in a response that
is deemed necessary and appropriate to the circumstances. The project team is
obligated to maintain confidentiality with regard to the reporter of an incident.
Further details of specific enforcement policies may be posted separately.
Project maintainers who do not follow or enforce the Code of Conduct in good
faith may face temporary or permanent repercussions as determined by other
members of the project's leadership.
## Attribution
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
available at [https://contributor-covenant.org/version/1/4][version]
[homepage]: https://contributor-covenant.org
[version]: https://contributor-covenant.org/version/1/4/

9
Gemfile Normal file
View File

@ -0,0 +1,9 @@
# frozen_string_literal: true
source 'https://rubygems.org'
# Specify your gem's dependencies in planet_express.gemspec
gemspec
gem 'rake', '~> 12.0'
gem 'rspec', '~> 3.0'

71
Gemfile.lock Normal file
View File

@ -0,0 +1,71 @@
PATH
remote: .
specs:
planet_express (0.1.0)
GEM
remote: https://rubygems.org/
specs:
activesupport (6.1.3)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 1.6, < 2)
minitest (>= 5.1)
tzinfo (~> 2.0)
zeitwerk (~> 2.3)
ast (2.4.2)
concurrent-ruby (1.1.8)
diff-lcs (1.4.4)
factory_bot (6.1.0)
activesupport (>= 5.0.0)
i18n (1.8.9)
concurrent-ruby (~> 1.0)
minitest (5.14.4)
parallel (1.20.1)
parser (3.0.0.0)
ast (~> 2.4.1)
rainbow (3.0.0)
rake (12.3.3)
regexp_parser (2.1.1)
rexml (3.2.4)
rspec (3.10.0)
rspec-core (~> 3.10.0)
rspec-expectations (~> 3.10.0)
rspec-mocks (~> 3.10.0)
rspec-core (3.10.1)
rspec-support (~> 3.10.0)
rspec-expectations (3.10.1)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.10.0)
rspec-mocks (3.10.2)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.10.0)
rspec-support (3.10.2)
rubocop (1.11.0)
parallel (~> 1.10)
parser (>= 3.0.0.0)
rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 1.8, < 3.0)
rexml
rubocop-ast (>= 1.2.0, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 1.4.0, < 3.0)
rubocop-ast (1.4.1)
parser (>= 2.7.1.5)
ruby-progressbar (1.11.0)
tzinfo (2.0.4)
concurrent-ruby (~> 1.0)
unicode-display_width (2.0.0)
zeitwerk (2.4.2)
PLATFORMS
ruby
DEPENDENCIES
factory_bot (~> 6.1)
planet_express!
rake (~> 12.0)
rspec (~> 3.0)
rubocop (~> 1.11)
BUNDLED WITH
2.1.4

21
LICENSE.txt Normal file
View File

@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright (c) 2021 Philip J. Fry
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

44
README.md Normal file
View File

@ -0,0 +1,44 @@
# PlanetExpress
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/planet_express`. To experiment with that code, run `bin/console` for an interactive prompt.
TODO: Delete this and the text above, and describe your gem
## Installation
Add this line to your application's Gemfile:
```ruby
gem 'planet_express'
```
And then execute:
$ bundle install
Or install it yourself as:
$ gem install planet_express
## Usage
TODO: Write usage instructions here
## Development
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
## Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/planet_express. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/planet_express/blob/master/CODE_OF_CONDUCT.md).
## License
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
## Code of Conduct
Everyone interacting in the PlanetExpress project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/planet_express/blob/master/CODE_OF_CONDUCT.md).

8
Rakefile Normal file
View File

@ -0,0 +1,8 @@
# frozen_string_literal: true
require 'bundler/gem_tasks'
require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new(:spec)
task default: :spec

15
bin/console Executable file
View File

@ -0,0 +1,15 @@
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'bundler/setup'
require 'planet_express'
# You can add fixtures and/or initialization code here to make experimenting
# with your gem easier. You can also use a different console, if you like.
# (If you use this, don't forget to add pry to your Gemfile!)
# require "pry"
# Pry.start
require 'irb'
IRB.start(__FILE__)

8
bin/setup Executable file
View File

@ -0,0 +1,8 @@
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
set -vx
bundle install
# Do any other automated setup that you need to do here

10
lib/planet_express.rb Normal file
View File

@ -0,0 +1,10 @@
# frozen_string_literal: true
require 'planet_express/version'
require 'planet_express/shipping_option'
require 'planet_express/shipping_option_repository'
module PlanetExpress
class Error < StandardError; end
# Your code goes here...
end

View File

@ -0,0 +1,5 @@
# frozen_string_literal: true
module PlanetExpress
ShippingOption = Struct.new(:provider, :package_size, :price_in_cents)
end

View File

@ -0,0 +1,44 @@
# frozen_string_literal: true
module PlanetExpress
class ShippingOptionRepository
attr_reader :data
def initialize(data: nil)
@data = data || default_data
end
def all
data
end
def where(attribute_matchers = {})
all.select do |entry|
attribute_matchers.map do |attribute, value|
entry.public_send(attribute.to_sym) == value
end.all?
end
end
def find_by_provider_and_package_size(provider, package_size)
where(provider: provider, package_size: package_size).first
end
def lowest_price_for_package_size(package_size)
where(package_size: package_size).map(&:price_in_cents).min
end
private
def default_data
[
ShippingOption.new('LP', 'S', 150),
ShippingOption.new('LP', 'M', 490),
ShippingOption.new('LP', 'L', 690),
ShippingOption.new('MR', 'S', 200),
ShippingOption.new('MR', 'M', 300),
ShippingOption.new('MR', 'L', 400)
]
end
end
end

View File

@ -0,0 +1,5 @@
# frozen_string_literal: true
module PlanetExpress
VERSION = '0.1.0'
end

34
planet_express.gemspec Normal file
View File

@ -0,0 +1,34 @@
# frozen_string_literal: true
require_relative 'lib/planet_express/version'
Gem::Specification.new do |spec|
spec.name = 'planet_express'
spec.version = PlanetExpress::VERSION
spec.authors = ['Philip J. Fry']
spec.email = ['philip@planetexpress.com']
spec.summary = 'Calculate your shipment discounts easily'
spec.description = 'A library to calculate the applicable shipment discounts for a list of shipments.'
spec.homepage = 'https://github.com/timkaechele/planet_express'
spec.license = 'MIT'
spec.required_ruby_version = Gem::Requirement.new('>= 2.4.0')
# spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
spec.metadata['homepage_uri'] = spec.homepage
# spec.metadata["source_code_uri"] = "TODO: Put your gem's public repo URL here."
# spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
# Specify which files should be added to the gem when it is released.
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
spec.files = Dir.chdir(File.expand_path(__dir__)) do
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
end
spec.bindir = 'exe'
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ['lib']
spec.add_development_dependency('rubocop', '~> 1.11')
spec.add_development_dependency('factory_bot', '~> 6.1')
end

View File

@ -0,0 +1,7 @@
FactoryBot.define do
factory :shipping_option, class: PlanetExpress::ShippingOption do
sequence(:provider) { 'LP' }
sequence(:package_size) { ['S', 'M', 'L'].sample }
sequence(:price_in_cents) { |i| i * 100 }
end
end

View File

@ -0,0 +1,77 @@
# frozen_string_literal: true
RSpec.describe PlanetExpress::ShippingOptionRepository do
subject { PlanetExpress::ShippingOptionRepository.new }
describe '#where' do
context 'no data' do
subject { PlanetExpress::ShippingOptionRepository.new(data: []) }
it 'returns an empty array' do
expect(subject.where()).to eq([])
end
end
context 'no arguments' do
before(:each) do
expect(subject.all).to_not be_empty
end
it 'returns all items' do
expect(subject.where()).to eq(subject.all)
end
end
context 'with existing attribute' do
let(:expected_item) do
build(:shipping_option, package_size: 'L', price_in_cents: 410)
end
let(:data) do
build_list(:shipping_option, 4, price_in_cents: 500) + [expected_item]
end
subject do
PlanetExpress::ShippingOptionRepository.new(data: data)
end
it 'returns a list with the expected item' do
expect(subject.where(package_size: 'L', price_in_cents: 410)).to eq([expected_item])
end
end
context 'with unknown attribute' do
it 'throws an exception' do
expect do
subject.where(unknown_attribute: nil)
end.to raise_error(NoMethodError)
end
end
end
describe '#lowest_price_for_package_size' do
it 'finds the lowest price for a package size' do
expect(subject.lowest_price_for_package_size('L')).to eq(400)
end
end
describe '#lowest_price_for_package_size' do
context 'existing option' do
it 'finds the correct shipping option' do
result = subject.find_by_provider_and_package_size('LP', 'S')
expect(result.provider).to eq('LP')
expect(result.package_size).to eq('S')
expect(result.price_in_cents).to eq(150)
end
end
context 'unknown option' do
it 'returns nil' do
result = subject.find_by_provider_and_package_size('UNKNOWN', 'S')
expect(result).to eq(nil)
end
end
end
end

24
spec/spec_helper.rb Normal file
View File

@ -0,0 +1,24 @@
# frozen_string_literal: true
require 'bundler/setup'
require 'factory_bot'
require 'planet_express'
RSpec.configure do |config|
# Enable flags like --only-failures and --next-failure
config.example_status_persistence_file_path = '.rspec_status'
# Disable RSpec exposing methods globally on `Module` and `main`
config.disable_monkey_patching!
config.expect_with :rspec do |c|
c.syntax = :expect
end
# FactoryBot setup
config.include FactoryBot::Syntax::Methods
config.before(:suite) do
FactoryBot.find_definitions
end
end