-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 0980471
Showing
26 changed files
with
800 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/.bundle/ | ||
/.yardoc | ||
/_yardoc/ | ||
/coverage/ | ||
/doc/ | ||
/pkg/ | ||
/spec/reports/ | ||
/tmp/ | ||
|
||
# rspec failure tracking | ||
.rspec_status | ||
|
||
*.gem | ||
.idea/* | ||
.ruby-version |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
--format progress | ||
--color | ||
--require spec_helper |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
require: | ||
- standard | ||
- rubocop-rake | ||
- rubocop-rspec | ||
|
||
inherit_gem: | ||
standard: config/base.yml | ||
|
||
AllCops: | ||
NewCops: disable | ||
DisplayStyleGuide: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# frozen_string_literal: true | ||
|
||
SimpleCov.start do | ||
add_filter "/spec/" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
ruby_version: "3.0" | ||
parallel: true | ||
format: progress |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# frozen_string_literal: true | ||
|
||
source "https://rubygems.org" | ||
|
||
gemspec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
PATH | ||
remote: . | ||
specs: | ||
turbine_service (0.0.1) | ||
|
||
GEM | ||
remote: https://rubygems.org/ | ||
specs: | ||
ast (2.4.2) | ||
debug (1.9.2) | ||
irb (~> 1.10) | ||
reline (>= 0.3.8) | ||
diff-lcs (1.5.1) | ||
docile (1.4.1) | ||
io-console (0.7.2) | ||
irb (1.14.0) | ||
rdoc (>= 4.0.0) | ||
reline (>= 0.4.2) | ||
json (2.7.2) | ||
language_server-protocol (3.17.0.3) | ||
lint_roller (1.1.0) | ||
parallel (1.26.3) | ||
parser (3.3.5.0) | ||
ast (~> 2.4.1) | ||
racc | ||
psych (5.1.2) | ||
stringio | ||
racc (1.8.1) | ||
rainbow (3.1.1) | ||
rake (13.2.1) | ||
rdoc (6.7.0) | ||
psych (>= 4.0.0) | ||
regexp_parser (2.9.2) | ||
reline (0.5.10) | ||
io-console (~> 0.5) | ||
rexml (3.3.7) | ||
rspec (3.13.0) | ||
rspec-core (~> 3.13.0) | ||
rspec-expectations (~> 3.13.0) | ||
rspec-mocks (~> 3.13.0) | ||
rspec-core (3.13.1) | ||
rspec-support (~> 3.13.0) | ||
rspec-expectations (3.13.3) | ||
diff-lcs (>= 1.2.0, < 2.0) | ||
rspec-support (~> 3.13.0) | ||
rspec-mocks (3.13.1) | ||
diff-lcs (>= 1.2.0, < 2.0) | ||
rspec-support (~> 3.13.0) | ||
rspec-support (3.13.1) | ||
rubocop (1.65.1) | ||
json (~> 2.3) | ||
language_server-protocol (>= 3.17.0) | ||
parallel (~> 1.10) | ||
parser (>= 3.3.0.2) | ||
rainbow (>= 2.2.2, < 4.0) | ||
regexp_parser (>= 2.4, < 3.0) | ||
rexml (>= 3.2.5, < 4.0) | ||
rubocop-ast (>= 1.31.1, < 2.0) | ||
ruby-progressbar (~> 1.7) | ||
unicode-display_width (>= 2.4.0, < 3.0) | ||
rubocop-ast (1.32.3) | ||
parser (>= 3.3.1.0) | ||
rubocop-performance (1.21.1) | ||
rubocop (>= 1.48.1, < 2.0) | ||
rubocop-ast (>= 1.31.1, < 2.0) | ||
rubocop-rake (0.6.0) | ||
rubocop (~> 1.0) | ||
rubocop-rspec (3.0.5) | ||
rubocop (~> 1.61) | ||
ruby-progressbar (1.13.0) | ||
simplecov (0.22.0) | ||
docile (~> 1.1) | ||
simplecov-html (~> 0.11) | ||
simplecov_json_formatter (~> 0.1) | ||
simplecov-html (0.13.1) | ||
simplecov_json_formatter (0.1.4) | ||
standard (1.40.0) | ||
language_server-protocol (~> 3.17.0.2) | ||
lint_roller (~> 1.0) | ||
rubocop (~> 1.65.0) | ||
standard-custom (~> 1.0.0) | ||
standard-performance (~> 1.4) | ||
standard-custom (1.0.2) | ||
lint_roller (~> 1.0) | ||
rubocop (~> 1.50) | ||
standard-performance (1.4.0) | ||
lint_roller (~> 1.1) | ||
rubocop-performance (~> 1.21.0) | ||
stringio (3.1.1) | ||
unicode-display_width (2.6.0) | ||
|
||
PLATFORMS | ||
ruby | ||
x86_64-linux | ||
|
||
DEPENDENCIES | ||
debug | ||
rake | ||
rspec | ||
rubocop | ||
rubocop-rake | ||
rubocop-rspec | ||
simplecov | ||
standard | ||
turbine_service! | ||
|
||
BUNDLED WITH | ||
2.5.9 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2023 Lago | ||
|
||
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
# Turbine Service | ||
|
||
## What's Turbine Service? | ||
|
||
Turbine Service is a simple and lightweight gem to build a service oriented architecture in Ruby. | ||
|
||
## Installation | ||
|
||
Add this line to you application's Gemfile: | ||
|
||
```ruby | ||
gem 'turbine_service' | ||
``` | ||
|
||
And run: | ||
|
||
```bash | ||
bundle install | ||
``` | ||
|
||
## How to use Turbine Service? | ||
|
||
### Basic usage: | ||
|
||
```ruby | ||
class MyService < TurbineService::Base | ||
def initialize(my_args) | ||
@my_args = my_args | ||
|
||
super # Don't forget to call super | ||
end | ||
|
||
def call | ||
# Perform your service logic here | ||
|
||
result | ||
end | ||
end | ||
|
||
result = MyService.call(my_args) | ||
result.success? # => true | ||
result.failure? # => false | ||
result.raise_if_error! # => TurbineService::Result instance | ||
``` | ||
|
||
### With failures | ||
|
||
```ruby | ||
class MyService < TurbineService::Base | ||
def initialize(my_args) | ||
@my_args = my_args | ||
|
||
super # Don't forget to call super | ||
end | ||
|
||
def call | ||
return result.not_found_failure!(resource: 'args') if @my_args.nil? | ||
return result.unauthorized_failure! unless @my_args.user.admin? | ||
|
||
# Perform your service logic here | ||
|
||
result | ||
rescue StandardError => e | ||
result.service_failure!(code: :my_error_code, message: e.message) | ||
end | ||
end | ||
|
||
# Let's imagine that a error occurred in the service | ||
result = MyService.call(my_args) | ||
result.success? # => false | ||
result.failure? # => true | ||
|
||
result.error.code # => :my_error_code | ||
|
||
result.raise_if_error! # => raises TurbineService::ServiceFailure | ||
``` | ||
|
||
### With data attached to the result | ||
|
||
```ruby | ||
class MyService < TurbineService::Base | ||
class Result < TurbineService::Result | ||
attr_accessor :my_data | ||
end | ||
|
||
def initialize(my_args) | ||
@my_args = my_args | ||
|
||
super # Don't forget to call super | ||
end | ||
|
||
def call | ||
# Perform your service logic here | ||
|
||
result.my_data = 'result data' | ||
result | ||
end | ||
end | ||
|
||
result = MyService.call(my_args) | ||
result.success? # => true | ||
result.failure? # => false | ||
result.raise_if_error! # => MyService::Result instance | ||
|
||
result.my_data # => 'result data' | ||
``` | ||
|
||
# Contributing | ||
|
||
Bug reports and pull requests are welcome. | ||
|
||
Here are some ways you can contribute: | ||
- Report bugs | ||
- Fix bugs and submit pull requests | ||
- Write, clarify, or fix documentation | ||
- Suggest or add new features | ||
|
||
To get started with development and testing: | ||
|
||
```bash | ||
git clone https://github.com/getlago/turbine_service.git | ||
cd turbine_service | ||
bundle install | ||
|
||
# Run specs | ||
bundle exec rspec | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# frozen_string_literal: true | ||
|
||
require "rubygems" | ||
require "bundler" | ||
require "rake" | ||
require "rspec/core/rake_task" | ||
require "standard/rake" | ||
require "rubocop/rake_task" | ||
|
||
RSpec::Core::RakeTask.new(:spec) | ||
|
||
RuboCop::RakeTask.new | ||
|
||
task default: %i[spec rubocop] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# frozen_string_literal: true | ||
|
||
require "turbine_service/version" | ||
|
||
# Base structure | ||
require "turbine_service/result" | ||
require "turbine_service/base" | ||
|
||
# Failures | ||
require "turbine_service/failures/base_failure" | ||
require "turbine_service/failures/forbidden_failure" | ||
require "turbine_service/failures/not_allowed_failure" | ||
require "turbine_service/failures/not_found_failure" | ||
require "turbine_service/failures/service_failure" | ||
require "turbine_service/failures/unauthorized_failure" | ||
require "turbine_service/failures/validation_failure" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# frozen_string_literal: true | ||
|
||
module TurbineService | ||
class Base | ||
Result = TurbineService::Result | ||
|
||
def self.call(*args, **keyword_args, &block) | ||
new(*args, **keyword_args).call(&block) | ||
end | ||
|
||
def initialize(*, **) | ||
@result = self.class::Result.new | ||
end | ||
|
||
def call(**args, &block) | ||
raise NotImplementedError | ||
end | ||
|
||
private | ||
|
||
attr_reader :result | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# frozen_string_literal: true | ||
|
||
module TurbineService | ||
module Failures | ||
class BaseFailure < StandardError | ||
attr_reader :result | ||
|
||
def initialize(result, message) | ||
@result = result | ||
|
||
super(message) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# frozen_string_literal: true | ||
|
||
module TurbineService | ||
module Failures | ||
class ForbiddenFailure < BaseFailure | ||
attr_reader :code | ||
|
||
def initialize(result, code) | ||
@code = code | ||
|
||
super | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.