restfulness 0.3.5 → 0.3.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +6 -2
- data/lib/restfulness/headers/accept.rb +7 -0
- data/lib/restfulness/response.rb +36 -8
- data/lib/restfulness/version.rb +1 -1
- data/restfulness.gemspec +2 -0
- data/spec/unit/headers/accept_spec.rb +12 -1
- data/spec/unit/response_spec.rb +80 -0
- 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: 5def9b04aa7a9bc0950574e5d1c05b831efd1119
|
4
|
+
data.tar.gz: 159796ac977c49ae23069175b52e3e452bdb2eb1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b8264abbb1f84368c3481d1f00eb22200a7d525ad9702dd19ccce78da1c77f9d4fa5cf0daa160abf14839a7006610ab490280c5c3401821e42078a27816ee9bf
|
7
|
+
data.tar.gz: 8dd35a50d84c0465df049d4bf1648eb13d7ecee684528d63713ba1de0964779e8fe7c0e6940558dc3d5a680bfbce75d89d68866f3dd46a592569ad2d7f8f3324
|
data/README.md
CHANGED
@@ -698,14 +698,18 @@ The project is now awesome, thanks to contributions by:
|
|
698
698
|
|
699
699
|
Restfulness is still a work in progress but at Cabify we are using it in production. Here is a list of things that we'd like to improve or fix:
|
700
700
|
|
701
|
-
*
|
701
|
+
* More generic support for custom serializers and content types.
|
702
702
|
* Support path methods for automatic URL generation.
|
703
703
|
* Support redirect exceptions.
|
704
704
|
* Needs more functional testing.
|
705
|
-
* Support for before and after filters in resources, although I'm slightly apprehensive about this.
|
706
705
|
|
707
706
|
## History
|
708
707
|
|
708
|
+
### 0.3.6 - June 14, 2017
|
709
|
+
|
710
|
+
* Fixing issue for overriding response Content-Type headers (@samlown)
|
711
|
+
* Adding basic support for XML responses if requested in Accept headers (@samlown)
|
712
|
+
|
709
713
|
### 0.3.5 - February 21, 2017
|
710
714
|
|
711
715
|
* Rewind body after reading (@pacoguzman)
|
data/lib/restfulness/response.rb
CHANGED
@@ -69,21 +69,49 @@ module Restfulness
|
|
69
69
|
end
|
70
70
|
|
71
71
|
def payload=(body)
|
72
|
-
|
73
|
-
|
74
|
-
|
72
|
+
type = content_type_from_accept_header
|
73
|
+
if body.nil?
|
74
|
+
@payload = ""
|
75
|
+
elsif body.is_a?(String)
|
76
|
+
# Implies that the body was already prepared, and we should rely on accept headers or assume text
|
77
|
+
@payload = body
|
78
|
+
update_content_headers(type || :text) unless @payload.empty?
|
79
|
+
elsif type && type == :xml
|
80
|
+
# Try to use a #to_xml if available, or just use to_s.
|
81
|
+
@payload = (body.respond_to?(:to_xml) ? body.to_xml : body).to_s
|
82
|
+
update_content_headers(:xml) unless @payload.empty?
|
75
83
|
else
|
84
|
+
# DEFAULT: Assume we want JSON
|
76
85
|
@payload = MultiJson.encode(body)
|
77
86
|
update_content_headers(:json) unless @payload.empty?
|
78
87
|
end
|
79
88
|
end
|
80
89
|
|
90
|
+
def content_type_from_accept_header
|
91
|
+
accept = self.request.accept
|
92
|
+
if accept
|
93
|
+
if accept.json?
|
94
|
+
:json
|
95
|
+
elsif accept.xml?
|
96
|
+
:xml
|
97
|
+
elsif accept.text?
|
98
|
+
:text
|
99
|
+
end
|
100
|
+
else
|
101
|
+
nil
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
81
105
|
def update_content_headers(type = :json)
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
106
|
+
if headers['Content-Type'].to_s.empty?
|
107
|
+
case type
|
108
|
+
when :json
|
109
|
+
headers['Content-Type'] = 'application/json; charset=utf-8'
|
110
|
+
when :xml
|
111
|
+
headers['Content-Type'] = 'application/xml; charset=utf-8'
|
112
|
+
else # Assume text
|
113
|
+
headers['Content-Type'] = 'text/plain; charset=utf-8'
|
114
|
+
end
|
87
115
|
end
|
88
116
|
headers['Content-Length'] = content_length
|
89
117
|
end
|
data/lib/restfulness/version.rb
CHANGED
data/restfulness.gemspec
CHANGED
@@ -61,10 +61,21 @@ describe Restfulness::Headers::Accept do
|
|
61
61
|
obj = klass.new("text/plain, application/xml; version=1, text/*")
|
62
62
|
expect(obj.xml?).to be true
|
63
63
|
end
|
64
|
-
it "should confirm if
|
64
|
+
it "should confirm if xml not accepted" do
|
65
65
|
obj = klass.new("text/plain, application/json, text/*")
|
66
66
|
expect(obj.xml?).to be false
|
67
67
|
end
|
68
68
|
end
|
69
69
|
|
70
|
+
describe "#text?" do
|
71
|
+
it "should confirm if content includes text" do
|
72
|
+
obj = klass.new("text/plain, text/*")
|
73
|
+
expect(obj.text?).to be true
|
74
|
+
end
|
75
|
+
it "should confirm if text not accepted" do
|
76
|
+
obj = klass.new("application/json")
|
77
|
+
expect(obj.text?).to be false
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
70
81
|
end
|
data/spec/unit/response_spec.rb
CHANGED
@@ -4,6 +4,9 @@ require 'spec_helper'
|
|
4
4
|
describe Restfulness::Response do
|
5
5
|
|
6
6
|
class ResponseResource < Restfulness::Resource
|
7
|
+
def get
|
8
|
+
""
|
9
|
+
end
|
7
10
|
end
|
8
11
|
|
9
12
|
let :klass do
|
@@ -170,4 +173,81 @@ describe Restfulness::Response do
|
|
170
173
|
|
171
174
|
end
|
172
175
|
|
176
|
+
describe "content type handling" do
|
177
|
+
before :each do
|
178
|
+
request.uri = "http://localhost:3000/project"
|
179
|
+
request.action = :get
|
180
|
+
end
|
181
|
+
context "nil content" do
|
182
|
+
it "should not set content headers" do
|
183
|
+
allow_any_instance_of(ResponseResource).to receive(:get).and_return(nil)
|
184
|
+
obj.run
|
185
|
+
expect(obj.headers['Content-Type']).to be_nil
|
186
|
+
end
|
187
|
+
end
|
188
|
+
context "empty content" do
|
189
|
+
it "should not set content headers" do
|
190
|
+
allow_any_instance_of(ResponseResource).to receive(:get).and_return("")
|
191
|
+
obj.run
|
192
|
+
expect(obj.headers['Content-Type']).to be_nil
|
193
|
+
end
|
194
|
+
end
|
195
|
+
context "json requested with content" do
|
196
|
+
let :accept do
|
197
|
+
Restfulness::Headers::Accept.new("application/json")
|
198
|
+
end
|
199
|
+
it "should set json content headers" do
|
200
|
+
allow(request).to receive(:accept).and_return(accept)
|
201
|
+
allow_any_instance_of(ResponseResource).to receive(:get).and_return({foo: "bar"})
|
202
|
+
obj.run
|
203
|
+
expect(obj.headers['Content-Type']).to match(/application\/json/)
|
204
|
+
end
|
205
|
+
end
|
206
|
+
context "xml requested with content" do
|
207
|
+
let :accept do
|
208
|
+
Restfulness::Headers::Accept.new("application/xml")
|
209
|
+
end
|
210
|
+
it "should set xml content headers" do
|
211
|
+
allow(request).to receive(:accept).and_return(accept)
|
212
|
+
allow_any_instance_of(ResponseResource).to receive(:get).and_return({foo: "bar"})
|
213
|
+
obj.run
|
214
|
+
expect(obj.headers['Content-Type']).to match(/application\/xml/)
|
215
|
+
expect(obj.payload).to match("<?xml version=\"1.0\"")
|
216
|
+
end
|
217
|
+
it "should set xml content headers even if string provided by resource" do
|
218
|
+
allow(request).to receive(:accept).and_return(accept)
|
219
|
+
allow_any_instance_of(ResponseResource).to receive(:get).and_return({foo: "bar"}.to_xml.to_s)
|
220
|
+
obj.run
|
221
|
+
expect(obj.headers['Content-Type']).to match(/application\/xml/)
|
222
|
+
expect(obj.payload).to match("<?xml version=\"1.0\"")
|
223
|
+
end
|
224
|
+
end
|
225
|
+
context "string requested with content" do
|
226
|
+
let :accept do
|
227
|
+
Restfulness::Headers::Accept.new("text/plain")
|
228
|
+
end
|
229
|
+
it "should set xml content headers" do
|
230
|
+
allow(request).to receive(:accept).and_return(accept)
|
231
|
+
allow_any_instance_of(ResponseResource).to receive(:get).and_return({foo: "bar"}.to_s)
|
232
|
+
obj.run
|
233
|
+
expect(obj.headers['Content-Type']).to match("text/plain")
|
234
|
+
end
|
235
|
+
end
|
236
|
+
context "default with content" do
|
237
|
+
it "should set json content headers" do
|
238
|
+
allow_any_instance_of(ResponseResource).to receive(:get).and_return({foo: "bar"})
|
239
|
+
obj.run
|
240
|
+
expect(obj.headers['Content-Type']).to match(/application\/json/)
|
241
|
+
end
|
242
|
+
end
|
243
|
+
context "overriding" do
|
244
|
+
it "should allow the content type to be overriden" do
|
245
|
+
obj.headers['Content-Type'] = 'application/foo'
|
246
|
+
allow_any_instance_of(ResponseResource).to receive(:get).and_return({foo: "bar"})
|
247
|
+
obj.run
|
248
|
+
expect(obj.headers['Content-Type']).to match("application/foo")
|
249
|
+
end
|
250
|
+
end
|
251
|
+
end
|
252
|
+
|
173
253
|
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.3.
|
4
|
+
version: 0.3.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sam Lown
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-06-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rack
|
@@ -120,6 +120,20 @@ dependencies:
|
|
120
120
|
- - ">="
|
121
121
|
- !ruby/object:Gem::Version
|
122
122
|
version: '0'
|
123
|
+
- !ruby/object:Gem::Dependency
|
124
|
+
name: builder
|
125
|
+
requirement: !ruby/object:Gem::Requirement
|
126
|
+
requirements:
|
127
|
+
- - ">="
|
128
|
+
- !ruby/object:Gem::Version
|
129
|
+
version: '0'
|
130
|
+
type: :development
|
131
|
+
prerelease: false
|
132
|
+
version_requirements: !ruby/object:Gem::Requirement
|
133
|
+
requirements:
|
134
|
+
- - ">="
|
135
|
+
- !ruby/object:Gem::Version
|
136
|
+
version: '0'
|
123
137
|
description: Simple REST server that focuses on resources instead of routes.
|
124
138
|
email:
|
125
139
|
- me@samlown.com
|