restful_resource 1.2.1 → 1.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d294661eeac9255891b66e6dbebb6f1f5da44921
|
4
|
+
data.tar.gz: 5859ac65ef925dbd3c92b310ab377f8b3247258b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 49e2eb64d13010ced65c8a1d0b7b18d61047cfb7afd9d9afaced84453975a8b8317161cd5ea038413750e777ef7bc93acd916e82b6ed088507f0bb03695c23aa
|
7
|
+
data.tar.gz: 590deacd3e072ec8bb49cbe2e5e5d479d7442e7ad45cc9ba6e108af080434a7ec2dd716badf6d1dc0c3019fcd77540eea4298ac593e3db5d0c3b140d28e6eb09
|
data/Rakefile
CHANGED
@@ -9,12 +9,12 @@ desc "Upload to rubygems"
|
|
9
9
|
task :upload => :build do
|
10
10
|
# Check if tag with v#{ResearchSiteApiClient::VERSION} version exists, if so, return with error
|
11
11
|
|
12
|
-
if tag_exists?(current_tag_name)
|
13
|
-
|
14
|
-
|
15
|
-
end
|
12
|
+
# if tag_exists?(current_tag_name)
|
13
|
+
# puts "Tag exists, did you run rake increase_revision_number after merging with master?"
|
14
|
+
# exit 1
|
15
|
+
# end
|
16
16
|
|
17
|
-
create_tag(current_tag_name)
|
17
|
+
# create_tag(current_tag_name)
|
18
18
|
Rake::Task[:release].invoke
|
19
19
|
end
|
20
20
|
|
@@ -3,9 +3,16 @@ module RestfulResource
|
|
3
3
|
class HttpError < StandardError
|
4
4
|
attr_reader :request, :response
|
5
5
|
|
6
|
-
def initialize(request, response)
|
7
|
-
@response =
|
8
|
-
|
6
|
+
def initialize(request, response = nil)
|
7
|
+
@request, @response = request, assign_response(response)
|
8
|
+
end
|
9
|
+
|
10
|
+
def assign_response(response = nil)
|
11
|
+
if response
|
12
|
+
@response = Response.new body: response[:body], headers: response[:headers], status: response[:status]
|
13
|
+
else
|
14
|
+
@response = Response.new
|
15
|
+
end
|
9
16
|
end
|
10
17
|
end
|
11
18
|
|
@@ -31,21 +38,40 @@ module RestfulResource
|
|
31
38
|
end
|
32
39
|
|
33
40
|
class ClientError < HttpError
|
34
|
-
def initalize(request)
|
35
|
-
super(request, {})
|
36
|
-
end
|
37
|
-
|
38
41
|
def message
|
39
42
|
"There was some client error"
|
40
43
|
end
|
41
44
|
end
|
42
45
|
|
43
|
-
def initialize(username: nil, password: nil, logger: nil, cache_store: nil)
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
46
|
+
def initialize(username: nil, password: nil, logger: nil, cache_store: nil, connection: nil)
|
47
|
+
# Use a provided faraday client or initalize a new one
|
48
|
+
@connection = connection || initialize_connection(logger: logger, cache_store: cache_store)
|
49
|
+
|
50
|
+
if username && password
|
51
|
+
@connection.basic_auth username, password
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def get(url, accept: 'application/json')
|
56
|
+
http_request(Request.new(:get, url, accept: accept))
|
57
|
+
end
|
58
|
+
|
59
|
+
def delete(url, accept: 'application/json')
|
60
|
+
http_request(Request.new(:delete, url, accept: accept))
|
61
|
+
end
|
62
|
+
|
63
|
+
def put(url, data: {}, accept: 'application/json')
|
64
|
+
http_request(Request.new(:put, url, body: data, accept: accept))
|
65
|
+
end
|
66
|
+
|
67
|
+
def post(url, data: {}, accept: 'application/json')
|
68
|
+
http_request(Request.new(:post, url, body: data, accept: accept))
|
69
|
+
end
|
48
70
|
|
71
|
+
private
|
72
|
+
|
73
|
+
def initialize_connection(logger: nil, cache_store: nil)
|
74
|
+
@connection = Faraday.new do |b|
|
49
75
|
b.request :url_encoded
|
50
76
|
b.response :raise_error
|
51
77
|
|
@@ -69,25 +95,8 @@ module RestfulResource
|
|
69
95
|
end
|
70
96
|
end
|
71
97
|
|
72
|
-
def get(url, accept: 'application/json')
|
73
|
-
http_request(Request.new(:get, url, accept: accept))
|
74
|
-
end
|
75
|
-
|
76
|
-
def delete(url, accept: 'application/json')
|
77
|
-
http_request(Request.new(:delete, url, accept: accept))
|
78
|
-
end
|
79
|
-
|
80
|
-
def put(url, data: {}, accept: 'application/json')
|
81
|
-
http_request(Request.new(:put, url, body: data, accept: accept))
|
82
|
-
end
|
83
|
-
|
84
|
-
def post(url, data: {}, accept: 'application/json')
|
85
|
-
http_request(Request.new(:post, url, body: data, accept: accept))
|
86
|
-
end
|
87
|
-
|
88
|
-
private
|
89
98
|
def http_request(request)
|
90
|
-
response = @
|
99
|
+
response = @connection.send(request.method) do |req|
|
91
100
|
req.body = request.body unless request.body.nil?
|
92
101
|
req.url request.url
|
93
102
|
|
@@ -0,0 +1,93 @@
|
|
1
|
+
require_relative '../spec_helper'
|
2
|
+
|
3
|
+
describe RestfulResource::HttpClient do
|
4
|
+
def find_middleware(adapter, name)
|
5
|
+
adapter.builder.handlers.find {|m| m.name == name }
|
6
|
+
|
7
|
+
rescue
|
8
|
+
raise "Could not find Faraday middleware: #{name}"
|
9
|
+
end
|
10
|
+
|
11
|
+
def find_middleware_args(adapter, name)
|
12
|
+
find_middleware(adapter, name).instance_variable_get("@args").first
|
13
|
+
|
14
|
+
rescue
|
15
|
+
raise "Could not find args for Faraday middleware: #{name}"
|
16
|
+
end
|
17
|
+
|
18
|
+
describe 'Configuration' do
|
19
|
+
let(:connection) { described_class.new.instance_variable_get("@connection") }
|
20
|
+
let(:middleware) { connection.builder.handlers }
|
21
|
+
|
22
|
+
describe 'Builder configuration' do
|
23
|
+
it 'uses the excon adapter' do
|
24
|
+
expect(middleware).to include Faraday::Adapter::Excon
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'url_encodes requests' do
|
28
|
+
expect(middleware).to include Faraday::Request::UrlEncoded
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'raises on any error responses' do
|
32
|
+
expect(middleware).to include Faraday::Response::RaiseError
|
33
|
+
end
|
34
|
+
|
35
|
+
it 'uses utf-8 encoding' do
|
36
|
+
expect(middleware).to include Faraday::Encoding
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'compresses requests' do
|
40
|
+
expect(middleware).to include FaradayMiddleware::Gzip
|
41
|
+
end
|
42
|
+
|
43
|
+
describe 'when provided a logger' do
|
44
|
+
let(:connection) { described_class.new(logger: logger).instance_variable_get("@connection") }
|
45
|
+
let(:logger) { Logger.new('/dev/null') }
|
46
|
+
|
47
|
+
it 'uses the logger middleware' do
|
48
|
+
expect(middleware).to include Faraday::Response::Logger
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'uses that logger' do
|
52
|
+
expect(find_middleware_args(connection, 'Faraday::Response::Logger')).to eq logger
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
describe 'when provided a cache store' do
|
57
|
+
let(:connection) { described_class.new(cache_store: 'redis').instance_variable_get("@connection") }
|
58
|
+
|
59
|
+
it 'uses the cache_store middleware' do
|
60
|
+
expect(middleware).to include Faraday::HttpCache
|
61
|
+
end
|
62
|
+
|
63
|
+
it 'uses that cache_store' do
|
64
|
+
expect(find_middleware_args(connection, 'Faraday::HttpCache')).to include(store: 'redis')
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
describe 'Excon Adapter configuration' do
|
70
|
+
let(:config) { find_middleware_args connection, 'Faraday::Adapter::Excon' }
|
71
|
+
|
72
|
+
it 'uses nonblock' do
|
73
|
+
expect(config[:nonblock]).to eq true
|
74
|
+
end
|
75
|
+
|
76
|
+
it 'uses a persistent connection' do
|
77
|
+
expect(config[:persistent]).to eq true
|
78
|
+
end
|
79
|
+
|
80
|
+
it 'has a connect_timeout of 2' do
|
81
|
+
expect(config[:connect_timeout]).to eq 2
|
82
|
+
end
|
83
|
+
|
84
|
+
it 'has a read_timeout of 10' do
|
85
|
+
expect(config[:read_timeout]).to eq 10
|
86
|
+
end
|
87
|
+
|
88
|
+
it 'has a write_timeout of 2' do
|
89
|
+
expect(config[:write_timeout]).to eq 2
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
@@ -1,67 +1,145 @@
|
|
1
1
|
require_relative '../spec_helper'
|
2
2
|
|
3
3
|
describe RestfulResource::HttpClient do
|
4
|
-
|
5
|
-
|
6
|
-
|
4
|
+
def faraday_connection
|
5
|
+
Faraday.new do |builder|
|
6
|
+
builder.request :url_encoded
|
7
|
+
builder.response :raise_error
|
8
|
+
builder.adapter :test do |stubs|
|
9
|
+
yield stubs if block_given?
|
10
|
+
end
|
7
11
|
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def http_client(connection)
|
15
|
+
described_class.new(connection: connection)
|
16
|
+
end
|
8
17
|
|
18
|
+
describe 'Basic HTTP' do
|
9
19
|
it 'should execute get' do
|
10
|
-
|
20
|
+
connection = faraday_connection do |stubs|
|
21
|
+
stubs.get('http://httpbin.org/get') { |env| [200, {}, nil] }
|
22
|
+
end
|
23
|
+
|
24
|
+
response = http_client(connection).get('http://httpbin.org/get')
|
11
25
|
expect(response.status).to eq 200
|
12
26
|
end
|
13
27
|
|
14
28
|
it 'should execute put' do
|
15
|
-
|
29
|
+
connection = faraday_connection do |stubs|
|
30
|
+
# Note: request body is serialized as url-encoded so the stub body must be in the same format to match
|
31
|
+
stubs.put('http://httpbin.org/put', 'name=Alfred') { |env| [200, {}, nil] }
|
32
|
+
end
|
33
|
+
|
34
|
+
response = http_client(connection).put('http://httpbin.org/put', data: { name: 'Alfred' })
|
16
35
|
expect(response.status).to eq 200
|
17
36
|
end
|
18
37
|
|
19
38
|
it 'should execute post' do
|
20
|
-
|
39
|
+
connection = faraday_connection do |stubs|
|
40
|
+
# Note: request body is serialized as url-encoded so the stub body must be in the same format to match
|
41
|
+
stubs.post('http://httpbin.org/post', 'name=Alfred') { |env| [200, {}, %{"name": "Alfred"}] }
|
42
|
+
end
|
43
|
+
|
44
|
+
response = http_client(connection).post('http://httpbin.org/post', data: { name: 'Alfred' })
|
45
|
+
|
21
46
|
expect(response.body).to include "name\": \"Alfred"
|
22
47
|
expect(response.status).to eq 200
|
23
48
|
end
|
24
49
|
|
25
50
|
it 'should execute delete' do
|
26
|
-
|
51
|
+
connection = faraday_connection do |stubs|
|
52
|
+
stubs.delete('http://httpbin.org/delete') { |env| [200, {}, nil] }
|
53
|
+
end
|
54
|
+
|
55
|
+
response = http_client(connection).delete('http://httpbin.org/delete')
|
56
|
+
|
27
57
|
expect(response.status).to eq 200
|
28
58
|
end
|
29
59
|
|
30
60
|
it 'put should raise error 422' do
|
31
|
-
|
61
|
+
connection = faraday_connection do |stubs|
|
62
|
+
stubs.put('http://httpbin.org/status/422') { |env| [422, {}, nil] }
|
63
|
+
end
|
64
|
+
|
65
|
+
expect { http_client(connection).put('http://httpbin.org/status/422') }.to raise_error(RestfulResource::HttpClient::UnprocessableEntity)
|
32
66
|
end
|
33
67
|
|
34
68
|
it 'post should raise error 422' do
|
35
|
-
|
69
|
+
connection = faraday_connection do |stubs|
|
70
|
+
stubs.post('http://httpbin.org/status/422') { |env| [422, {}, nil] }
|
71
|
+
end
|
72
|
+
|
73
|
+
expect { http_client(connection).post('http://httpbin.org/status/422') }.to raise_error(RestfulResource::HttpClient::UnprocessableEntity)
|
36
74
|
end
|
37
75
|
|
38
76
|
it 'put should raise error 503' do
|
39
|
-
|
77
|
+
connection = faraday_connection do |stubs|
|
78
|
+
stubs.put('http://httpbin.org/status/503') { |env| [503, {}, nil] }
|
79
|
+
end
|
80
|
+
|
81
|
+
expect { http_client(connection).put('http://httpbin.org/status/503') }.to raise_error(RestfulResource::HttpClient::ServiceUnavailable)
|
40
82
|
end
|
41
83
|
|
42
84
|
it 'post should raise error 503' do
|
43
|
-
|
85
|
+
connection = faraday_connection do |stubs|
|
86
|
+
stubs.post('http://httpbin.org/status/503') { |env| [503, {}, nil] }
|
87
|
+
end
|
88
|
+
|
89
|
+
expect { http_client(connection).post('http://httpbin.org/status/503') }.to raise_error(RestfulResource::HttpClient::ServiceUnavailable)
|
44
90
|
end
|
45
91
|
|
46
92
|
it 'should raise error on 404' do
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
93
|
+
connection = faraday_connection do |stubs|
|
94
|
+
stubs.get('http://httpbin.org/status/404') { |env| [404, {}, nil] }
|
95
|
+
stubs.post('http://httpbin.org/status/404') { |env| [404, {}, nil] }
|
96
|
+
stubs.put('http://httpbin.org/status/404') { |env| [404, {}, nil] }
|
97
|
+
stubs.delete('http://httpbin.org/status/404') { |env| [404, {}, nil] }
|
98
|
+
end
|
99
|
+
|
100
|
+
expect { http_client(connection).get('http://httpbin.org/status/404') }.to raise_error(RestfulResource::HttpClient::ResourceNotFound)
|
101
|
+
expect { http_client(connection).delete('http://httpbin.org/status/404') }.to raise_error(RestfulResource::HttpClient::ResourceNotFound)
|
102
|
+
expect { http_client(connection).put('http://httpbin.org/status/404', data: { name: 'Mad cow' }) }.to raise_error(RestfulResource::HttpClient::ResourceNotFound)
|
103
|
+
expect { http_client(connection).post('http://httpbin.org/status/404', data: { name: 'Mad cow' }) }.to raise_error(RestfulResource::HttpClient::ResourceNotFound)
|
104
|
+
end
|
105
|
+
|
106
|
+
it 'should raise Faraday::ConnectionFailed errors' do
|
107
|
+
connection = faraday_connection do |stubs|
|
108
|
+
stubs.get('https://localhost:3005') {|env| raise Faraday::ConnectionFailed.new(nil) }
|
109
|
+
end
|
110
|
+
|
111
|
+
expect { http_client(connection).get('https://localhost:3005') }.to raise_error(Faraday::ConnectionFailed)
|
51
112
|
end
|
52
113
|
|
53
|
-
it '
|
54
|
-
|
114
|
+
it 'raises ClientError when a client errors with no response' do
|
115
|
+
connection = faraday_connection do |stubs|
|
116
|
+
stubs.get('https://localhost:3005') {|env| raise Faraday::ClientError.new(nil) }
|
117
|
+
end
|
118
|
+
|
119
|
+
expect { http_client(connection).get('https://localhost:3005') }.to raise_error(RestfulResource::HttpClient::ClientError)
|
120
|
+
end
|
121
|
+
|
122
|
+
it 'raises OtherHttpError for other status response codes' do
|
123
|
+
connection = faraday_connection do |stubs|
|
124
|
+
stubs.get('http://httpbin.org/status/418') { |env| [418, {}, nil] }
|
125
|
+
end
|
126
|
+
|
127
|
+
expect { http_client(connection).get('http://httpbin.org/status/418') }.to raise_error(RestfulResource::HttpClient::OtherHttpError)
|
55
128
|
end
|
56
129
|
end
|
57
130
|
|
58
131
|
describe 'Authentication' do
|
59
|
-
|
60
|
-
|
132
|
+
def http_client(connection)
|
133
|
+
described_class.new(connection: connection, username: 'user', password: 'passwd')
|
61
134
|
end
|
62
135
|
|
63
136
|
it 'should execute authenticated get' do
|
64
|
-
|
137
|
+
connection = faraday_connection do |stubs|
|
138
|
+
stubs.get('http://httpbin.org/basic-auth/user/passwd', { "Authorization"=>"Basic dXNlcjpwYXNzd2Q=" }) { |env| [200, {}, nil] }
|
139
|
+
end
|
140
|
+
|
141
|
+
response = http_client(connection).get('http://httpbin.org/basic-auth/user/passwd')
|
142
|
+
|
65
143
|
expect(response.status).to eq 200
|
66
144
|
end
|
67
145
|
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: 1.2.
|
4
|
+
version: 1.2.2
|
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: 2016-10-
|
12
|
+
date: 2016-10-19 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -199,6 +199,7 @@ files:
|
|
199
199
|
- spec/restful_resource/associations_spec.rb
|
200
200
|
- spec/restful_resource/base_authorization_spec.rb
|
201
201
|
- spec/restful_resource/base_spec.rb
|
202
|
+
- spec/restful_resource/http_client_configuration_spec.rb
|
202
203
|
- spec/restful_resource/http_client_spec.rb
|
203
204
|
- spec/restful_resource/open_object_spec.rb
|
204
205
|
- spec/restful_resource/rails_validations_spec.rb
|
@@ -234,6 +235,7 @@ test_files:
|
|
234
235
|
- spec/restful_resource/associations_spec.rb
|
235
236
|
- spec/restful_resource/base_authorization_spec.rb
|
236
237
|
- spec/restful_resource/base_spec.rb
|
238
|
+
- spec/restful_resource/http_client_configuration_spec.rb
|
237
239
|
- spec/restful_resource/http_client_spec.rb
|
238
240
|
- spec/restful_resource/open_object_spec.rb
|
239
241
|
- spec/restful_resource/rails_validations_spec.rb
|