sliver-rails 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f7c14d480287cbdc9a25447e5ceb56f4afb27fb9
4
- data.tar.gz: bd5d0f5640681685401e4530b25159139d8eb45f
3
+ metadata.gz: c3bc921a9806786fed1b48aa7721a6237c2c398e
4
+ data.tar.gz: 299172b1437ea180c5448ba28a911394e99e2a82
5
5
  SHA512:
6
- metadata.gz: c856f578505b93d71e73e6c45733414d71f1c382be0e5cb13fdb8e908ed6c2ffb7292dacab3a5d70870add10706b3821a9ec91c099a1abd35781e8c04fcdf5c5
7
- data.tar.gz: 15ae1661f31276d01692fda474dc30e05fca889e66603997943aab4ff5c1510765a45deeea461425e48ba382fc6b0b609be391e1753cdbc69c5b9a4464488dcf
6
+ metadata.gz: 845f43b7b7a51de4175e65e401027bcd99868fd4b7d4d4ac07674e7230f02ab8df999913e5cff24509746306b937b7430e80533b185653291dfa55f583ff3527
7
+ data.tar.gz: 53455f2c9b07af35a587433408101cd4c7d629d51abc069912ee2572075f1456bd00aa522d55fcae131a8f289a9abd71da9fa1891298fe74ea865d9a85b01b8c
data/README.md CHANGED
@@ -1,30 +1,150 @@
1
- # Sliver::Rails
1
+ # Sliver-Rails
2
2
 
3
- TODO: Write a gem description
3
+ Rails-focused extensions to the [Sliver](https://github.com/pat/sliver) API libary.
4
4
 
5
5
  ## Installation
6
6
 
7
- Add this line to your application's Gemfile:
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
- And then execute:
13
+ ## Usage
14
14
 
15
- $ bundle
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
- Or install it yourself as:
17
+ ```ruby
18
+ app = Sliver::API.new do |api|
19
+ api.connect :get, '/changes/:id', ChangeAction
20
+ end
18
21
 
19
- $ gem install sliver-rails
22
+ class ChangeAction < Sliver::Rails::Action
23
+ def call
24
+ change = Change.find path_params['id']
20
25
 
21
- ## Usage
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
- TODO: Write usage instructions here
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
- 1. Fork it ( https://github.com/[my-github-username]/sliver-rails/fork )
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`)
@@ -1,5 +1,5 @@
1
1
  class Sliver::Rails::JBuilderTemplate
2
2
  def routes
3
- Rails.application.routes
3
+ Rails.application.routes.url_helpers
4
4
  end
5
5
  end
@@ -1,7 +1,7 @@
1
1
  # coding: utf-8
2
2
  Gem::Specification.new do |spec|
3
3
  spec.name = 'sliver-rails'
4
- spec.version = '0.1.1'
4
+ spec.version = '0.2.0'
5
5
  spec.authors = ['Pat Allan']
6
6
  spec.email = ['pat@freelancing-gods.com']
7
7
  spec.summary = %q{Rails extensions for Sliver}
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.1.1
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-02 00:00:00.000000000 Z
11
+ date: 2015-09-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sliver