restful_resource 0.8.34 → 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/Gemfile +1 -1
- data/lib/restful_resource/base.rb +10 -10
- data/lib/restful_resource/http_client.rb +65 -19
- data/lib/restful_resource/version.rb +1 -1
- data/lib/restful_resource.rb +10 -10
- data/restful_resource.gemspec +2 -1
- data/spec/restful_resource/base_authorization_spec.rb +0 -16
- data/spec/restful_resource/base_spec.rb +0 -28
- data/spec/restful_resource/http_client_spec.rb +8 -2
- metadata +18 -9
- data/lib/restful_resource/old_base.rb +0 -172
- data/spec/restful_resource/old_base_spec.rb +0 -119
- data/spec/restful_resource/rest_client_spec.rb +0 -15
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 060b67be52d4bbec24a0eeb0c8a5732f76698925
|
4
|
+
data.tar.gz: b016396bcb6958c1538cab53e60698090bb9af2f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 05be9cb4721f526c9f38a7505b5af2c32aa8b0de9a8c769a252115e12654afed340cd24deff2e17fabae0e5d9cf2abea4729225395f2f9e9f1d474c2beb40088
|
7
|
+
data.tar.gz: 72bab68f1a2d40dd9221351e983ef2dfbc99f765dc803073a22322819b9626c1633fd6b78b2f334d5e0d60a21f31811cd7366fb3706eceef90561ee115a5194a
|
data/.gitignore
CHANGED
data/Gemfile
CHANGED
@@ -2,18 +2,18 @@ module RestfulResource
|
|
2
2
|
class Base < OpenObject
|
3
3
|
extend RestfulResource::Associations
|
4
4
|
|
5
|
-
def self.configure(base_url: nil,
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
auth = nil
|
5
|
+
def self.configure(base_url: nil,
|
6
|
+
username: nil,
|
7
|
+
password: nil,
|
8
|
+
logger: nil,
|
9
|
+
cache_store: nil)
|
11
10
|
|
12
|
-
|
13
|
-
auth = RestfulResource::Authorization.http_authorization(username, password)
|
14
|
-
end
|
11
|
+
@base_url = URI.parse(base_url)
|
15
12
|
|
16
|
-
@http = RestfulResource::HttpClient.new(
|
13
|
+
@http = RestfulResource::HttpClient.new(username: username,
|
14
|
+
password: password,
|
15
|
+
logger: logger,
|
16
|
+
cache_store: cache_store)
|
17
17
|
end
|
18
18
|
|
19
19
|
def self.resource_path(url)
|
@@ -1,43 +1,89 @@
|
|
1
1
|
module RestfulResource
|
2
2
|
class HttpClient
|
3
|
-
class UnprocessableEntity <
|
3
|
+
class UnprocessableEntity < StandardError
|
4
4
|
attr_reader :response
|
5
5
|
|
6
6
|
def initialize(response)
|
7
|
-
@response = Response.new(body: response
|
7
|
+
@response = Response.new(body: response[:body], headers: response[:headers], status: response[:status])
|
8
8
|
end
|
9
9
|
end
|
10
10
|
|
11
|
-
|
12
|
-
|
11
|
+
class ResourceNotFound < StandardError
|
12
|
+
def message
|
13
|
+
"404 Resource Not Found"
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
class OtherHttpError < StandardError
|
18
|
+
attr_reader :response
|
19
|
+
|
20
|
+
def initialize(response)
|
21
|
+
puts response
|
22
|
+
@response = Response.new(body: response[:body], headers: response[:headers], status: response[:status])
|
23
|
+
end
|
24
|
+
|
25
|
+
def message
|
26
|
+
"Http Error - Status code: #{response.status}"
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def initialize(username: nil, password: nil, logger: nil, cache_store: nil)
|
31
|
+
@client = Faraday.new do |b|
|
32
|
+
b.request :url_encoded
|
33
|
+
b.response :raise_error
|
34
|
+
|
35
|
+
if logger
|
36
|
+
b.response :logger, logger
|
37
|
+
end
|
38
|
+
|
39
|
+
if cache_store
|
40
|
+
b.user :http_cache, store: cache_store
|
41
|
+
end
|
42
|
+
|
43
|
+
if username.present? && password.present?
|
44
|
+
b.basic_auth username, password
|
45
|
+
end
|
46
|
+
|
47
|
+
b.adapter :net_http
|
48
|
+
end
|
13
49
|
end
|
14
50
|
|
15
51
|
def get(url)
|
16
|
-
|
17
|
-
Response.new(body: response.body, headers: response.headers, status: response.code)
|
52
|
+
http_request(method: :get, url: url)
|
18
53
|
end
|
19
54
|
|
20
55
|
def delete(url)
|
21
|
-
|
22
|
-
Response.new(body: response.body, headers: response.headers, status: response.code)
|
56
|
+
http_request(method: :delete, url: url)
|
23
57
|
end
|
24
58
|
|
25
59
|
def put(url, data: {})
|
26
|
-
|
27
|
-
response = RestClient.put(url, data, :accept => :json, authorization: @authorization)
|
28
|
-
rescue RestClient::UnprocessableEntity => e
|
29
|
-
raise HttpClient::UnprocessableEntity.new(e.response)
|
30
|
-
end
|
31
|
-
Response.new(body: response.body, headers: response.headers, status: response.code)
|
60
|
+
http_request(method: :put, url: url, data: data)
|
32
61
|
end
|
33
62
|
|
34
63
|
def post(url, data: {})
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
64
|
+
http_request(method: :post, url: url, data: data)
|
65
|
+
end
|
66
|
+
|
67
|
+
private
|
68
|
+
def http_request(method: , url: , data: nil, accept: 'application/json')
|
69
|
+
response = @client.send(method) do |req|
|
70
|
+
req.body = data unless data.nil?
|
71
|
+
req.url url
|
72
|
+
|
73
|
+
if accept
|
74
|
+
req.headers['Accept'] = accept
|
75
|
+
end
|
76
|
+
end
|
77
|
+
Response.new(body: response.body, headers: response.headers, status: response.status)
|
78
|
+
rescue Faraday::Error::ResourceNotFound
|
79
|
+
raise HttpClient::ResourceNotFound
|
80
|
+
rescue Faraday::Error::ClientError => e
|
81
|
+
response = e.response
|
82
|
+
if response[:status] == 422
|
83
|
+
raise HttpClient::UnprocessableEntity.new(response)
|
84
|
+
else
|
85
|
+
raise HttpClient::OtherHttpError.new(response)
|
39
86
|
end
|
40
|
-
Response.new(body: response.body, headers: response.headers, status: response.code)
|
41
87
|
end
|
42
88
|
end
|
43
89
|
end
|
data/lib/restful_resource.rb
CHANGED
@@ -1,19 +1,19 @@
|
|
1
1
|
require 'rack'
|
2
2
|
require 'uri'
|
3
3
|
require 'link_header'
|
4
|
-
require '
|
4
|
+
require 'faraday'
|
5
5
|
require 'active_support'
|
6
6
|
require 'active_support/all'
|
7
|
-
|
7
|
+
require 'resolv-replace.rb'
|
8
|
+
require_relative 'restful_resource/version'
|
8
9
|
require_relative 'restful_resource/null_logger'
|
9
|
-
require_relative
|
10
|
-
require_relative
|
11
|
-
require_relative
|
12
|
-
require_relative
|
10
|
+
require_relative 'restful_resource/paginated_array'
|
11
|
+
require_relative 'restful_resource/parameter_missing_error'
|
12
|
+
require_relative 'restful_resource/resource_id_missing_error'
|
13
|
+
require_relative 'restful_resource/open_object'
|
13
14
|
require_relative 'restful_resource/response'
|
14
|
-
require_relative 'restful_resource/authorization'
|
15
15
|
require_relative 'restful_resource/http_client'
|
16
|
-
require_relative
|
16
|
+
require_relative 'restful_resource/associations'
|
17
17
|
require_relative 'restful_resource/rails_validations'
|
18
|
-
require_relative
|
19
|
-
|
18
|
+
require_relative 'restful_resource/base'
|
19
|
+
|
data/restful_resource.gemspec
CHANGED
@@ -22,7 +22,8 @@ Gem::Specification.new do |spec|
|
|
22
22
|
spec.add_development_dependency "rake"
|
23
23
|
spec.add_development_dependency "rspec", "~> 3.1"
|
24
24
|
|
25
|
-
spec.add_dependency "
|
25
|
+
spec.add_dependency "faraday"
|
26
|
+
spec.add_dependency "faraday-http-cache"
|
26
27
|
spec.add_dependency "link_header"
|
27
28
|
spec.add_dependency "activesupport"
|
28
29
|
spec.add_dependency "rack"
|
@@ -28,22 +28,6 @@ describe RestfulResource::Base, 'authorization' do
|
|
28
28
|
expect(FirstTest.send(:http)).not_to equal(SecondTest.send(:http))
|
29
29
|
end
|
30
30
|
|
31
|
-
it 'should have http auth on SecondClient when initialised first' do
|
32
|
-
SecondTest.send(:http)
|
33
|
-
FirstTest.send(:http)
|
34
|
-
|
35
|
-
authorization = SecondTest.send(:http).instance_variable_get :@authorization
|
36
|
-
expect(authorization).to be_truthy
|
37
|
-
end
|
38
|
-
|
39
|
-
it 'should have http auth on SecondTest when initialised second' do
|
40
|
-
FirstTest.send(:http)
|
41
|
-
SecondTest.send(:http)
|
42
|
-
|
43
|
-
authorization = SecondTest.send(:http).instance_variable_get :@authorization
|
44
|
-
expect(authorization).to be_truthy
|
45
|
-
end
|
46
|
-
|
47
31
|
it 'should have same http auth on superclass' do
|
48
32
|
expect(SecondTest.send(:http)).to equal(SecondClient.send(:http))
|
49
33
|
end
|
@@ -223,34 +223,6 @@ describe RestfulResource::Base do
|
|
223
223
|
end
|
224
224
|
end
|
225
225
|
|
226
|
-
describe 'logger' do
|
227
|
-
before do
|
228
|
-
RestfulResource::Base.configure(base_url: 'http://api.carwow.co.uk/')
|
229
|
-
end
|
230
|
-
|
231
|
-
context 'when no logger is passed' do
|
232
|
-
it 'defaults to NullLogger' do
|
233
|
-
expect(RestClient.log).to be_instance_of(RestfulResource::NullLogger)
|
234
|
-
end
|
235
|
-
end
|
236
|
-
|
237
|
-
context 'when a logger is passed' do
|
238
|
-
before do
|
239
|
-
RestfulResource::Base.configure(base_url: 'http://api.carwow.co.uk/', logger: my_logger)
|
240
|
-
end
|
241
|
-
|
242
|
-
after do
|
243
|
-
RestClient.log = RestfulResource::NullLogger.new
|
244
|
-
end
|
245
|
-
|
246
|
-
let(:my_logger) { double('logger') }
|
247
|
-
|
248
|
-
it 'uses that logger' do
|
249
|
-
expect(RestClient.log).to eq(my_logger)
|
250
|
-
end
|
251
|
-
end
|
252
|
-
end
|
253
|
-
|
254
226
|
def response_with_page_information
|
255
227
|
RestfulResource::Response.new(body: [{ id: 1, name: 'Golf'}, { id: 2, name: 'Polo' }].to_json,
|
256
228
|
headers: { links: '<http://api.carwow.co.uk/makes/Volkswagen/models.json?page=6>;rel="last",<http://api.carwow.co.uk/makes/Volkswagen/models.json?page=2>;rel="next"'})
|
@@ -34,12 +34,18 @@ describe RestfulResource::HttpClient do
|
|
34
34
|
it 'post should raise error 422' do
|
35
35
|
expect { @http_client.post('http://httpbin.org/status/422', data: { name: 'Mad cow' }) }.to raise_error(RestfulResource::HttpClient::UnprocessableEntity)
|
36
36
|
end
|
37
|
+
|
38
|
+
it 'should raise error on 404' do
|
39
|
+
expect { @http_client.get('http://httpbin.org/status/404') }.to raise_error(RestfulResource::HttpClient::ResourceNotFound)
|
40
|
+
expect { @http_client.delete('http://httpbin.org/status/404') }.to raise_error(RestfulResource::HttpClient::ResourceNotFound)
|
41
|
+
expect { @http_client.put('http://httpbin.org/status/404', data: { name: 'Mad cow' }) }.to raise_error(RestfulResource::HttpClient::ResourceNotFound)
|
42
|
+
expect { @http_client.post('http://httpbin.org/status/404', data: { name: 'Mad cow' }) }.to raise_error(RestfulResource::HttpClient::ResourceNotFound)
|
43
|
+
end
|
37
44
|
end
|
38
45
|
|
39
46
|
describe 'Authentication' do
|
40
47
|
before :each do
|
41
|
-
|
42
|
-
@http_client = RestfulResource::HttpClient.new(authorization: auth)
|
48
|
+
@http_client = RestfulResource::HttpClient.new(username: 'user', password: 'passwd')
|
43
49
|
end
|
44
50
|
|
45
51
|
it 'should execute authenticated get' do
|
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: 0.
|
4
|
+
version: 0.9.0
|
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:
|
12
|
+
date: 2016-01-29 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -54,7 +54,21 @@ dependencies:
|
|
54
54
|
- !ruby/object:Gem::Version
|
55
55
|
version: '3.1'
|
56
56
|
- !ruby/object:Gem::Dependency
|
57
|
-
name:
|
57
|
+
name: faraday
|
58
|
+
requirement: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
type: :runtime
|
64
|
+
prerelease: false
|
65
|
+
version_requirements: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
- !ruby/object:Gem::Dependency
|
71
|
+
name: faraday-http-cache
|
58
72
|
requirement: !ruby/object:Gem::Requirement
|
59
73
|
requirements:
|
60
74
|
- - ">="
|
@@ -128,7 +142,6 @@ files:
|
|
128
142
|
- lib/restful_resource/base.rb
|
129
143
|
- lib/restful_resource/http_client.rb
|
130
144
|
- lib/restful_resource/null_logger.rb
|
131
|
-
- lib/restful_resource/old_base.rb
|
132
145
|
- lib/restful_resource/open_object.rb
|
133
146
|
- lib/restful_resource/paginated_array.rb
|
134
147
|
- lib/restful_resource/parameter_missing_error.rb
|
@@ -142,10 +155,8 @@ files:
|
|
142
155
|
- spec/restful_resource/base_authorization_spec.rb
|
143
156
|
- spec/restful_resource/base_spec.rb
|
144
157
|
- spec/restful_resource/http_client_spec.rb
|
145
|
-
- spec/restful_resource/old_base_spec.rb
|
146
158
|
- spec/restful_resource/open_object_spec.rb
|
147
159
|
- spec/restful_resource/rails_validations_spec.rb
|
148
|
-
- spec/restful_resource/rest_client_spec.rb
|
149
160
|
- spec/spec_helper.rb
|
150
161
|
homepage: http://www.github.com/carwow/restful_resource
|
151
162
|
licenses:
|
@@ -167,7 +178,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
167
178
|
version: '0'
|
168
179
|
requirements: []
|
169
180
|
rubyforge_project:
|
170
|
-
rubygems_version: 2.
|
181
|
+
rubygems_version: 2.5.1
|
171
182
|
signing_key:
|
172
183
|
specification_version: 4
|
173
184
|
summary: A simple activerecord inspired rest resource base class implemented using
|
@@ -178,8 +189,6 @@ test_files:
|
|
178
189
|
- spec/restful_resource/base_authorization_spec.rb
|
179
190
|
- spec/restful_resource/base_spec.rb
|
180
191
|
- spec/restful_resource/http_client_spec.rb
|
181
|
-
- spec/restful_resource/old_base_spec.rb
|
182
192
|
- spec/restful_resource/open_object_spec.rb
|
183
193
|
- spec/restful_resource/rails_validations_spec.rb
|
184
|
-
- spec/restful_resource/rest_client_spec.rb
|
185
194
|
- spec/spec_helper.rb
|
@@ -1,172 +0,0 @@
|
|
1
|
-
module RestfulResource
|
2
|
-
class OldBase
|
3
|
-
def self.url=(url)
|
4
|
-
@url = url
|
5
|
-
end
|
6
|
-
|
7
|
-
def self.processed_url_and_params(params={})
|
8
|
-
url = @url
|
9
|
-
other_params = params.clone
|
10
|
-
missing_params = []
|
11
|
-
|
12
|
-
url_params = url.scan(/:([A-Za-z][^\/]*)/).flatten
|
13
|
-
url_params.each do |key|
|
14
|
-
value = other_params.delete(key.to_sym)
|
15
|
-
if value.nil?
|
16
|
-
missing_params << key
|
17
|
-
else
|
18
|
-
url = url.gsub(':'+key.to_s, value.to_s)
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
if missing_params.any?
|
23
|
-
raise ParameterMissingError.new(missing_params)
|
24
|
-
end
|
25
|
-
|
26
|
-
[url, other_params]
|
27
|
-
end
|
28
|
-
|
29
|
-
def self.url(params={})
|
30
|
-
processed_url_and_params(params).first
|
31
|
-
end
|
32
|
-
|
33
|
-
|
34
|
-
def self.has_one(nested_resource_type)
|
35
|
-
klass = nested_resource_type.to_s.camelize.safe_constantize
|
36
|
-
klass = OpenStruct if (klass.nil? || klass.superclass != RestfulResource)
|
37
|
-
|
38
|
-
self.send(:define_method, nested_resource_type) do
|
39
|
-
nested_resource = @inner_object.send(nested_resource_type)
|
40
|
-
return nil if nested_resource.nil?
|
41
|
-
klass.new(@inner_object.send(nested_resource_type))
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
def self.has_many(nested_resource_type)
|
46
|
-
klass = nested_resource_type.to_s.singularize.camelize.safe_constantize
|
47
|
-
klass = OpenStruct if (klass.nil? || (klass.superclass != RestfulResource))
|
48
|
-
|
49
|
-
self.send(:define_method, nested_resource_type) do
|
50
|
-
@inner_object.send(nested_resource_type).map { |obj| klass.new(obj) }
|
51
|
-
end
|
52
|
-
end
|
53
|
-
|
54
|
-
def initialize(attributes = {}, hack_for_activeresource = false)
|
55
|
-
@inner_object = OpenStruct.new(attributes)
|
56
|
-
end
|
57
|
-
|
58
|
-
def method_missing(method)
|
59
|
-
if @inner_object.respond_to?(method)
|
60
|
-
@inner_object.send(method)
|
61
|
-
else
|
62
|
-
super(method)
|
63
|
-
end
|
64
|
-
end
|
65
|
-
|
66
|
-
def respond_to?(method, include_private = false)
|
67
|
-
super || @inner_object.respond_to?(method, include_private)
|
68
|
-
end
|
69
|
-
|
70
|
-
def valid?
|
71
|
-
errors.nil? || errors.count == 0
|
72
|
-
end
|
73
|
-
|
74
|
-
def self.find(id, url_params={})
|
75
|
-
response = RestClient.get("#{url(url_params)}/#{id}", params: {})
|
76
|
-
self.new(ActiveSupport::JSON.decode(response))
|
77
|
-
end
|
78
|
-
|
79
|
-
def self.get_one(url_params={})
|
80
|
-
resource = create_new_resource(url_params)
|
81
|
-
response = resource.get
|
82
|
-
self.new(ActiveSupport::JSON.decode(response))
|
83
|
-
end
|
84
|
-
|
85
|
-
def self.update_attributes(id, attributes)
|
86
|
-
begin
|
87
|
-
result = parse(RestClient.put("#{url}/#{id}", attributes))
|
88
|
-
rescue RestClient::UnprocessableEntity => e
|
89
|
-
errors = parse(e.response)
|
90
|
-
result = attributes.merge(errors: errors)
|
91
|
-
end
|
92
|
-
self.new(result)
|
93
|
-
end
|
94
|
-
|
95
|
-
def self.create(attributes)
|
96
|
-
begin
|
97
|
-
result = parse(RestClient.post("#{url}", attributes))
|
98
|
-
rescue RestClient::UnprocessableEntity => e
|
99
|
-
errors = parse(e.response)
|
100
|
-
result = attributes.merge(errors: errors)
|
101
|
-
end
|
102
|
-
self.new(result)
|
103
|
-
end
|
104
|
-
|
105
|
-
def self.search(params = {})
|
106
|
-
response = RestClient.get("#{url}/search", params: params)
|
107
|
-
paginate_response(response)
|
108
|
-
end
|
109
|
-
|
110
|
-
def self.all(params = {})
|
111
|
-
resource = create_new_resource(params)
|
112
|
-
response = resource.get
|
113
|
-
paginate_response(response)
|
114
|
-
end
|
115
|
-
|
116
|
-
def self.get(postfix_url = "", params = {})
|
117
|
-
response = RestClient.get("#{url}#{postfix_url}", params: params)
|
118
|
-
paginate_response(response)
|
119
|
-
end
|
120
|
-
|
121
|
-
def self.put(postfix_url = "", params = {})
|
122
|
-
response = RestClient.put("#{url}#{postfix_url}", params)
|
123
|
-
end
|
124
|
-
|
125
|
-
def self.post(postfix_url = "", params = {})
|
126
|
-
response = RestClient.post("#{url}#{postfix_url}", params)
|
127
|
-
end
|
128
|
-
|
129
|
-
def self.all_not_paginated
|
130
|
-
page = 1
|
131
|
-
results = []
|
132
|
-
while page
|
133
|
-
page_data = self.all(page: page);
|
134
|
-
results += page_data
|
135
|
-
page = page_data.next_page
|
136
|
-
end
|
137
|
-
|
138
|
-
results
|
139
|
-
end
|
140
|
-
|
141
|
-
def self.parse(json)
|
142
|
-
ActiveSupport::JSON.decode(json)
|
143
|
-
end
|
144
|
-
|
145
|
-
def to_json(*args)
|
146
|
-
@inner_object.send(:table).to_json(*args)
|
147
|
-
end
|
148
|
-
|
149
|
-
def as_json(*)
|
150
|
-
@inner_object.send(:table).as_json
|
151
|
-
end
|
152
|
-
|
153
|
-
private
|
154
|
-
def self.paginate_response(response)
|
155
|
-
links_header = response.headers[:links]
|
156
|
-
links = LinkHeader.parse(links_header)
|
157
|
-
|
158
|
-
prev_url = links.find_link(['rel', 'prev']).try(:href)
|
159
|
-
next_url = links.find_link(['rel', 'next']).try(:href)
|
160
|
-
|
161
|
-
array = ActiveSupport::JSON.decode(response).map { |attributes| self.new(attributes) }
|
162
|
-
PaginatedArray.new(array, previous_page_url: prev_url, next_page_url: next_url, total_count: response.headers['X-Total-Count'])
|
163
|
-
end
|
164
|
-
|
165
|
-
def self.create_new_resource(params={})
|
166
|
-
url, other_params = processed_url_and_params(params)
|
167
|
-
url += "?#{other_params.to_query}" if not other_params.empty?
|
168
|
-
resource = RestClient::Resource.new("#{url}")
|
169
|
-
end
|
170
|
-
|
171
|
-
end
|
172
|
-
end
|
@@ -1,119 +0,0 @@
|
|
1
|
-
require_relative '../spec_helper'
|
2
|
-
|
3
|
-
describe RestfulResource::OldBase do
|
4
|
-
context "#find" do
|
5
|
-
it "should retrieve a resource with a simple url" do
|
6
|
-
response = { id: 15, name: 'Arsenal' }.to_json
|
7
|
-
stub_get('http://api.carwow.co.uk/teams/15', response)
|
8
|
-
|
9
|
-
team = Team.find(15)
|
10
|
-
|
11
|
-
expect(team.id).to eq 15
|
12
|
-
expect(team.name).to eq 'Arsenal'
|
13
|
-
end
|
14
|
-
|
15
|
-
it "should retrieve a nested resource" do
|
16
|
-
response = { id: 1, team_id: 15, name: 'David', santoro: 'Santoro' }.to_json
|
17
|
-
stub_get('http://api.carwow.co.uk/teams/15/players/1', response)
|
18
|
-
|
19
|
-
player = Player.find(1, team_id: 15)
|
20
|
-
|
21
|
-
expect(player.id).to eq 1
|
22
|
-
expect(player.team_id).to eq 15
|
23
|
-
expect(player.name).to eq 'David'
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
context "#url" do
|
28
|
-
it "should return the url set if no extra parameters are necessary" do
|
29
|
-
Player.url = 'http://api.carwow.co.uk/players'
|
30
|
-
|
31
|
-
expect(Player.url).to eq 'http://api.carwow.co.uk/players'
|
32
|
-
end
|
33
|
-
|
34
|
-
it "should return the url with the right parameters replaced" do
|
35
|
-
Player.url = 'http://api.carwow.co.uk/teams/:team_id/players'
|
36
|
-
|
37
|
-
expect(Player.url(team_id: 13)).to eq 'http://api.carwow.co.uk/teams/13/players'
|
38
|
-
end
|
39
|
-
|
40
|
-
it "should raise a parameter required exception if parameter needed and not provided" do
|
41
|
-
Player.url = 'http://api.carwow.co.uk/countries/:country_slug/teams/:team_id/players'
|
42
|
-
|
43
|
-
expected_error_message = 'You must pass values for the following parameters: [country_slug, team_id]'
|
44
|
-
expect { Player.url }.to raise_error(RestfulResource::ParameterMissingError, expected_error_message)
|
45
|
-
end
|
46
|
-
|
47
|
-
it "should not confuse port number as a parameter" do
|
48
|
-
Player.url = 'http://api.carwow.co.uk:7000/teams/:team_id/players'
|
49
|
-
|
50
|
-
expect { Player.url(team_id: 13) }.not_to raise_error
|
51
|
-
end
|
52
|
-
end
|
53
|
-
|
54
|
-
context "#all" do
|
55
|
-
it "should provide a paginated result if response contains rest pagination headers" do
|
56
|
-
response = response_with_page_information()
|
57
|
-
stub_new_resource('http://api.carwow.co.uk/teams', response)
|
58
|
-
|
59
|
-
teams = Team.all
|
60
|
-
|
61
|
-
expect(teams.previous_page).to be_nil
|
62
|
-
expect(teams.next_page).to eq 2
|
63
|
-
expect(teams.first.name).to eq 'Arsenal'
|
64
|
-
expect(teams.last.name).to eq 'Chelsea'
|
65
|
-
end
|
66
|
-
end
|
67
|
-
|
68
|
-
it "should act as an openstruct" do
|
69
|
-
example = Player.new(name: 'David', surname: 'Santoro')
|
70
|
-
|
71
|
-
expect(example.name).to eq 'David'
|
72
|
-
expect(example.surname).to eq 'Santoro'
|
73
|
-
end
|
74
|
-
|
75
|
-
it "should use some params for the url and other for the query string" do
|
76
|
-
stub_new_resource('http://api.carwow.co.uk/teams/15/players?name_like=Ars', response_with_page_information)
|
77
|
-
|
78
|
-
players = Player.all(team_id: 15, name_like: 'Ars')
|
79
|
-
end
|
80
|
-
|
81
|
-
it "should raise an error when accessing a field that doesn't exist" do
|
82
|
-
player = Player.new({name: 'David', surname: 'Santoro'})
|
83
|
-
|
84
|
-
expect { player.age }.to raise_error(NoMethodError)
|
85
|
-
end
|
86
|
-
|
87
|
-
private
|
88
|
-
def stub_new_resource(url, fake_response)
|
89
|
-
resource = instance_double('RestClient::Resource', get: fake_response)
|
90
|
-
allow(RestClient::Resource).to receive(:new).with(url).and_return resource
|
91
|
-
end
|
92
|
-
|
93
|
-
def stub_get(url, fake_response, params = {})
|
94
|
-
expect(RestClient).to receive(:get).
|
95
|
-
with(url, params: params).
|
96
|
-
and_return(fake_response)
|
97
|
-
end
|
98
|
-
|
99
|
-
def response_with_page_information
|
100
|
-
response = [{ id: 1, name: 'Arsenal'}, { id: 2, name: 'Chelsea' }].to_json
|
101
|
-
allow(response).to receive(:headers) {
|
102
|
-
{:links => '<http://api.carwow.co.uk/teams.json?page=6>;rel="last",<http://api.carwow.co.uk/teams.json?page=2>;rel="next"'}
|
103
|
-
}
|
104
|
-
response
|
105
|
-
end
|
106
|
-
end
|
107
|
-
|
108
|
-
class Team < RestfulResource::OldBase
|
109
|
-
self.url = "http://api.carwow.co.uk/teams"
|
110
|
-
end
|
111
|
-
|
112
|
-
class Player < RestfulResource::OldBase
|
113
|
-
has_one :team
|
114
|
-
|
115
|
-
self.url = "http://api.carwow.co.uk/teams/:team_id/players"
|
116
|
-
end
|
117
|
-
|
118
|
-
|
119
|
-
|
@@ -1,15 +0,0 @@
|
|
1
|
-
require_relative '../spec_helper'
|
2
|
-
|
3
|
-
describe RestfulResource::Base do
|
4
|
-
context "#get" do
|
5
|
-
it "should throw an exception with wrong url" do
|
6
|
-
expect{RestClient.get("http://www.example.com/djksafjadl", params: {})}.to raise_error
|
7
|
-
end
|
8
|
-
|
9
|
-
it "should throw an exception with wrong url" do
|
10
|
-
r = RestClient::Resource.new("http://www.example.com/djksafjadl")
|
11
|
-
expect{r.get}.to raise_error
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
end
|