restfulness 0.3.1 → 0.3.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +27 -7
- data/lib/restfulness/request.rb +25 -9
- data/lib/restfulness/version.rb +1 -1
- data/restfulness.gemspec +1 -1
- data/spec/unit/request_spec.rb +33 -1
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8b7f899ce27987a0fc7cd6bfd1d272e999921b4a
|
4
|
+
data.tar.gz: 4c37afe53ef267decffc2d72b7fab1b5407bc2ae
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 099b1309377a645b8fad3c13116aa5e65d87a43dc0caa4987a05e1b7aefce60a6265e6f0911c7728675e99d5bed40ec2b7c9aab5d717618261f45b23413c23b9
|
7
|
+
data.tar.gz: 0fa3111acd852dfec3d0b3fe14ab02eae9b3288dcd7e32e81da062072f4e6ac3888aebe6b63c958652ea2beac1954d322f16ae60b6ac4d5a185e28cbe0cf7256
|
data/README.md
CHANGED
@@ -318,9 +318,8 @@ Here's an example with the authentication details in the code, you'd obviously w
|
|
318
318
|
```ruby
|
319
319
|
def authorized?
|
320
320
|
authenticate_with_http_basic do |username, password|
|
321
|
-
|
321
|
+
username == 'user' && password == 'pass'
|
322
322
|
end
|
323
|
-
false
|
324
323
|
end
|
325
324
|
```
|
326
325
|
|
@@ -329,16 +328,31 @@ The `request` object provided in the resource, described below, provides access
|
|
329
328
|
```ruby
|
330
329
|
def authorized?
|
331
330
|
auth = request.authorization
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
331
|
+
auth && (auth.schema == 'Token') && (auth.params == our_secret_token)
|
332
|
+
end
|
333
|
+
```
|
334
|
+
|
335
|
+
We don't yet provide support for Digest authentication, but your contributions would be more than welcome. Checkout the [HttpAuthentication/basic.rb](https://github.com/samlown/restfulness/blob/master/lib/restfulness/http_authentication/basic.rb) source for an example.
|
336
|
+
|
337
|
+
Restfulness doesn't make any provisions for requesting authentication from the client as most APIs don't really need to offer this functionality. You can acheive the same effect however by providing the `WWW-Authenticate` header in the response. For example:
|
338
|
+
|
339
|
+
```ruby
|
340
|
+
def authorized?
|
341
|
+
authorize_with_http_basic || request_authentication
|
342
|
+
end
|
343
|
+
|
344
|
+
def authorize_with_http_basic
|
345
|
+
authenticate_with_http_basic do |username, password|
|
346
|
+
username == 'user' && password == 'pass'
|
336
347
|
end
|
348
|
+
end
|
349
|
+
|
350
|
+
def request_authentication
|
351
|
+
response.headers['WWW-Authenticate'] = 'Basic realm="My Realm"'
|
337
352
|
false
|
338
353
|
end
|
339
354
|
```
|
340
355
|
|
341
|
-
We don't yet provide support for Digest authentication, but your contributions would be more than welcome. Checkout the [HttpAuthentication/basic.rb](https://github.com/samlown/restfulness/blob/master/lib/restfulness/http_authentication/basic.rb) source for an example.
|
342
356
|
|
343
357
|
### Requests
|
344
358
|
|
@@ -649,6 +663,12 @@ Restfulness is still a work in progress but at Cabify we are using it in product
|
|
649
663
|
|
650
664
|
## History
|
651
665
|
|
666
|
+
### 0.3.2 - February 9, 2015
|
667
|
+
|
668
|
+
* Added support for application/x-www-form-urlencoded parameter decoding (@samlown)
|
669
|
+
* Support for empty StringIOs when accessing Request#params (@samlown)
|
670
|
+
* Fixing at Rack ~> 1.5.0 due to issues with Rack 1.6 (@samlown)
|
671
|
+
|
652
672
|
### 0.3.1 - September 19, 2014
|
653
673
|
|
654
674
|
* Added support for HTTP Basic Authentication, no breaking changes. (@samlown)
|
data/lib/restfulness/request.rb
CHANGED
@@ -56,16 +56,19 @@ module Restfulness
|
|
56
56
|
end
|
57
57
|
|
58
58
|
def params
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
59
|
+
@params ||= begin
|
60
|
+
if body.nil? || body.length == 0
|
61
|
+
{}
|
62
|
+
else
|
63
|
+
case headers[:content_type]
|
64
|
+
when /application\/json/
|
65
|
+
@params = params_from_json(body)
|
66
|
+
when /application\/x\-www\-form\-urlencoded/
|
67
|
+
@params = params_from_form(body)
|
68
|
+
else
|
69
|
+
raise HTTPException.new(406)
|
66
70
|
end
|
67
|
-
|
68
|
-
raise HTTPException.new(406)
|
71
|
+
end
|
69
72
|
end
|
70
73
|
end
|
71
74
|
|
@@ -85,5 +88,18 @@ module Restfulness
|
|
85
88
|
end
|
86
89
|
end
|
87
90
|
|
91
|
+
protected
|
92
|
+
|
93
|
+
def params_from_json(body)
|
94
|
+
MultiJson.decode(body)
|
95
|
+
rescue MultiJson::LoadError
|
96
|
+
raise HTTPException.new(400)
|
97
|
+
end
|
98
|
+
|
99
|
+
def params_from_form(body)
|
100
|
+
# Sometimes the body can be a StringIO
|
101
|
+
Rack::Utils.parse_query(body.is_a?(StringIO) ? body.read : body)
|
102
|
+
end
|
103
|
+
|
88
104
|
end
|
89
105
|
end
|
data/lib/restfulness/version.rb
CHANGED
data/restfulness.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 "rack", "~> 1.
|
21
|
+
spec.add_dependency "rack", "~> 1.5.0"
|
22
22
|
spec.add_dependency "multi_json", "~> 1.8"
|
23
23
|
spec.add_dependency "activesupport", ">= 3.1"
|
24
24
|
spec.add_dependency "http_accept_language", "~> 2.0"
|
data/spec/unit/request_spec.rb
CHANGED
@@ -116,7 +116,7 @@ describe Restfulness::Request do
|
|
116
116
|
describe "#params" do
|
117
117
|
it "should not return anything for empty body" do
|
118
118
|
obj.stub(:body).and_return(nil)
|
119
|
-
obj.params.should
|
119
|
+
obj.params.should be_empty
|
120
120
|
end
|
121
121
|
|
122
122
|
it "should raise 400 bad request for invalid json body" do
|
@@ -152,6 +152,38 @@ describe Restfulness::Request do
|
|
152
152
|
obj.body = "{\"foo\":\"bar\"}"
|
153
153
|
obj.params['foo'].should eql('bar')
|
154
154
|
end
|
155
|
+
|
156
|
+
it "should decode a WWW Form body" do
|
157
|
+
obj.headers[:content_type] = "application/x-www-form-urlencoded"
|
158
|
+
obj.body = "grant_type=password&username=johndoe&password=A3ddj3w"
|
159
|
+
obj.params['grant_type'].should eql('password')
|
160
|
+
obj.params['username'].should eql('johndoe')
|
161
|
+
end
|
162
|
+
|
163
|
+
it "should deal with empty WWW Form body" do
|
164
|
+
obj.headers[:content_type] = "application/x-www-form-urlencoded"
|
165
|
+
obj.body = ""
|
166
|
+
obj.params.should be_empty
|
167
|
+
end
|
168
|
+
|
169
|
+
it "should deal with a StringIO WWW form body" do
|
170
|
+
obj.headers[:content_type] = "application/x-www-form-urlencoded"
|
171
|
+
obj.body = StringIO.new("grant_type=password&username=johndoe&password=A3ddj3w")
|
172
|
+
obj.params['grant_type'].should eql('password')
|
173
|
+
obj.params['username'].should eql('johndoe')
|
174
|
+
end
|
175
|
+
|
176
|
+
it "should deal with empty JSON String body" do
|
177
|
+
obj.headers[:content_type] = "application/json"
|
178
|
+
obj.body = ""
|
179
|
+
obj.params.should be_empty
|
180
|
+
end
|
181
|
+
|
182
|
+
it "should deal with empty JSON StringIO body" do
|
183
|
+
obj.headers[:content_type] = "application/json"
|
184
|
+
obj.body = StringIO.new("")
|
185
|
+
obj.params.should be_empty
|
186
|
+
end
|
155
187
|
end
|
156
188
|
|
157
189
|
describe "#sanitized_params" do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: restfulness
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sam Lown
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-02-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rack
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
19
|
+
version: 1.5.0
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version:
|
26
|
+
version: 1.5.0
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: multi_json
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|