sliver-rails 0.1.1 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +131 -11
- data/lib/sliver/rails/jbuilder_template.rb +1 -1
- data/sliver-rails.gemspec +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c3bc921a9806786fed1b48aa7721a6237c2c398e
|
4
|
+
data.tar.gz: 299172b1437ea180c5448ba28a911394e99e2a82
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 845f43b7b7a51de4175e65e401027bcd99868fd4b7d4d4ac07674e7230f02ab8df999913e5cff24509746306b937b7430e80533b185653291dfa55f583ff3527
|
7
|
+
data.tar.gz: 53455f2c9b07af35a587433408101cd4c7d629d51abc069912ee2572075f1456bd00aa522d55fcae131a8f289a9abd71da9fa1891298fe74ea865d9a85b01b8c
|
data/README.md
CHANGED
@@ -1,30 +1,150 @@
|
|
1
|
-
# Sliver
|
1
|
+
# Sliver-Rails
|
2
2
|
|
3
|
-
|
3
|
+
Rails-focused extensions to the [Sliver](https://github.com/pat/sliver) API libary.
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
7
|
-
Add
|
7
|
+
Add the gem to your Gemfile:
|
8
8
|
|
9
9
|
```ruby
|
10
|
-
gem 'sliver-rails'
|
10
|
+
gem 'sliver-rails', '~> 0.2.0'
|
11
11
|
```
|
12
12
|
|
13
|
-
|
13
|
+
## Usage
|
14
14
|
|
15
|
-
|
15
|
+
Slightly different to the pure Sliver approach, instead of including the `Sliver::Action` module, you should instead inherit from `Sliver::Rails::Action`.
|
16
16
|
|
17
|
-
|
17
|
+
```ruby
|
18
|
+
app = Sliver::API.new do |api|
|
19
|
+
api.connect :get, '/changes/:id', ChangeAction
|
20
|
+
end
|
18
21
|
|
19
|
-
|
22
|
+
class ChangeAction < Sliver::Rails::Action
|
23
|
+
def call
|
24
|
+
change = Change.find path_params['id']
|
20
25
|
|
21
|
-
|
26
|
+
response.status = 200
|
27
|
+
response.body = ["Change: #{change.name}"]
|
28
|
+
end
|
29
|
+
end
|
30
|
+
```
|
31
|
+
|
32
|
+
### Parameters
|
33
|
+
|
34
|
+
Just like in Rails controllers, all incoming request parameters are treated as strong parameters, so you can be explicit about what you're expecting to receive.
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
class V1::Changes::Create < Sliver::Rails::Action
|
38
|
+
def call
|
39
|
+
change = Change.create change_params
|
40
|
+
|
41
|
+
response.status = 200
|
42
|
+
response.body = ["Success"]
|
43
|
+
end
|
44
|
+
|
45
|
+
private
|
46
|
+
|
47
|
+
def change_params
|
48
|
+
params.fetch(:change, {}).permit(:name, :description)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
```
|
52
|
+
|
53
|
+
### JSON Requests
|
54
|
+
|
55
|
+
If the content-type of the request has been set to `application/json`, then the request body data sent through in POST and PUT requests is parsed and available via the `params` method.
|
56
|
+
|
57
|
+
### Inbuilt JSON Processor
|
58
|
+
|
59
|
+
Most modern APIs will return JSON-formatted data - and so a Sliver Processor is provided to automatically translate response bodies into JSON. So, in the example below, the response body will be transformed to `"{\"status\":\"OK\"}"`, and the Content-Type header will be set to `application/json`.
|
60
|
+
|
61
|
+
```ruby
|
62
|
+
class V1::Changes::Create < Sliver::Rails::Action
|
63
|
+
def self.processors
|
64
|
+
[Sliver::Rails::Processors::JSONProcessor]
|
65
|
+
end
|
66
|
+
|
67
|
+
def call
|
68
|
+
change = Change.create change_params
|
69
|
+
|
70
|
+
response.status = 200
|
71
|
+
response.body = {:status => "OK"}
|
72
|
+
end
|
73
|
+
|
74
|
+
private
|
75
|
+
|
76
|
+
def change_params
|
77
|
+
params.fetch(:change, {}).permit(:name, :description)
|
78
|
+
end
|
79
|
+
end
|
80
|
+
```
|
22
81
|
|
23
|
-
|
82
|
+
### JSON templates and exposed variables
|
83
|
+
|
84
|
+
The JSON Processor can also use a Jbuilder template (provided you've got the `jbuilder` gem in your Gemfile) via the `use_template` method. Similarly to [decent_exposure](https://github.com/voxdolo/decent_exposure) (although without most of the features), anything specified with a `expose` call is available in the template view.
|
85
|
+
|
86
|
+
```ruby
|
87
|
+
class V1::Changes::Create < Sliver::Rails::Action
|
88
|
+
# This will use app/views/api/changes/show.jbuilder
|
89
|
+
use_template 'api/changes/show'
|
90
|
+
|
91
|
+
expose(:change) { Change.new change_params }
|
92
|
+
|
93
|
+
def self.processors
|
94
|
+
[Sliver::Rails::Processors::JSONProcessor]
|
95
|
+
end
|
96
|
+
|
97
|
+
def call
|
98
|
+
response.status = 200 if change.save
|
99
|
+
|
100
|
+
# If you use response.body, the template will not be used.
|
101
|
+
# response.body = {:status => 'OK'}
|
102
|
+
end
|
103
|
+
|
104
|
+
private
|
105
|
+
|
106
|
+
def change_params
|
107
|
+
params.fetch(:change, {}).permit(:name, :description)
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
# app/views/api/changes/show.jbuilder
|
112
|
+
json.(change, :name, :description)
|
113
|
+
```
|
114
|
+
|
115
|
+
This is a very simple example, but your Jbuilder templates can be as complex as you like. Blocks provided for `expose` will only be evaluated once and remembered - and if no block is provided, it looks for a private method of the same name (which is also only evaluated once when referenced from the template).
|
116
|
+
|
117
|
+
The default template is an instance of `Sliver::Rails::JBuilderTemplate`, which has one method available beyond what you've exposed: `routes`, which provides access to your Rails route helper methods. No other Rails helper methods are provided by default, but you can use your own template if you'd like:
|
118
|
+
|
119
|
+
```ruby
|
120
|
+
# config/initializers/sliver.rb
|
121
|
+
class ApiTemplate
|
122
|
+
include ActionView::Helpers::NumberHelper
|
123
|
+
|
124
|
+
def routes
|
125
|
+
Rails.application.routes
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
Rails.application.config.after_initialize do
|
130
|
+
Sliver::Rails.template_class = ApiTemplate
|
131
|
+
end
|
132
|
+
```
|
133
|
+
|
134
|
+
### Common directory and file structures
|
135
|
+
|
136
|
+
This is not enforced by this gem, but it's a habit we at [Inspire9](http://inspire9dev.com) have fallen into:
|
137
|
+
|
138
|
+
* Separate APIs for separate versions.
|
139
|
+
* `app/apis/v1.rb` contains the API definition
|
140
|
+
* `app/apis/v1/posts/create.rb`, `app/apis/v1/posts/show.rb`, `app/apis/v1/posts/index.rb` and so forth covering CRUD behaviour for resources.
|
141
|
+
* All endpoint classes inherit from an app-specific base class (covering things like `current_user`, setting guards and processors), and that base class inherits from `Sliver::Rails::Action`.
|
24
142
|
|
25
143
|
## Contributing
|
26
144
|
|
27
|
-
|
145
|
+
Please note that this project now has a [Contributor Code of Conduct](http://contributor-covenant.org/version/1/0/0/). By participating in this project you agree to abide by its terms.
|
146
|
+
|
147
|
+
1. Fork it ( https://github.com/pat/sliver-rails/fork )
|
28
148
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
29
149
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
30
150
|
4. Push to the branch (`git push origin my-new-feature`)
|
data/sliver-rails.gemspec
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sliver-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Pat Allan
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-09-
|
11
|
+
date: 2015-09-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: sliver
|