api_service 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +15 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/Gemfile +3 -0
- data/LICENSE.txt +21 -0
- data/README.md +29 -0
- data/Rakefile +6 -0
- data/api_service.gemspec +27 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/api_service.rb +5 -0
- data/lib/api_service/action.rb +14 -0
- data/lib/api_service/action_state.rb +49 -0
- data/lib/api_service/base.rb +42 -0
- data/lib/api_service/base_action.rb +27 -0
- data/lib/api_service/error_manager.rb +46 -0
- data/lib/api_service/failed_result.rb +17 -0
- data/lib/api_service/successful_result.rb +23 -0
- data/lib/api_service/version.rb +3 -0
- metadata +106 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 160cef037bed12f7c2ba17a17f5a4974c700c270
|
4
|
+
data.tar.gz: 2d224a81a522a3e1b50fa903751a76e993453363
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2dce24ee9bde816aa3872e1534598275f79582f68fbb70631d7a2f70c411c3f0acfb03991357629124cc591b6b879be0cfef6f3e6d9679071396dad9ef25fda9
|
7
|
+
data.tar.gz: e3c9a5aae6b78d10481de9df92c8b0e5657de8407b19043f6e03e813bd0d2fa786ce922f2a3997d7774082ed6d35253c1433eb4a8a17033e74381d9579b9e898
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Bogdan Sviripa
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# ApiService
|
2
|
+
|
3
|
+
Lightweight skeleton of service for API actions.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'api_service'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install api_service
|
20
|
+
|
21
|
+
## Contributing
|
22
|
+
|
23
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/bogdan-sviripa/api_service.
|
24
|
+
|
25
|
+
|
26
|
+
## License
|
27
|
+
|
28
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
29
|
+
|
data/Rakefile
ADDED
data/api_service.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'api_service/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'api_service'
|
8
|
+
spec.version = ApiService::VERSION
|
9
|
+
spec.authors = ['Bogdan Sviripa']
|
10
|
+
spec.email = ['sviripa.work@gmail.com']
|
11
|
+
|
12
|
+
spec.summary = 'Lightweight skeleton of service for API actions'
|
13
|
+
spec.description = 'Lightweight skeleton of service for API actions'
|
14
|
+
spec.homepage = 'https://github.com/bogdan-sviripa/api_service'
|
15
|
+
spec.license = 'MIT'
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
18
|
+
f.match(%r{^(test|spec|features)/})
|
19
|
+
end
|
20
|
+
spec.bindir = 'exe'
|
21
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
|
+
spec.require_paths = ['lib']
|
23
|
+
|
24
|
+
spec.add_development_dependency 'bundler', '~> 1.14'
|
25
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
26
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
27
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "api_service"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/lib/api_service.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'api_service/base_action'
|
2
|
+
|
3
|
+
module ApiService
|
4
|
+
class Action < BaseAction
|
5
|
+
def initialize(name, options, to_perform)
|
6
|
+
super(name, options)
|
7
|
+
@to_perform = to_perform
|
8
|
+
end
|
9
|
+
|
10
|
+
def perform(params, state)
|
11
|
+
@to_perform.call(params, state)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'api_service/error_manager'
|
2
|
+
require 'api_service/failed_result'
|
3
|
+
require 'api_service/successful_result'
|
4
|
+
|
5
|
+
module ApiService
|
6
|
+
class ActionState
|
7
|
+
attr_reader :response
|
8
|
+
|
9
|
+
def initialize(initial_state = {})
|
10
|
+
@response = nil
|
11
|
+
@state = initial_state
|
12
|
+
@error_manager = ErrorManager.new
|
13
|
+
end
|
14
|
+
|
15
|
+
def add_current_action(action, is_last = false)
|
16
|
+
@current_action = action
|
17
|
+
@is_current_action_last = is_last
|
18
|
+
end
|
19
|
+
|
20
|
+
def action
|
21
|
+
@current_action
|
22
|
+
end
|
23
|
+
|
24
|
+
def valid?
|
25
|
+
!errors.any?
|
26
|
+
end
|
27
|
+
|
28
|
+
def errors
|
29
|
+
@error_manager
|
30
|
+
end
|
31
|
+
|
32
|
+
def set(field, value)
|
33
|
+
@state[field] = value
|
34
|
+
end
|
35
|
+
|
36
|
+
def fetch(field)
|
37
|
+
@state[field]
|
38
|
+
end
|
39
|
+
|
40
|
+
def build_failed_response
|
41
|
+
@response = FailedResult.new(self)
|
42
|
+
end
|
43
|
+
|
44
|
+
def success(response = {}, options = {})
|
45
|
+
return unless @is_current_action_last
|
46
|
+
@response = SuccessfulResult.new(response, self, options)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'api_service/action'
|
2
|
+
require 'api_service/action_state'
|
3
|
+
|
4
|
+
module ApiService
|
5
|
+
class Base
|
6
|
+
class << self
|
7
|
+
def actions
|
8
|
+
@actions ||= []
|
9
|
+
end
|
10
|
+
|
11
|
+
def add_shared_action(name, shared_action, options = {})
|
12
|
+
actions << shared_action.new(name, options)
|
13
|
+
end
|
14
|
+
|
15
|
+
def add_action(name, options = {}, &action_body)
|
16
|
+
actions << Action.new(name, options, action_body)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def initialize(params, initial_state = {})
|
21
|
+
@response = nil
|
22
|
+
@params = params
|
23
|
+
@state = ActionState.new(initial_state)
|
24
|
+
end
|
25
|
+
|
26
|
+
def perform
|
27
|
+
actions_pool = self.class.actions
|
28
|
+
actions_pool.each_with_index do |action, index|
|
29
|
+
@state.add_current_action(action, index == actions_pool.size - 1)
|
30
|
+
|
31
|
+
action.perform(@params, @state)
|
32
|
+
|
33
|
+
if !@state.valid? && action.abort_on_fail?
|
34
|
+
@state.build_failed_response
|
35
|
+
break
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
@state.response
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module ApiService
|
2
|
+
class BaseAction
|
3
|
+
attr_reader :options
|
4
|
+
|
5
|
+
def initialize(name, options)
|
6
|
+
@options = default_options.merge(options)
|
7
|
+
@name = name
|
8
|
+
end
|
9
|
+
|
10
|
+
def abort_on_fail?
|
11
|
+
!@options[:permissive]
|
12
|
+
end
|
13
|
+
|
14
|
+
def fail_http_status
|
15
|
+
@options[:fail_http_status]
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def default_options
|
21
|
+
{
|
22
|
+
permissive: false,
|
23
|
+
fail_http_status: 422
|
24
|
+
}
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
module ApiService
|
2
|
+
class ErrorManager
|
3
|
+
def initialize
|
4
|
+
@errors = {}
|
5
|
+
end
|
6
|
+
|
7
|
+
def any?
|
8
|
+
@errors.any?
|
9
|
+
end
|
10
|
+
|
11
|
+
def add(field, message, scope = '')
|
12
|
+
in_scope(scope) do |errors|
|
13
|
+
field = field.to_sym
|
14
|
+
errors[field] ||= []
|
15
|
+
errors[field] << message
|
16
|
+
errors
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def to_hash
|
21
|
+
@errors
|
22
|
+
end
|
23
|
+
|
24
|
+
def from_model(model)
|
25
|
+
model.errors.messages.each do |field, errors|
|
26
|
+
@errors[field] ||= []
|
27
|
+
@errors[field] = @errors[field].concat(errors).uniq
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
def in_scope(scope)
|
34
|
+
return unless block_given?
|
35
|
+
|
36
|
+
keys = scope.to_s.split('.')
|
37
|
+
@errors = yield(@errors) if keys.empty?
|
38
|
+
|
39
|
+
keys.inject(@errors) do |hash, key|
|
40
|
+
hash[key] ||= {}
|
41
|
+
yield(hash[key]) if key == keys.last
|
42
|
+
hash[key]
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module ApiService
|
2
|
+
class SuccessfulResult
|
3
|
+
attr_reader :state, :response
|
4
|
+
|
5
|
+
def initialize(response, state, options = {})
|
6
|
+
@options = default_options.merge(options)
|
7
|
+
@response = response
|
8
|
+
@state = state
|
9
|
+
end
|
10
|
+
|
11
|
+
def http_status
|
12
|
+
@options[:http_status]
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def default_options
|
18
|
+
{
|
19
|
+
http_status: 201
|
20
|
+
}
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,106 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: api_service
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Bogdan Sviripa
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-11-07 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.14'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.14'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
description: Lightweight skeleton of service for API actions
|
56
|
+
email:
|
57
|
+
- sviripa.work@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
64
|
+
- ".travis.yml"
|
65
|
+
- Gemfile
|
66
|
+
- LICENSE.txt
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- api_service.gemspec
|
70
|
+
- bin/console
|
71
|
+
- bin/setup
|
72
|
+
- lib/api_service.rb
|
73
|
+
- lib/api_service/action.rb
|
74
|
+
- lib/api_service/action_state.rb
|
75
|
+
- lib/api_service/base.rb
|
76
|
+
- lib/api_service/base_action.rb
|
77
|
+
- lib/api_service/error_manager.rb
|
78
|
+
- lib/api_service/failed_result.rb
|
79
|
+
- lib/api_service/successful_result.rb
|
80
|
+
- lib/api_service/version.rb
|
81
|
+
homepage: https://github.com/bogdan-sviripa/api_service
|
82
|
+
licenses:
|
83
|
+
- MIT
|
84
|
+
metadata: {}
|
85
|
+
post_install_message:
|
86
|
+
rdoc_options: []
|
87
|
+
require_paths:
|
88
|
+
- lib
|
89
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
90
|
+
requirements:
|
91
|
+
- - ">="
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
95
|
+
requirements:
|
96
|
+
- - ">="
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: '0'
|
99
|
+
requirements: []
|
100
|
+
rubyforge_project:
|
101
|
+
rubygems_version: 2.5.2
|
102
|
+
signing_key:
|
103
|
+
specification_version: 4
|
104
|
+
summary: Lightweight skeleton of service for API actions
|
105
|
+
test_files: []
|
106
|
+
has_rdoc:
|