restful_resource 1.6.0 → 2.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +22 -0
- data/lib/restful_resource/base.rb +28 -11
- data/lib/restful_resource/http_client.rb +9 -11
- data/lib/restful_resource/instrumentation.rb +6 -3
- data/lib/restful_resource/rails_validations.rb +4 -4
- data/lib/restful_resource/redirections.rb +4 -4
- data/lib/restful_resource/request.rb +25 -3
- data/lib/restful_resource/version.rb +1 -1
- data/restful_resource.gemspec +1 -0
- data/spec/restful_resource/base_spec.rb +90 -2
- data/spec/restful_resource/http_client_spec.rb +14 -3
- data/spec/restful_resource/rails_validations_spec.rb +2 -2
- data/spec/restful_resource/redirections_spec.rb +3 -3
- data/spec/restful_resource/request_spec.rb +48 -0
- data/spec/spec_helper.rb +12 -11
- metadata +18 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2e1eacf50c2ea555b6163b0d5a788f1460dc26f4
|
4
|
+
data.tar.gz: c04d86771984bd9ccd61e67bfa38cdc49c9d2fd8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 450a24de450adc7b8c27d3ff5f91c2c9c9a3cc04d61a4ac5bdd2eae71df2dcec28bfcd359091231e841f9911726fbca8c2b15d45c9b5752a11173a6867f824ce
|
7
|
+
data.tar.gz: e79f1804f0469e64d5f2c78ed0394179fea08ae869fabc4109af62d8e762472956041f76da9d547be3da9b7703217765597f9f8b412228efa2b3cb472b21479c
|
data/README.md
CHANGED
@@ -2,6 +2,28 @@
|
|
2
2
|
|
3
3
|
Provides an ActiveResource like interface to JSON API's
|
4
4
|
|
5
|
+
## Caching
|
6
|
+
|
7
|
+
Caching using [faraday-http-cache](https://github.com/plataformatec/faraday-http-cache)
|
8
|
+
|
9
|
+
Enabled by passing an initialsed cache object (eg Rails.cache)
|
10
|
+
|
11
|
+
```
|
12
|
+
RestfulResource::Base.configure(
|
13
|
+
base_url: "http://my.api.com/",
|
14
|
+
cache_store: Rails.cache
|
15
|
+
)
|
16
|
+
```
|
17
|
+
|
18
|
+
### Bypassing the cache
|
19
|
+
|
20
|
+
To make requests that bypass the local HTTP cache use the `no_cache: true` option eg:
|
21
|
+
|
22
|
+
```
|
23
|
+
Object.find(1, no_cache: true)
|
24
|
+
```
|
25
|
+
|
26
|
+
|
5
27
|
## Metrics
|
6
28
|
|
7
29
|
### HTTP Metrics
|
@@ -23,42 +23,50 @@ module RestfulResource
|
|
23
23
|
end
|
24
24
|
|
25
25
|
def self.find(id, params={})
|
26
|
-
|
26
|
+
params_without_headers, headers = extract_headers!(params)
|
27
|
+
|
28
|
+
response = http.get(member_url(id, params_without_headers), headers: headers)
|
27
29
|
self.new(parse_json(response.body))
|
28
30
|
end
|
29
31
|
|
30
32
|
def self.where(params={})
|
31
|
-
|
32
|
-
|
33
|
+
params_without_headers, headers = extract_headers!(params)
|
34
|
+
|
35
|
+
url = collection_url(params_without_headers)
|
36
|
+
response = http.get(url, headers: headers)
|
33
37
|
self.paginate_response(response)
|
34
38
|
end
|
35
39
|
|
36
40
|
def self.get(params = {})
|
37
|
-
|
41
|
+
params_without_headers, headers = extract_headers!(params)
|
42
|
+
|
43
|
+
response = http.get(collection_url(params_without_headers), headers: headers)
|
38
44
|
self.new(parse_json(response.body))
|
39
45
|
end
|
40
46
|
|
41
47
|
def self.delete(id, **params)
|
42
|
-
|
48
|
+
headers = params.delete(:headers) || {}
|
49
|
+
|
50
|
+
response = http.delete(member_url(id, params), headers: headers)
|
43
51
|
RestfulResource::OpenObject.new(parse_json(response.body))
|
44
52
|
end
|
45
53
|
|
46
|
-
def self.put(id, data: {}, **params)
|
54
|
+
def self.put(id, data: {}, headers: {}, **params)
|
47
55
|
url = member_url(id, params)
|
48
|
-
response = http.put(url, data: data)
|
56
|
+
response = http.put(url, data: data, headers: headers)
|
49
57
|
self.new(parse_json(response.body))
|
50
58
|
end
|
51
59
|
|
52
|
-
def self.post(data: {}, **params)
|
60
|
+
def self.post(data: {}, headers: {}, **params)
|
53
61
|
url = collection_url(params)
|
54
62
|
|
55
|
-
response = http.post(url, data: data)
|
63
|
+
response = http.post(url, data: data, headers: headers)
|
56
64
|
|
57
65
|
self.new(parse_json(response.body))
|
58
66
|
end
|
59
67
|
|
60
|
-
def self.all
|
61
|
-
self.where
|
68
|
+
def self.all(params = {})
|
69
|
+
self.where(params)
|
62
70
|
end
|
63
71
|
|
64
72
|
def self.action(action_name)
|
@@ -104,6 +112,15 @@ module RestfulResource
|
|
104
112
|
end
|
105
113
|
|
106
114
|
private
|
115
|
+
|
116
|
+
def self.extract_headers!(params = {})
|
117
|
+
headers = params.delete(:headers) || {}
|
118
|
+
|
119
|
+
headers.merge!(cache_control: 'no-cache') if params.delete(:no_cache)
|
120
|
+
|
121
|
+
[params, headers]
|
122
|
+
end
|
123
|
+
|
107
124
|
def self.merge_url_paths(uri, *paths)
|
108
125
|
uri.merge(paths.compact.join('/')).to_s
|
109
126
|
end
|
@@ -83,20 +83,20 @@ module RestfulResource
|
|
83
83
|
end
|
84
84
|
end
|
85
85
|
|
86
|
-
def get(url,
|
87
|
-
http_request(Request.new(:get, url,
|
86
|
+
def get(url, headers: {})
|
87
|
+
http_request(Request.new(:get, url, headers: headers))
|
88
88
|
end
|
89
89
|
|
90
|
-
def delete(url,
|
91
|
-
http_request(Request.new(:delete, url,
|
90
|
+
def delete(url, headers: {})
|
91
|
+
http_request(Request.new(:delete, url, headers: headers))
|
92
92
|
end
|
93
93
|
|
94
|
-
def put(url, data: {},
|
95
|
-
http_request(Request.new(:put, url, body: data,
|
94
|
+
def put(url, data: {}, headers: {})
|
95
|
+
http_request(Request.new(:put, url, body: data, headers: headers))
|
96
96
|
end
|
97
97
|
|
98
|
-
def post(url, data: {},
|
99
|
-
http_request(Request.new(:post, url, body: data,
|
98
|
+
def post(url, data: {}, headers: {})
|
99
|
+
http_request(Request.new(:post, url, body: data, headers: headers))
|
100
100
|
end
|
101
101
|
|
102
102
|
private
|
@@ -141,9 +141,7 @@ module RestfulResource
|
|
141
141
|
req.body = request.body unless request.body.nil?
|
142
142
|
req.url request.url
|
143
143
|
|
144
|
-
|
145
|
-
req.headers['Accept'] = request.accept
|
146
|
-
end
|
144
|
+
req.headers = req.headers.merge(request.headers)
|
147
145
|
end
|
148
146
|
Response.new(body: response.body, headers: response.headers, status: response.status)
|
149
147
|
rescue Faraday::ConnectionFailed
|
@@ -42,7 +42,7 @@ module RestfulResource
|
|
42
42
|
# Subscribes to events from Faraday::HttpCache
|
43
43
|
ActiveSupport::Notifications.subscribe cache_instrument_name do |*args|
|
44
44
|
event = ActiveSupport::Notifications::Event.new(*args)
|
45
|
-
cache_status = event.payload
|
45
|
+
cache_status = event.payload.fetch(:cache_status, nil)
|
46
46
|
|
47
47
|
# Outputs log lines like:
|
48
48
|
# count#quotes_site.research_site_api.cache_hit=1
|
@@ -55,6 +55,9 @@ module RestfulResource
|
|
55
55
|
metric_class.count cache_notifier_namespace(metric: 'cache_miss'), 1
|
56
56
|
metric_class.count cache_notifier_namespace(metric: 'cache_miss', event: event), 1
|
57
57
|
when :unacceptable
|
58
|
+
metric_class.count cache_notifier_namespace(metric: 'cache_not_cacheable'), 1
|
59
|
+
metric_class.count cache_notifier_namespace(metric: 'cache_not_cacheable', event: event), 1
|
60
|
+
when :bypass
|
58
61
|
metric_class.count cache_notifier_namespace(metric: 'cache_bypass'), 1
|
59
62
|
metric_class.count cache_notifier_namespace(metric: 'cache_bypass', event: event), 1
|
60
63
|
end
|
@@ -62,13 +65,13 @@ module RestfulResource
|
|
62
65
|
end
|
63
66
|
|
64
67
|
def validate_metric_class!
|
65
|
-
metric_methods =
|
68
|
+
metric_methods = %i(count sample measure)
|
66
69
|
if metric_methods.any? {|m| !metric_class.respond_to?(m) }
|
67
70
|
raise ArgumentError.new "Metric class '#{metric_class}' does not respond to #{metric_methods.join ','}"
|
68
71
|
end
|
69
72
|
end
|
70
73
|
|
71
|
-
def cache_notifier_namespace(event: nil
|
74
|
+
def cache_notifier_namespace(metric:, event: nil)
|
72
75
|
[app_name, api_name, base_request_path(event), metric].compact.join('.')
|
73
76
|
end
|
74
77
|
|
@@ -1,9 +1,9 @@
|
|
1
1
|
module RestfulResource
|
2
2
|
module RailsValidations
|
3
3
|
module ClassMethods
|
4
|
-
def put(id, data: {}, **params)
|
4
|
+
def put(id, data: {}, headers: {}, **params)
|
5
5
|
begin
|
6
|
-
super(id, data: data, **params)
|
6
|
+
super(id, data: data, headers: {}, **params)
|
7
7
|
rescue HttpClient::UnprocessableEntity => e
|
8
8
|
errors = parse_json(e.response.body)
|
9
9
|
result = nil
|
@@ -17,9 +17,9 @@ module RestfulResource
|
|
17
17
|
end
|
18
18
|
end
|
19
19
|
|
20
|
-
def post(data: {}, **params)
|
20
|
+
def post(data: {}, headers: {}, **params)
|
21
21
|
with_validations(data: data) do
|
22
|
-
super(data: data, **params)
|
22
|
+
super(data: data, headers: {}, **params)
|
23
23
|
end
|
24
24
|
end
|
25
25
|
|
@@ -8,10 +8,10 @@ module RestfulResource
|
|
8
8
|
module Redirections
|
9
9
|
def self.included(base)
|
10
10
|
base.instance_eval do
|
11
|
-
def post(data: {}, delay: 1.0, max_attempts: 10, **params)
|
11
|
+
def post(data: {}, delay: 1.0, max_attempts: 10, headers: {}, **params)
|
12
12
|
url = collection_url(params)
|
13
13
|
|
14
|
-
response = self.accept_redirected_result(response: http.post(url, data: data), delay: delay, max_attempts: max_attempts)
|
14
|
+
response = self.accept_redirected_result(response: http.post(url, data: data, headers: headers), delay: delay, max_attempts: max_attempts)
|
15
15
|
|
16
16
|
self.new(parse_json(response.body))
|
17
17
|
end
|
@@ -24,12 +24,12 @@ module RestfulResource
|
|
24
24
|
resource_location = response.headers[:location]
|
25
25
|
|
26
26
|
RestfulResource::Redirections.wait(delay)
|
27
|
-
new_response = http.get(resource_location)
|
27
|
+
new_response = http.get(resource_location, headers: {})
|
28
28
|
|
29
29
|
while (new_response.status == 202) && (attempts < max_attempts)
|
30
30
|
attempts += 1
|
31
31
|
RestfulResource::Redirections.wait(delay)
|
32
|
-
new_response = http.get(resource_location)
|
32
|
+
new_response = http.get(resource_location, headers: {})
|
33
33
|
end
|
34
34
|
|
35
35
|
if attempts == max_attempts
|
@@ -1,9 +1,31 @@
|
|
1
1
|
module RestfulResource
|
2
2
|
class Request
|
3
|
-
attr_reader :body, :method, :url
|
3
|
+
attr_reader :body, :method, :url
|
4
4
|
|
5
|
-
def initialize(method, url,
|
6
|
-
@method, @url, @
|
5
|
+
def initialize(method, url, headers: {}, body: nil)
|
6
|
+
@method, @url, @headers, @body = method, url, headers, body
|
7
|
+
end
|
8
|
+
|
9
|
+
def headers
|
10
|
+
default_headers.merge(format_headers)
|
11
|
+
end
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
# Formats all keys in Word-Word format
|
16
|
+
def format_headers
|
17
|
+
@headers.stringify_keys.inject({}) do |h, key_with_value|
|
18
|
+
h[format_key key_with_value.first] = key_with_value.last
|
19
|
+
h
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def format_key(key)
|
24
|
+
key.humanize.split(' ').map(&:humanize).join('-')
|
25
|
+
end
|
26
|
+
|
27
|
+
def default_headers
|
28
|
+
{ 'Accept' => 'application/json' }
|
7
29
|
end
|
8
30
|
end
|
9
31
|
end
|
data/restful_resource.gemspec
CHANGED
@@ -22,6 +22,7 @@ Gem::Specification.new do |spec|
|
|
22
22
|
spec.add_development_dependency "bundler", "~> 1.3"
|
23
23
|
spec.add_development_dependency "rake"
|
24
24
|
spec.add_development_dependency "rspec", "~> 3.1"
|
25
|
+
spec.add_development_dependency "rspec-its"
|
25
26
|
|
26
27
|
spec.add_dependency "faraday"
|
27
28
|
spec.add_dependency "faraday_middleware"
|
@@ -1,7 +1,7 @@
|
|
1
1
|
require_relative '../spec_helper'
|
2
2
|
|
3
|
-
describe RestfulResource::Base do
|
4
|
-
before
|
3
|
+
RSpec.describe RestfulResource::Base do
|
4
|
+
before do
|
5
5
|
@mock_http = double("mock_http")
|
6
6
|
allow(RestfulResource::Base).to receive(:http).and_return(@mock_http)
|
7
7
|
RestfulResource::Base.configure(base_url: 'http://api.carwow.co.uk/')
|
@@ -49,6 +49,22 @@ describe RestfulResource::Base do
|
|
49
49
|
|
50
50
|
object = Model.find('Golf Cabriolet?test', make_slug: 'Land Rover?x=0.123', group_id: 'xxx yyy?l=7')
|
51
51
|
end
|
52
|
+
|
53
|
+
it 'accepts custom headers' do
|
54
|
+
expect_get("http://api.carwow.co.uk/makes/12",
|
55
|
+
RestfulResource::Response.new,
|
56
|
+
headers: { cache_control: 'no-cache' })
|
57
|
+
|
58
|
+
Make.find(12, headers: { cache_control: 'no-cache' })
|
59
|
+
end
|
60
|
+
|
61
|
+
it 'accepts no_cache option' do
|
62
|
+
expect_get("http://api.carwow.co.uk/makes/12",
|
63
|
+
RestfulResource::Response.new,
|
64
|
+
headers: { cache_control: 'no-cache' })
|
65
|
+
|
66
|
+
Make.find(12, no_cache: true)
|
67
|
+
end
|
52
68
|
end
|
53
69
|
|
54
70
|
describe "#where" do
|
@@ -74,6 +90,22 @@ describe RestfulResource::Base do
|
|
74
90
|
expect(models.previous_page).to be_nil
|
75
91
|
expect(models.next_page).to eq 2
|
76
92
|
end
|
93
|
+
|
94
|
+
it 'accepts custom headers' do
|
95
|
+
expect_get("http://api.carwow.co.uk/groups/15/makes/Volkswagen/models?on_sale=true",
|
96
|
+
RestfulResource::Response.new,
|
97
|
+
headers: { cache_control: 'no-cache' })
|
98
|
+
|
99
|
+
Model.where(make_slug: 'Volkswagen', on_sale: true, group_id: 15, headers: { cache_control: 'no-cache' })
|
100
|
+
end
|
101
|
+
|
102
|
+
it 'accepts no_cache option' do
|
103
|
+
expect_get("http://api.carwow.co.uk/groups/15/makes/Volkswagen/models?on_sale=true",
|
104
|
+
RestfulResource::Response.new,
|
105
|
+
headers: { cache_control: 'no-cache' })
|
106
|
+
|
107
|
+
Model.where(make_slug: 'Volkswagen', on_sale: true, group_id: 15, no_cache: true)
|
108
|
+
end
|
77
109
|
end
|
78
110
|
|
79
111
|
describe "#all" do
|
@@ -86,6 +118,22 @@ describe RestfulResource::Base do
|
|
86
118
|
expect(makes.length).to eq 2
|
87
119
|
expect(makes.first.name).to eq 'Volkswagen'
|
88
120
|
end
|
121
|
+
|
122
|
+
it 'accepts custom headers' do
|
123
|
+
expect_get("http://api.carwow.co.uk/makes",
|
124
|
+
RestfulResource::Response.new,
|
125
|
+
headers: { cache_control: 'no-cache' })
|
126
|
+
|
127
|
+
Make.all(headers: { cache_control: 'no-cache' })
|
128
|
+
end
|
129
|
+
|
130
|
+
it 'accepts no_cache option' do
|
131
|
+
expect_get("http://api.carwow.co.uk/makes",
|
132
|
+
RestfulResource::Response.new,
|
133
|
+
headers: { cache_control: 'no-cache' })
|
134
|
+
|
135
|
+
Make.all(no_cache: true)
|
136
|
+
end
|
89
137
|
end
|
90
138
|
|
91
139
|
describe "#base_url" do
|
@@ -130,6 +178,22 @@ describe RestfulResource::Base do
|
|
130
178
|
|
131
179
|
expect(object.average_score).to eq 4.3
|
132
180
|
end
|
181
|
+
|
182
|
+
it 'accepts custom headers' do
|
183
|
+
expect_get("http://api.carwow.co.uk/makes/average_score",
|
184
|
+
RestfulResource::Response.new,
|
185
|
+
headers: { cache_control: 'no-cache' })
|
186
|
+
|
187
|
+
Make.action(:average_score).get(headers: { cache_control: 'no-cache' })
|
188
|
+
end
|
189
|
+
|
190
|
+
it 'accepts no_cache option' do
|
191
|
+
expect_get("http://api.carwow.co.uk/makes/average_score",
|
192
|
+
RestfulResource::Response.new,
|
193
|
+
headers: { cache_control: 'no-cache' })
|
194
|
+
|
195
|
+
Make.action(:average_score).get(no_cache: true)
|
196
|
+
end
|
133
197
|
end
|
134
198
|
|
135
199
|
describe "#put" do
|
@@ -172,6 +236,14 @@ describe RestfulResource::Base do
|
|
172
236
|
|
173
237
|
expect(object.name).to eq 'Audi'
|
174
238
|
end
|
239
|
+
|
240
|
+
it 'accepts custom headers' do
|
241
|
+
expect_put("http://api.carwow.co.uk/makes/1",
|
242
|
+
RestfulResource::Response.new,
|
243
|
+
headers: { accept: 'application/json' })
|
244
|
+
|
245
|
+
Make.put(1, data: {}, headers: { accept: 'application/json' })
|
246
|
+
end
|
175
247
|
end
|
176
248
|
|
177
249
|
describe "#post" do
|
@@ -186,6 +258,14 @@ describe RestfulResource::Base do
|
|
186
258
|
expect(object.name).to eq 'Audi'
|
187
259
|
expect(object.num_of_cars).to eq 3
|
188
260
|
end
|
261
|
+
|
262
|
+
it 'accepts custom headers' do
|
263
|
+
expect_post("http://api.carwow.co.uk/makes",
|
264
|
+
RestfulResource::Response.new,
|
265
|
+
headers: { accept: 'application/json' })
|
266
|
+
|
267
|
+
Make.post(data: {}, headers: { accept: 'application/json' })
|
268
|
+
end
|
189
269
|
end
|
190
270
|
|
191
271
|
describe "#delete" do
|
@@ -197,6 +277,14 @@ describe RestfulResource::Base do
|
|
197
277
|
|
198
278
|
expect(object.deleted).to be_truthy
|
199
279
|
end
|
280
|
+
|
281
|
+
it 'accepts custom headers' do
|
282
|
+
expect_delete("http://api.carwow.co.uk/makes/1",
|
283
|
+
RestfulResource::Response.new,
|
284
|
+
headers: { accept: 'application/json' })
|
285
|
+
|
286
|
+
Make.delete(1, headers: { accept: 'application/json' })
|
287
|
+
end
|
200
288
|
end
|
201
289
|
|
202
290
|
describe ".as_json" do
|
@@ -1,6 +1,6 @@
|
|
1
1
|
require_relative '../spec_helper'
|
2
2
|
|
3
|
-
describe RestfulResource::HttpClient do
|
3
|
+
RSpec.describe RestfulResource::HttpClient do
|
4
4
|
def faraday_connection
|
5
5
|
Faraday.new do |builder|
|
6
6
|
builder.request :url_encoded
|
@@ -143,12 +143,23 @@ describe RestfulResource::HttpClient do
|
|
143
143
|
|
144
144
|
it 'should execute authenticated get' do
|
145
145
|
connection = faraday_connection do |stubs|
|
146
|
-
stubs.get('http://httpbin.org/basic-auth/user/passwd'
|
146
|
+
stubs.get('http://httpbin.org/basic-auth/user/passwd') { |env| [200, {}, nil] }
|
147
147
|
end
|
148
148
|
|
149
|
-
response = http_client(connection).get('http://httpbin.org/basic-auth/user/passwd')
|
149
|
+
response = http_client(connection).get('http://httpbin.org/basic-auth/user/passwd', headers: {"Authorization"=>"Basic dXNlcjpwYXNzd2Q="})
|
150
150
|
|
151
151
|
expect(response.status).to eq 200
|
152
152
|
end
|
153
153
|
end
|
154
|
+
|
155
|
+
describe 'Headers' do
|
156
|
+
it 'uses custom headers' do
|
157
|
+
connection = faraday_connection do |stubs|
|
158
|
+
stubs.get('http://httpbin.org/get', { 'Cache-Control' => 'no-cache' }) { |env| [200, {}, nil] }
|
159
|
+
end
|
160
|
+
|
161
|
+
response = http_client(connection).get('http://httpbin.org/get', headers: { cache_control: 'no-cache' })
|
162
|
+
expect(response.status).to eq 200
|
163
|
+
end
|
164
|
+
end
|
154
165
|
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
require_relative '../spec_helper'
|
2
2
|
|
3
|
-
describe RestfulResource::RailsValidations do
|
3
|
+
RSpec.describe RestfulResource::RailsValidations do
|
4
4
|
before :each do
|
5
5
|
@mock_http = double("mock_http")
|
6
6
|
RestfulResource::Base.configure(base_url: "http://api.carwow.co.uk/")
|
@@ -26,7 +26,7 @@ describe RestfulResource::RailsValidations do
|
|
26
26
|
end
|
27
27
|
|
28
28
|
context "#put with errors" do
|
29
|
-
before
|
29
|
+
before do
|
30
30
|
data = {name: 'Leonardo'}
|
31
31
|
@error = 'Cannot use Ninja Turtles names'
|
32
32
|
expected_response = RestfulResource::Response.new(body: {errors: [@error]}.to_json)
|
@@ -1,7 +1,7 @@
|
|
1
1
|
require_relative '../spec_helper'
|
2
2
|
|
3
|
-
describe RestfulResource::Redirections do
|
4
|
-
before
|
3
|
+
RSpec.describe RestfulResource::Redirections do
|
4
|
+
before do
|
5
5
|
@mock_http = double("mock_http")
|
6
6
|
allow(RestfulResource::Base).to receive(:http).and_return(@mock_http)
|
7
7
|
RestfulResource::Base.configure(base_url: 'http://api.carwow.co.uk/')
|
@@ -24,7 +24,7 @@ describe RestfulResource::Redirections do
|
|
24
24
|
context 'with a redirect to resource location' do
|
25
25
|
let(:redirect_target) { 'http://api.carwow.co.uk/model_with_redirections/123' }
|
26
26
|
|
27
|
-
before
|
27
|
+
before do
|
28
28
|
allow(RestfulResource::Redirections).to receive(:wait)
|
29
29
|
expected_redirect_response = RestfulResource::Response.new(body: 'You are being redirected', status: 303, headers: { location: redirect_target})
|
30
30
|
expect_post("http://api.carwow.co.uk/model_with_redirections", expected_redirect_response, data: data)
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe RestfulResource::Request do
|
4
|
+
let(:method) { 'get' }
|
5
|
+
let(:url) { 'https://www.carwow.co.uk/api/v2/models' }
|
6
|
+
|
7
|
+
describe '.headers' do
|
8
|
+
context 'given no headers' do
|
9
|
+
subject { described_class.new(method, url).headers }
|
10
|
+
|
11
|
+
its(['Accept']) { is_expected.to eq 'application/json' }
|
12
|
+
end
|
13
|
+
|
14
|
+
context 'given an explicit accept header' do
|
15
|
+
subject { described_class.new(method, url, headers: { accept: 'application/xml' }).headers }
|
16
|
+
|
17
|
+
its(['Accept']) { is_expected.to eq 'application/xml' }
|
18
|
+
end
|
19
|
+
|
20
|
+
context 'given symbolized header' do
|
21
|
+
subject { described_class.new(method, url, headers: { authorization: 'Bearer: xyz' }).headers }
|
22
|
+
|
23
|
+
its(['Authorization']) { is_expected.to eq 'Bearer: xyz' }
|
24
|
+
its(['Accept']) { is_expected.to eq 'application/json' }
|
25
|
+
end
|
26
|
+
|
27
|
+
context 'given multi word symbolized header' do
|
28
|
+
subject { described_class.new(method, url, headers: { cache_control: 'no-cache' }).headers }
|
29
|
+
|
30
|
+
its(['Cache-Control']) { is_expected.to eq 'no-cache' }
|
31
|
+
its(['Accept']) { is_expected.to eq 'application/json' }
|
32
|
+
end
|
33
|
+
|
34
|
+
context 'given string header' do
|
35
|
+
subject { described_class.new(method, url, headers: { 'authorization' => 'Bearer: xyz' }).headers }
|
36
|
+
|
37
|
+
its(['Authorization']) { is_expected.to eq 'Bearer: xyz' }
|
38
|
+
its(['Accept']) { is_expected.to eq 'application/json' }
|
39
|
+
end
|
40
|
+
|
41
|
+
context 'given multi word string header' do
|
42
|
+
subject { described_class.new(method, url, headers: { 'cache control' => 'no-cache' }).headers }
|
43
|
+
|
44
|
+
its(['Cache-Control']) { is_expected.to eq 'no-cache' }
|
45
|
+
its(['Accept']) { is_expected.to eq 'application/json' }
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'rspec'
|
2
|
+
require 'rspec/its'
|
2
3
|
require_relative '../lib/restful_resource'
|
3
4
|
require_relative 'fixtures'
|
4
5
|
|
@@ -8,39 +9,39 @@ RSpec.configure do |config|
|
|
8
9
|
end
|
9
10
|
|
10
11
|
|
11
|
-
def expect_get(url, response)
|
12
|
-
expect(@mock_http).to receive(:get).with(url).and_return(response)
|
12
|
+
def expect_get(url, response, headers: {})
|
13
|
+
expect(@mock_http).to receive(:get).with(url, headers: headers).and_return(response)
|
13
14
|
end
|
14
15
|
|
15
|
-
def expect_delete(url, response)
|
16
|
-
expect(@mock_http).to receive(:delete).with(url).and_return(response)
|
16
|
+
def expect_delete(url, response, headers: {})
|
17
|
+
expect(@mock_http).to receive(:delete).with(url, headers: headers).and_return(response)
|
17
18
|
end
|
18
19
|
|
19
|
-
def expect_put(url, response, data: {})
|
20
|
-
expect(@mock_http).to receive(:put).with(url, data: data).and_return(response)
|
20
|
+
def expect_put(url, response, data: {}, headers: {})
|
21
|
+
expect(@mock_http).to receive(:put).with(url, data: data, headers: headers).and_return(response)
|
21
22
|
end
|
22
23
|
|
23
|
-
def expect_post(url, response, data: {})
|
24
|
-
expect(@mock_http).to receive(:post).with(url, data: data).and_return(response)
|
24
|
+
def expect_post(url, response, data: {}, headers: {})
|
25
|
+
expect(@mock_http).to receive(:post).with(url, data: data, headers: headers).and_return(response)
|
25
26
|
end
|
26
27
|
|
27
28
|
def expect_get_with_unprocessable_entity(url, response)
|
28
29
|
request = RestfulResource::Request.new(:get, url)
|
29
30
|
rest_client_response = OpenStruct.new({body: response.body, headers: response.headers, code: response.status})
|
30
31
|
exception = RestfulResource::HttpClient::UnprocessableEntity.new(request, rest_client_response)
|
31
|
-
expect(@mock_http).to receive(:get).with(url).and_raise(exception)
|
32
|
+
expect(@mock_http).to receive(:get).with(url, headers: {}).and_raise(exception)
|
32
33
|
end
|
33
34
|
|
34
35
|
def expect_put_with_unprocessable_entity(url, response, data: {})
|
35
36
|
request = RestfulResource::Request.new(:put, url, body: data)
|
36
37
|
rest_client_response = OpenStruct.new({body: response.body, headers: response.headers, code: response.status})
|
37
38
|
exception = RestfulResource::HttpClient::UnprocessableEntity.new(request, rest_client_response)
|
38
|
-
expect(@mock_http).to receive(:put).with(url, data: data).and_raise(exception)
|
39
|
+
expect(@mock_http).to receive(:put).with(url, data: data, headers: {}).and_raise(exception)
|
39
40
|
end
|
40
41
|
|
41
42
|
def expect_post_with_unprocessable_entity(url, response, data: {})
|
42
43
|
request = RestfulResource::Request.new(:put, url, body: data)
|
43
44
|
rest_client_response = OpenStruct.new({body: response.body, headers: response.headers, code: response.status})
|
44
45
|
exception = RestfulResource::HttpClient::UnprocessableEntity.new(request, rest_client_response)
|
45
|
-
expect(@mock_http).to receive(:post).with(url, data: data).and_raise(exception)
|
46
|
+
expect(@mock_http).to receive(:post).with(url, data: data, headers: {}).and_raise(exception)
|
46
47
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: restful_resource
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- David Santoro
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2017-
|
12
|
+
date: 2017-10-13 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -53,6 +53,20 @@ dependencies:
|
|
53
53
|
- - "~>"
|
54
54
|
- !ruby/object:Gem::Version
|
55
55
|
version: '3.1'
|
56
|
+
- !ruby/object:Gem::Dependency
|
57
|
+
name: rspec-its
|
58
|
+
requirement: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
type: :development
|
64
|
+
prerelease: false
|
65
|
+
version_requirements: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
56
70
|
- !ruby/object:Gem::Dependency
|
57
71
|
name: faraday
|
58
72
|
requirement: !ruby/object:Gem::Requirement
|
@@ -208,6 +222,7 @@ files:
|
|
208
222
|
- spec/restful_resource/open_object_spec.rb
|
209
223
|
- spec/restful_resource/rails_validations_spec.rb
|
210
224
|
- spec/restful_resource/redirections_spec.rb
|
225
|
+
- spec/restful_resource/request_spec.rb
|
211
226
|
- spec/spec_helper.rb
|
212
227
|
homepage: http://www.github.com/carwow/restful_resource
|
213
228
|
licenses:
|
@@ -244,4 +259,5 @@ test_files:
|
|
244
259
|
- spec/restful_resource/open_object_spec.rb
|
245
260
|
- spec/restful_resource/rails_validations_spec.rb
|
246
261
|
- spec/restful_resource/redirections_spec.rb
|
262
|
+
- spec/restful_resource/request_spec.rb
|
247
263
|
- spec/spec_helper.rb
|