simple_action 0.0.1.pre1 → 0.0.1.pre3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/Gemfile.lock +6 -3
- data/README.md +108 -2
- data/lib/simple_action/response.rb +1 -0
- data/lib/simple_action/service.rb +1 -1
- data/lib/simple_action/version.rb +1 -1
- data/simple_action.gemspec +1 -1
- data/spec/response_spec.rb +20 -0
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
OGNmNTZkMDQxNDllYjJkMGQ5NjhlZjMyMTRiODEyNTk1Y2I0YzRkYg==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
ZWUxMDRiNmRlOWRiNWFjMGNhNTYyZGMzNzQ1YmE1NjFmMTEzNTcwMQ==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
ZTVmZWZmOTk0MmVjYzQ5Yjk1ZTRiZjBkYzk4OTQ3N2YwYmJkZjg3MDQ0NDk2
|
10
|
+
Nzg5ZTY0MTkzMDBkNjQwM2FiY2Q4MzFjMGQ5ODIyMmY4ODc5OTQyZGFmMzE3
|
11
|
+
Zjk5NWVjNjA3YmFjODNmOTEyNDNmNGU5MjgwOTExOWE3ZmFjMjg=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
MDg1NDRiNmRiZjdjYzA1NTFmYjk2ZTUzMzA5NDM2Zjk0N2M3NDIxYWRhMjEx
|
14
|
+
ZmMxMDgxZGVkYzM5YjM1NjE2NjMzMGVkMjg0YmU4MmU2OTVlNjNhYjJhNjQy
|
15
|
+
MDBjYzZkZDY1YzE4OGMyZWRmNzg5ODc1Y2ZhMjc2NTU1ZjYxMWM=
|
data/Gemfile.lock
CHANGED
@@ -2,7 +2,7 @@ PATH
|
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
4
|
simple_action (0.0.1.pre1)
|
5
|
-
simple_params (>= 0.0.
|
5
|
+
simple_params (>= 0.0.4, < 1.0)
|
6
6
|
|
7
7
|
GEM
|
8
8
|
remote: https://rubygems.org/
|
@@ -32,7 +32,7 @@ GEM
|
|
32
32
|
ice_nine (0.11.1)
|
33
33
|
json (1.8.2)
|
34
34
|
method_source (0.8.2)
|
35
|
-
minitest (5.
|
35
|
+
minitest (5.6.1)
|
36
36
|
pry (0.10.1)
|
37
37
|
coderay (~> 1.1.0)
|
38
38
|
method_source (~> 0.8.1)
|
@@ -46,8 +46,11 @@ GEM
|
|
46
46
|
rspec-expectations (2.99.2)
|
47
47
|
diff-lcs (>= 1.1.3, < 2.0)
|
48
48
|
rspec-mocks (2.99.3)
|
49
|
-
|
49
|
+
shoulda-matchers (2.8.0)
|
50
|
+
activesupport (>= 3.0.0)
|
51
|
+
simple_params (0.0.4)
|
50
52
|
activemodel (>= 3.0, < 5.0)
|
53
|
+
shoulda-matchers (~> 2.8)
|
51
54
|
virtus (>= 1.0.0)
|
52
55
|
slop (3.6.0)
|
53
56
|
thread_safe (0.3.5)
|
data/README.md
CHANGED
@@ -1,2 +1,108 @@
|
|
1
|
-
#
|
2
|
-
|
1
|
+
# Simple Action
|
2
|
+
|
3
|
+
Simple Action provides a convenient DSL for building API endpoints and service objects. It builds significantly off the the parameter coercion/validation support provided by Simple Params [simple_params](https://github.com/brycesenz/simple_params).
|
4
|
+
|
5
|
+
The design of this gem was greatly influenced by this post by Philippe Creux:
|
6
|
+
http://brewhouse.io/blog/2014/04/30/gourmet-service-objects.html
|
7
|
+
|
8
|
+
This class provides the following benefits for building API endpoints:
|
9
|
+
* Easy assignment and automatic validation of parameters with ActiveModel-like errors.
|
10
|
+
* A simple syntax for defining the execution block
|
11
|
+
* Easy validation, for integration into controllers
|
12
|
+
|
13
|
+
## Installation
|
14
|
+
|
15
|
+
Add this line to your application's Gemfile:
|
16
|
+
|
17
|
+
gem 'simple_action'
|
18
|
+
|
19
|
+
And then execute:
|
20
|
+
|
21
|
+
$ bundle
|
22
|
+
|
23
|
+
Or install it yourself as:
|
24
|
+
|
25
|
+
$ gem install simple_action
|
26
|
+
|
27
|
+
## Defining your Service Class & Working with Rails Controllers
|
28
|
+
|
29
|
+
A service class is defined by two things - the parameters it accepts, and the `execute` method:
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
class RegisterUser < SimpleAction::Service
|
33
|
+
params do
|
34
|
+
param :name
|
35
|
+
param :age, type: :integer, validations: { numericality: { greater_than_or_equal_to: 18 } }
|
36
|
+
param :hair_color, default: "brown", validations: { inclusion: { in: ["brown", "red", "blonde", "white"] }}
|
37
|
+
end
|
38
|
+
|
39
|
+
def execute
|
40
|
+
user = User.create(name: name, age: age, hair_color: hair_color)
|
41
|
+
UserMailer.welcome_letter(user).deliver
|
42
|
+
user
|
43
|
+
end
|
44
|
+
end
|
45
|
+
```
|
46
|
+
|
47
|
+
The class is executed via a `run` call to the class itself:
|
48
|
+
|
49
|
+
```ruby
|
50
|
+
response = RegisterUser.run(name: "Tom", age: 21)
|
51
|
+
response.valid? #=> true
|
52
|
+
response.errors.empty? #=> true
|
53
|
+
response.result #=> User, id: 1, name: "Tom", age: 21
|
54
|
+
|
55
|
+
response = RegisterUser.run(name: nil, age: 21)
|
56
|
+
response.valid? #=> false
|
57
|
+
response.errors[:name] #=> ["can't be blank"]
|
58
|
+
response.result #=> nil
|
59
|
+
```
|
60
|
+
|
61
|
+
## Working with Rails Controllers
|
62
|
+
|
63
|
+
Building off of the example service class above, our controller logic can now be greatly simplified, as such
|
64
|
+
|
65
|
+
```ruby
|
66
|
+
class UserController < ApplicationController
|
67
|
+
...
|
68
|
+
|
69
|
+
def new
|
70
|
+
@registration = RegisterUser.new
|
71
|
+
render action: :new
|
72
|
+
end
|
73
|
+
|
74
|
+
def create
|
75
|
+
registration = RegisterUser.run(params[:register_user])
|
76
|
+
if registration.valid?
|
77
|
+
@user = registration.result
|
78
|
+
redirect_to @user, notice: "Success!"
|
79
|
+
else
|
80
|
+
@registration = registration
|
81
|
+
render action: :new, alert: "Errors!"
|
82
|
+
end
|
83
|
+
end
|
84
|
+
```
|
85
|
+
|
86
|
+
Because the service class behaves like an ActiveModel object with regards to it's attribute assignment and parameter validation, it will continue to work with Rails forms.
|
87
|
+
|
88
|
+
|
89
|
+
# ApiPie Documentation
|
90
|
+
|
91
|
+
If your project is using [apipie-rails](http://example.com/ "apipie-rails"),
|
92
|
+
then SimpleAction is able to automatically generate the documentation markup
|
93
|
+
for apipie.
|
94
|
+
|
95
|
+
```ruby
|
96
|
+
api :POST, '/users', "Registers a user"
|
97
|
+
eval(RegisterUser.api_pie_documentation)
|
98
|
+
```
|
99
|
+
|
100
|
+
This feature is also delegated to the SimpleParams class. You can read more on the details of that functionality here [simple_params](https://github.com/brycesenz/simple_params).
|
101
|
+
|
102
|
+
## Contributing
|
103
|
+
|
104
|
+
1. Fork it
|
105
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
106
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
107
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
108
|
+
5. Create new Pull Request
|
data/simple_action.gemspec
CHANGED
@@ -18,7 +18,7 @@ Gem::Specification.new do |spec|
|
|
18
18
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
19
|
spec.require_paths = ["lib"]
|
20
20
|
|
21
|
-
spec.add_dependency "simple_params", ">= 0.0.
|
21
|
+
spec.add_dependency "simple_params", ">= 0.0.4", "< 1.0"
|
22
22
|
spec.add_development_dependency "bundler", "~> 1.5"
|
23
23
|
spec.add_development_dependency "rake", "~> 10.1"
|
24
24
|
spec.add_development_dependency "rspec", "~> 2.6"
|
data/spec/response_spec.rb
CHANGED
@@ -81,4 +81,24 @@ describe SimpleAction::Response do
|
|
81
81
|
end
|
82
82
|
end
|
83
83
|
end
|
84
|
+
|
85
|
+
describe "#value" do
|
86
|
+
let(:object) { DummyServiceObjectClass.new(name: "Dummy") }
|
87
|
+
|
88
|
+
context "with no result provided" do
|
89
|
+
let(:response) { described_class.new(object) }
|
90
|
+
|
91
|
+
it "is nil" do
|
92
|
+
response.value.should be_nil
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
context "with response provided" do
|
97
|
+
let(:response) { described_class.new(object, 53) }
|
98
|
+
|
99
|
+
it "is 53" do
|
100
|
+
response.value.should eq(53)
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
84
104
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple_action
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.1.
|
4
|
+
version: 0.0.1.pre3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- brycesenz
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-05-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: simple_params
|
@@ -16,7 +16,7 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ! '>='
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: 0.0.
|
19
|
+
version: 0.0.4
|
20
20
|
- - <
|
21
21
|
- !ruby/object:Gem::Version
|
22
22
|
version: '1.0'
|
@@ -26,7 +26,7 @@ dependencies:
|
|
26
26
|
requirements:
|
27
27
|
- - ! '>='
|
28
28
|
- !ruby/object:Gem::Version
|
29
|
-
version: 0.0.
|
29
|
+
version: 0.0.4
|
30
30
|
- - <
|
31
31
|
- !ruby/object:Gem::Version
|
32
32
|
version: '1.0'
|