restfulness 0.2.3 → 0.2.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +29 -1
- data/lib/restfulness/request.rb +5 -0
- data/lib/restfulness/resource.rb +11 -0
- data/lib/restfulness/version.rb +1 -1
- data/lib/restfulness.rb +2 -0
- data/restfulness.gemspec +1 -0
- data/spec/spec_helper.rb +3 -0
- data/spec/unit/request_spec.rb +11 -0
- data/spec/unit/resource_spec.rb +40 -1
- metadata +16 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cfa898034332002c6aca448027bc52cf29abfbd7
|
4
|
+
data.tar.gz: 17961f1c4475131c07321a775575b7c32f66360a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 493a6d814a922764739e3698bea3a8bf16573bb207f940b0f240f7e70a27c50a9607cc2a3e5d7ad2053ddad95593109fd8370e591e07669cbbd0f6ec23d6185c
|
7
|
+
data.tar.gz: a62d90e70f22d2db78e0df083ecaf77c8fa769b6c2372874ad0b88b544cd963a95d8d5ac35d6d064f2aac6d6c56afcba6a7ce1969f20f27b493394c4723ae109
|
data/README.md
CHANGED
@@ -255,6 +255,30 @@ class ProjectResource < Restfulness::Resource
|
|
255
255
|
end
|
256
256
|
```
|
257
257
|
|
258
|
+
#### I18n in Resources
|
259
|
+
|
260
|
+
Restfulness uses the [http_accept_language](https://github.com/iain/http_accept_language) gem to automatically handle the `Accept-Language` header coming in from a client. After trying to make a match between the available locales, it will automatically set the `I18n.locale`. You can access the http_accept_language parser via the `request.http_accept_language` method.
|
261
|
+
|
262
|
+
For most APIs this should work great, especially for mobile applications where this header is automatically set by the phone. There may however be situations where you need a bit more control. If a user has a preferred language setting for example.
|
263
|
+
|
264
|
+
Resources contain two protected methods that can be overwritten if you need more precise control. This is what they look like in the Restfulness code:
|
265
|
+
|
266
|
+
```ruby
|
267
|
+
protected
|
268
|
+
|
269
|
+
def locale
|
270
|
+
request.http_accept_language.compatible_language_from(I18n.available_locales)
|
271
|
+
end
|
272
|
+
|
273
|
+
def set_locale
|
274
|
+
I18n.locale = locale
|
275
|
+
end
|
276
|
+
```
|
277
|
+
|
278
|
+
The `Resource#set_locale` method is called before any of the other callbacks are handled. This is important as it allows the locale to be set before returning any translatable error messages.
|
279
|
+
|
280
|
+
Most users will probably just want to override the `Resource#locale` method and provide the appropriate locale for the request. If you are using a User object or similar, double check your authentication process as the default `authorized?` method will be called *after* the locale is prepared.
|
281
|
+
|
258
282
|
|
259
283
|
### Requests
|
260
284
|
|
@@ -565,7 +589,11 @@ Restfulness is still a work in progress but at Cabify we are using it in product
|
|
565
589
|
|
566
590
|
## History
|
567
591
|
|
568
|
-
### 0.2.
|
592
|
+
### 0.2.4 - February 7, 2014
|
593
|
+
|
594
|
+
* Added I18n support with the help of the http_accept_language gem. (@samlown)
|
595
|
+
|
596
|
+
### 0.2.3 - February 6, 2014
|
569
597
|
|
570
598
|
* Fixing issue where query parameters are set as Hash instead of HashWithIndifferentAccess.
|
571
599
|
* Rewinding the body, incase rails got there first.
|
data/lib/restfulness/request.rb
CHANGED
@@ -73,6 +73,11 @@ module Restfulness
|
|
73
73
|
@sanitized_params ||= @params ? Sanitizer.sanitize_hash(@params) : nil
|
74
74
|
end
|
75
75
|
|
76
|
+
# Provide a wrapper for the http_accept_language parser
|
77
|
+
def http_accept_language
|
78
|
+
@http_accept_language = HttpAcceptLanguage::Parser.new(headers[:accept_language])
|
79
|
+
end
|
80
|
+
|
76
81
|
[:get, :post, :put, :patch, :delete, :head, :options].each do |m|
|
77
82
|
define_method("#{m}?") do
|
78
83
|
action == m
|
data/lib/restfulness/resource.rb
CHANGED
@@ -51,6 +51,9 @@ module Restfulness
|
|
51
51
|
end
|
52
52
|
|
53
53
|
def check_callbacks
|
54
|
+
# Locale Handling
|
55
|
+
set_locale
|
56
|
+
|
54
57
|
# Access control
|
55
58
|
method_not_allowed! unless method_allowed?
|
56
59
|
unauthorized! unless authorized?
|
@@ -70,6 +73,14 @@ module Restfulness
|
|
70
73
|
|
71
74
|
protected
|
72
75
|
|
76
|
+
def locale
|
77
|
+
request.http_accept_language.compatible_language_from(I18n.available_locales)
|
78
|
+
end
|
79
|
+
|
80
|
+
def set_locale
|
81
|
+
I18n.locale = locale
|
82
|
+
end
|
83
|
+
|
73
84
|
def logger
|
74
85
|
Restfulness.logger
|
75
86
|
end
|
data/lib/restfulness/version.rb
CHANGED
data/lib/restfulness.rb
CHANGED
data/restfulness.gemspec
CHANGED
@@ -21,6 +21,7 @@ Gem::Specification.new do |spec|
|
|
21
21
|
spec.add_dependency "rack", "~> 1.4"
|
22
22
|
spec.add_dependency "multi_json", "~> 1.8"
|
23
23
|
spec.add_dependency "activesupport", ">= 3.1"
|
24
|
+
spec.add_dependency "http_accept_language", "~> 2.0"
|
24
25
|
|
25
26
|
spec.add_development_dependency "bundler", "~> 1.3"
|
26
27
|
spec.add_development_dependency "rake"
|
data/spec/spec_helper.rb
CHANGED
data/spec/unit/request_spec.rb
CHANGED
@@ -169,6 +169,17 @@ describe Restfulness::Request do
|
|
169
169
|
end
|
170
170
|
end
|
171
171
|
|
172
|
+
describe "#http_accept_language" do
|
173
|
+
it "should provide an instance of Parser" do
|
174
|
+
obj.http_accept_language.should be_a(HttpAcceptLanguage::Parser)
|
175
|
+
end
|
176
|
+
it "should use the accept_language header" do
|
177
|
+
header = "en-us,en-gb;q=0.8,en"
|
178
|
+
obj.headers[:accept_language] = header
|
179
|
+
obj.http_accept_language.header.should eql(header)
|
180
|
+
end
|
181
|
+
end
|
182
|
+
|
172
183
|
describe "method helpers" do
|
173
184
|
it "should respond to method questions" do
|
174
185
|
[:get?, :post?, :put?, :delete?, :head?, :options?].each do |q|
|
data/spec/unit/resource_spec.rb
CHANGED
@@ -121,7 +121,7 @@ describe Restfulness::Resource do
|
|
121
121
|
|
122
122
|
let :obj do
|
123
123
|
request.action = :get
|
124
|
-
|
124
|
+
resource.new(request, response)
|
125
125
|
end
|
126
126
|
|
127
127
|
it "should all be good by default" do
|
@@ -130,6 +130,11 @@ describe Restfulness::Resource do
|
|
130
130
|
}.to_not raise_error
|
131
131
|
end
|
132
132
|
|
133
|
+
it "should try to set the locale" do
|
134
|
+
obj.should_receive(:set_locale)
|
135
|
+
obj.check_callbacks
|
136
|
+
end
|
137
|
+
|
133
138
|
it "should raise error on invalid method" do
|
134
139
|
obj.stub(:method_allowed?).and_return(false)
|
135
140
|
expect {
|
@@ -227,4 +232,38 @@ describe Restfulness::Resource do
|
|
227
232
|
end
|
228
233
|
end
|
229
234
|
|
235
|
+
describe "Locale handling" do
|
236
|
+
|
237
|
+
let :resource do
|
238
|
+
Class.new(GetPostResource) do
|
239
|
+
def head; nil; end
|
240
|
+
def put; nil; end
|
241
|
+
def delete; nil; end
|
242
|
+
end
|
243
|
+
end
|
244
|
+
|
245
|
+
let :obj do
|
246
|
+
request.headers[:accept_language] = "nl, es, en"
|
247
|
+
request.action = :get
|
248
|
+
resource.new(request, response)
|
249
|
+
end
|
250
|
+
|
251
|
+
describe "#locale" do
|
252
|
+
it "should return acceptable locale" do
|
253
|
+
I18n.available_locales = ['es']
|
254
|
+
obj.send(:locale).should eql(:es)
|
255
|
+
end
|
256
|
+
end
|
257
|
+
|
258
|
+
describe "#set_locale" do
|
259
|
+
it "should set the global locale value" do
|
260
|
+
I18n.available_locales = ['en', 'es']
|
261
|
+
I18n.locale.should_not eql(:es)
|
262
|
+
obj.send(:set_locale)
|
263
|
+
I18n.locale.should eql(:es)
|
264
|
+
end
|
265
|
+
end
|
266
|
+
|
267
|
+
end
|
268
|
+
|
230
269
|
end
|
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.2.
|
4
|
+
version: 0.2.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sam Lown
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-02-
|
11
|
+
date: 2014-02-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rack
|
@@ -52,6 +52,20 @@ dependencies:
|
|
52
52
|
- - '>='
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '3.1'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: http_accept_language
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '2.0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ~>
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '2.0'
|
55
69
|
- !ruby/object:Gem::Dependency
|
56
70
|
name: bundler
|
57
71
|
requirement: !ruby/object:Gem::Requirement
|