rao-api-service_controller 0.0.8.pre
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +34 -0
- data/Rakefile +25 -0
- data/app/concerns/rao/api/service_controller/exception_handling_concern.rb +42 -0
- data/app/concerns/rao/api/service_controller/rest_actions_concern.rb +88 -0
- data/app/concerns/rao/api/service_controller/serialization_concern.rb +17 -0
- data/app/concerns/rao/api/service_controller/service_concern.rb +18 -0
- data/app/concerns/rao/api/service_controller/service_inflections_concern.rb +19 -0
- data/app/controllers/rao/api/service_controller/base.rb +12 -0
- data/config/locales/de.yml +1 -0
- data/config/locales/en.yml +1 -0
- data/lib/generators/rao/api/service_controller/install_generator.rb +17 -0
- data/lib/generators/rao/api/service_controller/templates/initializer.rb +7 -0
- data/lib/rao/api/service_controller/configuration.rb +13 -0
- data/lib/rao/api/service_controller/engine.rb +9 -0
- data/lib/rao/api/service_controller/version.rb +9 -0
- data/lib/rao/api/service_controller.rb +11 -0
- data/lib/rao-api-service_controller.rb +1 -0
- metadata +118 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ca5415c541504a8cbf36a062665d300d9b0dbc6c
|
4
|
+
data.tar.gz: a315f682e4fd9e89450824c3a5a93fb66627e91c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: be5a598b36cc0f50768376b07e0cb5426dfc4905d6f81f8b7aac0cfc92e6a62bce76493851b48baeddecb3cc5d8679690fa28dd995a969826f2c0dde1cb6edfa
|
7
|
+
data.tar.gz: e3c5ef454411e2e106222e71bc483012a7ed55965707f0f9ce86500dfe0304f684a72b8b62eff404098ac6c3af2ad2487d9f69ce8b38f034943598c61575fac0
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2018 Roberto Vasquez Angel
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
# Rao::Api:ServiceController
|
2
|
+
Short description and motivation.
|
3
|
+
|
4
|
+
## Usage
|
5
|
+
How to use my plugin.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
gem 'rao-api-service_controller'
|
12
|
+
```
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
```bash
|
16
|
+
$ bundle
|
17
|
+
```
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
```bash
|
21
|
+
$ gem install rao-api-service_controller
|
22
|
+
```
|
23
|
+
|
24
|
+
Generate the initializer:
|
25
|
+
|
26
|
+
```bash
|
27
|
+
$ rails g rao:api:service_controller:install
|
28
|
+
```
|
29
|
+
|
30
|
+
## Contributing
|
31
|
+
Contribution directions go here.
|
32
|
+
|
33
|
+
## License
|
34
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
rescue LoadError
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
+
end
|
6
|
+
|
7
|
+
require 'rdoc/task'
|
8
|
+
|
9
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
10
|
+
rdoc.rdoc_dir = 'rdoc'
|
11
|
+
rdoc.title = 'Rails Add-Ons API Service Controller'
|
12
|
+
rdoc.options << '--line-numbers'
|
13
|
+
rdoc.rdoc_files.include('README.md')
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
+
end
|
16
|
+
|
17
|
+
APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
|
18
|
+
load 'rails/tasks/engine.rake'
|
19
|
+
|
20
|
+
|
21
|
+
load 'rails/tasks/statistics.rake'
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
require 'bundler/gem_tasks'
|
@@ -0,0 +1,42 @@
|
|
1
|
+
module Rao
|
2
|
+
module Api
|
3
|
+
module ServiceController::ExceptionHandlingConcern
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
included do
|
7
|
+
rescue_from Exception do |exception|
|
8
|
+
handle_exception(exception)
|
9
|
+
end
|
10
|
+
|
11
|
+
rescue_from ActiveRecord::RecordNotFound do |exception|
|
12
|
+
handle_404(exception)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def handle_404(exception = nil)
|
19
|
+
respond_to do |format|
|
20
|
+
format.json { render json: { error: (exception.try(:message) || 'Not found') }, status: 404 }
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def handle_exception(exception)
|
25
|
+
if Rails.env.development? || Rails.env.test?
|
26
|
+
error = { message: exception.message }
|
27
|
+
|
28
|
+
error[:application_trace] = Rails.backtrace_cleaner.clean(exception.backtrace)
|
29
|
+
error[:full_trace] = exception.backtrace
|
30
|
+
|
31
|
+
respond_to do |format|
|
32
|
+
format.json { render json: error, status: 500 }
|
33
|
+
end
|
34
|
+
else
|
35
|
+
respond_to do |format|
|
36
|
+
format.json { render json: { error: 'Internal server error.' }, status: 500 }
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,88 @@
|
|
1
|
+
module Rao
|
2
|
+
module Api
|
3
|
+
module ServiceController::RestActionsConcern
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
included do
|
7
|
+
include ActionController::MimeResponds
|
8
|
+
|
9
|
+
unless respond_to?(:respond_to)
|
10
|
+
raise "undefined method `respond_to' for #{self.name}: If you are running Rails > 4 you may need to add the responders gem to your Gemfile."
|
11
|
+
end
|
12
|
+
|
13
|
+
respond_to :json
|
14
|
+
|
15
|
+
if respond_to?(:before_action)
|
16
|
+
before_action :initialize_service_for_create, only: [:create]
|
17
|
+
else
|
18
|
+
before_filter :initialize_service_for_create, only: [:create]
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def create
|
23
|
+
perform
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def perform
|
29
|
+
@result = execute_service
|
30
|
+
respond_to do |format|
|
31
|
+
if @result.success?
|
32
|
+
before_respond_on_success if respond_to?(:before_respond_on_success, true)
|
33
|
+
format.json { render json: serialize_result(@result), status: :created }
|
34
|
+
else
|
35
|
+
format.json { render json: { errors: serialize_errors(@result.errors) }, status: 422 }
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def execute_service
|
41
|
+
@service.send(execute_method)
|
42
|
+
end
|
43
|
+
|
44
|
+
def execute_method
|
45
|
+
:perform
|
46
|
+
end
|
47
|
+
|
48
|
+
def hashified_params
|
49
|
+
if permitted_params.respond_to?(:to_h)
|
50
|
+
permitted_params.to_h
|
51
|
+
else
|
52
|
+
permitted_params
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
# Override this method in your child class to manipulate your service before
|
57
|
+
# it performs.
|
58
|
+
#
|
59
|
+
# Example:
|
60
|
+
# # app/controller/import_services_controller.rb
|
61
|
+
# class ImportServices < ApplicationServicesController
|
62
|
+
# #...
|
63
|
+
# private
|
64
|
+
#
|
65
|
+
# def initialize_service_for_create
|
66
|
+
# super
|
67
|
+
# @service.current_user_id = session['current_user_id']
|
68
|
+
# end
|
69
|
+
# end
|
70
|
+
#
|
71
|
+
def initialize_service_for_create
|
72
|
+
@service = service_class.new(hashified_params, service_options)
|
73
|
+
end
|
74
|
+
|
75
|
+
def service_options
|
76
|
+
default_options
|
77
|
+
end
|
78
|
+
|
79
|
+
def default_options
|
80
|
+
{ autosave: true }
|
81
|
+
end
|
82
|
+
|
83
|
+
def permitted_params
|
84
|
+
raise "You have to implement permitted_params in #{self.class.name}."
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module Rao
|
2
|
+
module Api
|
3
|
+
module ServiceController::ServiceConcern
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
included do
|
7
|
+
helper_method :service_class
|
8
|
+
end
|
9
|
+
|
10
|
+
def service_class
|
11
|
+
unless self.class.respond_to?(:service_class)
|
12
|
+
raise "undefined method `service_class' for #{self.class.name}: Add a service_class method to your controller. Example: def self.service_class; MyAmazingService; end"
|
13
|
+
end
|
14
|
+
self.class.service_class
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Rao
|
2
|
+
module Api
|
3
|
+
module ServiceController::ServiceInflectionsConcern
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
included do
|
7
|
+
helper_method :inflections
|
8
|
+
end
|
9
|
+
|
10
|
+
private
|
11
|
+
|
12
|
+
def inflections
|
13
|
+
{
|
14
|
+
service_name: service_class.model_name.human(count: 1)
|
15
|
+
}
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
module Rao
|
2
|
+
module Api
|
3
|
+
module ServiceController
|
4
|
+
class Base < Rao::Api::ServiceController::Configuration.service_controller_base_class_name.constantize
|
5
|
+
include ServiceConcern
|
6
|
+
include RestActionsConcern
|
7
|
+
include SerializationConcern
|
8
|
+
include ExceptionHandlingConcern
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
de:
|
@@ -0,0 +1 @@
|
|
1
|
+
en:
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Rao
|
2
|
+
module Api
|
3
|
+
module ServiceController
|
4
|
+
module Generators
|
5
|
+
class InstallGenerator < Rails::Generators::Base
|
6
|
+
desc 'Generates the initializer'
|
7
|
+
|
8
|
+
source_root File.expand_path('../templates', __FILE__)
|
9
|
+
|
10
|
+
def generate_initializer
|
11
|
+
template 'initializer.rb', 'config/initializers/rao-api-service_controller.rb'
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,7 @@
|
|
1
|
+
Rao::Api::ServiceController.configure do |config|
|
2
|
+
# Set the base controller for service controllers.
|
3
|
+
#
|
4
|
+
# default: config.service_controller_base_class_name = '::ApplicationController'
|
5
|
+
#
|
6
|
+
config.service_controller_base_class_name = '::ApplicationController'
|
7
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require "rao/api/service_controller"
|
metadata
ADDED
@@ -0,0 +1,118 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rao-api-service_controller
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.8.pre
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Roberto Vasquez Angel
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-01-16 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rao
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: guard-rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: guard-bundler
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description:
|
70
|
+
email:
|
71
|
+
- roberto@vasquez-angel.de
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- MIT-LICENSE
|
77
|
+
- README.md
|
78
|
+
- Rakefile
|
79
|
+
- app/concerns/rao/api/service_controller/exception_handling_concern.rb
|
80
|
+
- app/concerns/rao/api/service_controller/rest_actions_concern.rb
|
81
|
+
- app/concerns/rao/api/service_controller/serialization_concern.rb
|
82
|
+
- app/concerns/rao/api/service_controller/service_concern.rb
|
83
|
+
- app/concerns/rao/api/service_controller/service_inflections_concern.rb
|
84
|
+
- app/controllers/rao/api/service_controller/base.rb
|
85
|
+
- config/locales/de.yml
|
86
|
+
- config/locales/en.yml
|
87
|
+
- lib/generators/rao/api/service_controller/install_generator.rb
|
88
|
+
- lib/generators/rao/api/service_controller/templates/initializer.rb
|
89
|
+
- lib/rao-api-service_controller.rb
|
90
|
+
- lib/rao/api/service_controller.rb
|
91
|
+
- lib/rao/api/service_controller/configuration.rb
|
92
|
+
- lib/rao/api/service_controller/engine.rb
|
93
|
+
- lib/rao/api/service_controller/version.rb
|
94
|
+
homepage: https://github.com/rao
|
95
|
+
licenses:
|
96
|
+
- MIT
|
97
|
+
metadata: {}
|
98
|
+
post_install_message:
|
99
|
+
rdoc_options: []
|
100
|
+
require_paths:
|
101
|
+
- lib
|
102
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
103
|
+
requirements:
|
104
|
+
- - ">="
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
version: '0'
|
107
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
108
|
+
requirements:
|
109
|
+
- - ">"
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
version: 1.3.1
|
112
|
+
requirements: []
|
113
|
+
rubyforge_project:
|
114
|
+
rubygems_version: 2.6.14
|
115
|
+
signing_key:
|
116
|
+
specification_version: 4
|
117
|
+
summary: API Services Controller for Ruby on Rails.
|
118
|
+
test_files: []
|