hyperclient 0.6.1 → 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -2
- data/Gemfile +1 -1
- data/README.md +25 -4
- data/Rakefile +0 -0
- data/UPGRADING.md +13 -0
- data/features/steps/default_config.rb +3 -1
- data/features/support/api.rb +3 -3
- data/hyperclient.gemspec +1 -0
- data/lib/hyperclient/entry_point.rb +30 -13
- data/lib/hyperclient/version.rb +1 -1
- data/test/hyperclient/entry_point_test.rb +101 -53
- data/test/hyperclient/link_test.rb +70 -16
- data/test/hyperclient_test.rb +4 -4
- data/test/test_helper.rb +9 -1
- metadata +17 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0fef7b6957e239321aff88f157c57b669cdaa388
|
4
|
+
data.tar.gz: 5e1fc8350dbd97205743d716146a4b148202034e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8f005fb328d44e78d11d443184361a8709422977c10a574ef98d8a21694acb72930058ca05075d06baa5290be17502060f5efe09b9d604264f2d9f8a26ec9171
|
7
|
+
data.tar.gz: 7c587f7c4abfc82e571b3dc8a3b571521d13c077a84d12aca7497777b894630b648adf87e296e2f3f2d4806c3ed31a91152d8ced262f0dc9305a8f6814485da0
|
data/CHANGELOG.md
CHANGED
@@ -1,6 +1,9 @@
|
|
1
|
-
###
|
1
|
+
### 0.7.0 (February 23, 2015)
|
2
2
|
|
3
|
-
|
3
|
+
This version introduces several backwards incompatible changes. See [UPGRADING](UPGRADING.md) for details.
|
4
|
+
|
5
|
+
* [#80](https://github.com/codegram/hyperclient/pull/80): Faraday options can be passed to the connection on initialization - [@koenpunt](https://github.com/koenpunt).
|
6
|
+
* [#81](https://github.com/codegram/hyperclient/pull/81): The default Content-Type is now `application/hal+json` - [@koenpunt](https://github.com/koenpunt).
|
4
7
|
|
5
8
|
### 0.6.1 (October 17, 2014)
|
6
9
|
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,8 +1,10 @@
|
|
1
1
|
# Hyperclient
|
2
2
|
|
3
|
-
[![
|
4
|
-
[![
|
5
|
-
[![
|
3
|
+
[![Gem Version](http://img.shields.io/gem/v/hyperclient.svg)](http://badge.fury.io/rb/hyperclient)
|
4
|
+
[![Build Status](http://img.shields.io/travis/codegram/hyperclient.svg)](https://travis-ci.org/codegram/hyperclient)
|
5
|
+
[![Dependency Status](https://gemnasium.com/codegram/hyperclient.svg)](https://gemnasium.com/codegram/hyperclient)
|
6
|
+
[![Code Climate](https://codeclimate.com/github/codegram/hyperclient.svg)](https://codeclimate.com/github/codegram/hyperclient)
|
7
|
+
[![Coverage Status](https://img.shields.io/coveralls/codegram/hyperclient.svg)](https://coveralls.io/r/codegram/hyperclient?branch=master)
|
6
8
|
|
7
9
|
Hyperclient is a Hypermedia API client written in Ruby. It fully supports [JSON HAL](http://stateless.co/hal_specification.html).
|
8
10
|
|
@@ -20,7 +22,7 @@ require 'hyperclient'
|
|
20
22
|
api = Hyperclient.new('https://grape-with-roar.herokuapp.com/api')
|
21
23
|
```
|
22
24
|
|
23
|
-
By default, Hyperclient adds `application/json` as `Content-Type` and `Accept` headers. It will also send requests as JSON and parse JSON responses. Specify additional headers or authentication if necessary.
|
25
|
+
By default, Hyperclient adds `application/hal+json` as `Content-Type` and `Accept` headers. It will also send requests as JSON and parse JSON responses. Specify additional headers or authentication if necessary.
|
24
26
|
|
25
27
|
```ruby
|
26
28
|
api = Hyperclient.new('https://grape-with-roar.herokuapp.com/api') do |client|
|
@@ -38,6 +40,24 @@ api = Hyperclient.new('https://grape-with-roar.herokuapp.com/api') do |client|
|
|
38
40
|
end
|
39
41
|
```
|
40
42
|
|
43
|
+
You can pass options to the Faraday connection block in the `connection` block:
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
api = Hyperclient.new('https://grape-with-roar.herokuapp.com/api') do |client|
|
47
|
+
client.connection(ssl: { verify: false }) do |conn|
|
48
|
+
conn.use Faraday::Request::OAuth
|
49
|
+
end
|
50
|
+
end
|
51
|
+
```
|
52
|
+
|
53
|
+
Or when using the default connection configuration you can use `faraday_options`:
|
54
|
+
|
55
|
+
```ruby
|
56
|
+
api = Hyperclient.new('https://grape-with-roar.herokuapp.com/api') do |client|
|
57
|
+
client.faraday_options = { ssl: { verify: false } }
|
58
|
+
end
|
59
|
+
```
|
60
|
+
|
41
61
|
You can build a new Faraday connection block without inheriting default middleware by specifying `default: false` in the `connection` block.
|
42
62
|
|
43
63
|
```ruby
|
@@ -170,6 +190,7 @@ describe Acme::Api do
|
|
170
190
|
let(:client) do
|
171
191
|
Hyperclient.new('http://example.org/api') do |client|
|
172
192
|
client.connection(default: false) do |conn|
|
193
|
+
conn.request :json
|
173
194
|
conn.response :json
|
174
195
|
conn.use Faraday::Adapter::Rack, app
|
175
196
|
end
|
data/Rakefile
CHANGED
File without changes
|
data/UPGRADING.md
CHANGED
@@ -1,6 +1,19 @@
|
|
1
1
|
Upgrading Hyperclient
|
2
2
|
=====================
|
3
3
|
|
4
|
+
### Upgrading to >= 0.7.0
|
5
|
+
|
6
|
+
#### Changes in default headers
|
7
|
+
|
8
|
+
The default `Content-Type` is now `application/hal+json` and the `Accept` header includes `application/hal+json` as well.
|
9
|
+
If your API only accepts `application/json` as Content-Type, you can override the default headers in the client initialization block as such:
|
10
|
+
|
11
|
+
```rb
|
12
|
+
Hyperclient.new('https://api.example.org/') do |client|
|
13
|
+
client.headers = { 'Content-Type' => 'application/json', 'Accept' => 'application/json,application/hal+json' }
|
14
|
+
end
|
15
|
+
```
|
16
|
+
|
4
17
|
### Upgrading to >= 0.6.0
|
5
18
|
|
6
19
|
#### Changes in HTTP Error Handling
|
@@ -6,7 +6,9 @@ class Spinach::Features::DefaultConfig < Spinach::FeatureSteps
|
|
6
6
|
end
|
7
7
|
|
8
8
|
step 'the request should have been sent with the correct JSON headers' do
|
9
|
-
assert_requested :get, 'api.example.org', headers: {
|
9
|
+
assert_requested :get, 'api.example.org', headers: {
|
10
|
+
'Content-Type' => 'application/hal+json', 'Accept' => 'application/hal+json,application/json'
|
11
|
+
}
|
10
12
|
end
|
11
13
|
|
12
14
|
step 'I send some data to the API' do
|
data/features/support/api.rb
CHANGED
@@ -5,9 +5,9 @@ module API
|
|
5
5
|
include Spinach::Fixtures
|
6
6
|
|
7
7
|
before do
|
8
|
-
stub_request(:any, %r{api.example.org*}).to_return(body: root_response, headers: { 'Content-Type' => 'application/json' })
|
9
|
-
stub_request(:get, 'api.example.org/posts').to_return(body: posts_response, headers: { 'Content-Type' => 'application/json' })
|
10
|
-
stub_request(:get, 'api.example.org/posts/1').to_return(body: post_response, headers: { 'Content-Type' => 'application/json' })
|
8
|
+
stub_request(:any, %r{api.example.org*}).to_return(body: root_response, headers: { 'Content-Type' => 'application/hal+json' })
|
9
|
+
stub_request(:get, 'api.example.org/posts').to_return(body: posts_response, headers: { 'Content-Type' => 'application/hal+json' })
|
10
|
+
stub_request(:get, 'api.example.org/posts/1').to_return(body: post_response, headers: { 'Content-Type' => 'application/hal+json' })
|
11
11
|
end
|
12
12
|
|
13
13
|
def api
|
data/hyperclient.gemspec
CHANGED
@@ -17,6 +17,7 @@ Gem::Specification.new do |gem|
|
|
17
17
|
gem.add_dependency 'faraday', '~> 0.8'
|
18
18
|
gem.add_dependency 'futuroscope', '>= 0.0.10'
|
19
19
|
gem.add_dependency 'faraday_middleware', '~> 0.9'
|
20
|
+
gem.add_dependency 'faraday_hal_middleware', '~> 0.0.1'
|
20
21
|
gem.add_dependency 'uri_template', '~> 0.5'
|
21
22
|
gem.add_dependency 'net-http-digest_auth', '~> 1.2'
|
22
23
|
gem.add_dependency 'faraday-digestauth', '~> 0.2'
|
@@ -1,5 +1,6 @@
|
|
1
1
|
require 'hyperclient/link'
|
2
2
|
require 'faraday_middleware'
|
3
|
+
require 'faraday_hal_middleware'
|
3
4
|
require_relative '../faraday/connection'
|
4
5
|
|
5
6
|
module Hyperclient
|
@@ -20,7 +21,7 @@ module Hyperclient
|
|
20
21
|
# client = Hyperclient::EntryPoint.new('http://my.api.org')
|
21
22
|
#
|
22
23
|
# client = Hyperclient::EntryPoint.new('http://my.api.org') do |entry_point|
|
23
|
-
# entry_point.connection
|
24
|
+
# entry_point.connection do |conn|
|
24
25
|
# conn.use Faraday::Request::OAuth
|
25
26
|
# end
|
26
27
|
# entry_point.headers['Access-Token'] = 'token'
|
@@ -47,22 +48,31 @@ module Hyperclient
|
|
47
48
|
# default - Set to true to reuse default Faraday connection options.
|
48
49
|
#
|
49
50
|
# Returns a Faraday::Connection.
|
50
|
-
def connection(options = {
|
51
|
+
def connection(options = {}, &block)
|
52
|
+
@faraday_options ||= options.dup
|
51
53
|
if block_given?
|
52
54
|
fail ConnectionAlreadyInitializedError if @connection
|
53
|
-
if
|
55
|
+
if @faraday_options.delete(:default) == false
|
56
|
+
@faraday_block = block
|
57
|
+
else
|
54
58
|
@faraday_block = lambda do |conn|
|
55
59
|
default_faraday_block.call conn
|
56
60
|
block.call conn
|
57
61
|
end
|
58
|
-
else
|
59
|
-
@faraday_block = block
|
60
62
|
end
|
61
63
|
else
|
62
|
-
@connection ||= Faraday.new(_url,
|
64
|
+
@connection ||= Faraday.new(_url, faraday_options, &faraday_block)
|
63
65
|
end
|
64
66
|
end
|
65
67
|
|
68
|
+
# Public: Headers included with every API request.
|
69
|
+
#
|
70
|
+
# Returns a Hash.
|
71
|
+
def headers
|
72
|
+
return @connection.headers if @connection
|
73
|
+
@headers ||= default_headers
|
74
|
+
end
|
75
|
+
|
66
76
|
# Public: Set headers.
|
67
77
|
#
|
68
78
|
# value - A Hash containing headers to include with every API request.
|
@@ -71,12 +81,19 @@ module Hyperclient
|
|
71
81
|
@headers = value
|
72
82
|
end
|
73
83
|
|
74
|
-
# Public:
|
84
|
+
# Public: Options passed to Faraday
|
75
85
|
#
|
76
86
|
# Returns a Hash.
|
77
|
-
def
|
78
|
-
|
79
|
-
|
87
|
+
def faraday_options
|
88
|
+
(@faraday_options ||= {}).merge(headers: headers)
|
89
|
+
end
|
90
|
+
|
91
|
+
# Public: Set Faraday connection options.
|
92
|
+
#
|
93
|
+
# value - A Hash containing options to pass to Faraday
|
94
|
+
def faraday_options=(value)
|
95
|
+
fail ConnectionAlreadyInitializedError if @connection
|
96
|
+
@faraday_options = value
|
80
97
|
end
|
81
98
|
|
82
99
|
# Public: Faraday block used with every API request.
|
@@ -109,8 +126,8 @@ module Hyperclient
|
|
109
126
|
lambda do |conn|
|
110
127
|
conn.use Faraday::Response::RaiseError
|
111
128
|
conn.use FaradayMiddleware::FollowRedirects
|
112
|
-
conn.request :
|
113
|
-
conn.response :
|
129
|
+
conn.request :hal_json
|
130
|
+
conn.response :hal_json, content_type: /\bjson$/
|
114
131
|
conn.adapter :net_http
|
115
132
|
end
|
116
133
|
end
|
@@ -120,7 +137,7 @@ module Hyperclient
|
|
120
137
|
#
|
121
138
|
# Returns a Hash.
|
122
139
|
def default_headers
|
123
|
-
{ 'Content-Type' => 'application/json', 'Accept' => 'application/json' }
|
140
|
+
{ 'Content-Type' => 'application/hal+json', 'Accept' => 'application/hal+json,application/json' }
|
124
141
|
end
|
125
142
|
end
|
126
143
|
end
|
data/lib/hyperclient/version.rb
CHANGED
@@ -14,8 +14,8 @@ module Hyperclient
|
|
14
14
|
end
|
15
15
|
|
16
16
|
it 'creates a Faraday connection with the default headers' do
|
17
|
-
entry_point.headers['Content-Type'].must_equal 'application/json'
|
18
|
-
entry_point.headers['Accept'].must_equal 'application/json'
|
17
|
+
entry_point.headers['Content-Type'].must_equal 'application/hal+json'
|
18
|
+
entry_point.headers['Accept'].must_equal 'application/hal+json,application/json'
|
19
19
|
end
|
20
20
|
|
21
21
|
it 'can update headers after a connection has been constructed' do
|
@@ -35,8 +35,8 @@ module Hyperclient
|
|
35
35
|
handlers = entry_point.connection.builder.handlers
|
36
36
|
handlers.must_include Faraday::Response::RaiseError
|
37
37
|
handlers.must_include FaradayMiddleware::FollowRedirects
|
38
|
-
handlers.must_include FaradayMiddleware::
|
39
|
-
handlers.must_include FaradayMiddleware::
|
38
|
+
handlers.must_include FaradayMiddleware::EncodeHalJson
|
39
|
+
handlers.must_include FaradayMiddleware::ParseHalJson
|
40
40
|
handlers.must_include Faraday::Adapter::NetHttp
|
41
41
|
end
|
42
42
|
|
@@ -57,74 +57,122 @@ module Hyperclient
|
|
57
57
|
end
|
58
58
|
end
|
59
59
|
end
|
60
|
-
end
|
61
60
|
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
conn.request :json
|
67
|
-
conn.response :json, content_type: /\bjson$/
|
68
|
-
conn.adapter :net_http
|
61
|
+
describe 'faraday_options' do
|
62
|
+
let(:entry_point) do
|
63
|
+
EntryPoint.new 'http://my.api.org' do |entry_point|
|
64
|
+
entry_point.faraday_options = { proxy: 'http://my.proxy:8080' }
|
69
65
|
end
|
66
|
+
end
|
70
67
|
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
68
|
+
describe 'connection' do
|
69
|
+
it 'creates a Faraday connection with the entry point url' do
|
70
|
+
entry_point.connection.url_prefix.to_s.must_equal 'http://my.api.org/'
|
71
|
+
end
|
72
|
+
|
73
|
+
it 'creates a Faraday connection with the default headers' do
|
74
|
+
entry_point.headers['Content-Type'].must_equal 'application/hal+json'
|
75
|
+
entry_point.headers['Accept'].must_equal 'application/hal+json,application/json'
|
76
|
+
end
|
77
|
+
|
78
|
+
it 'creates a Faraday connection with options' do
|
79
|
+
entry_point.connection.proxy.must_be_kind_of Faraday::ProxyOptions
|
80
|
+
entry_point.connection.proxy.uri.to_s.must_equal 'http://my.proxy:8080'
|
81
|
+
end
|
75
82
|
end
|
76
83
|
end
|
77
84
|
|
78
|
-
describe '
|
79
|
-
|
80
|
-
|
85
|
+
describe 'options' do
|
86
|
+
let(:entry_point) do
|
87
|
+
EntryPoint.new 'http://my.api.org' do |entry_point|
|
88
|
+
entry_point.connection(proxy: 'http://my.proxy:8080')
|
89
|
+
end
|
81
90
|
end
|
82
91
|
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
92
|
+
describe 'connection' do
|
93
|
+
it 'creates a Faraday connection with the entry point url' do
|
94
|
+
entry_point.connection.url_prefix.to_s.must_equal 'http://my.api.org/'
|
95
|
+
end
|
96
|
+
|
97
|
+
it 'creates a Faraday connection with the default headers' do
|
98
|
+
entry_point.headers['Content-Type'].must_equal 'application/hal+json'
|
99
|
+
entry_point.headers['Accept'].must_equal 'application/hal+json,application/json'
|
100
|
+
end
|
87
101
|
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
handlers.must_include FaradayMiddleware::EncodeJson
|
93
|
-
handlers.must_include FaradayMiddleware::ParseJson
|
94
|
-
handlers.must_include Faraday::Adapter::NetHttp
|
102
|
+
it 'creates a Faraday connection with options' do
|
103
|
+
entry_point.connection.proxy.must_be_kind_of Faraday::ProxyOptions
|
104
|
+
entry_point.connection.proxy.uri.to_s.must_equal 'http://my.proxy:8080'
|
105
|
+
end
|
95
106
|
end
|
96
107
|
end
|
97
|
-
end
|
98
108
|
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
109
|
+
describe 'custom' do
|
110
|
+
let(:entry_point) do
|
111
|
+
EntryPoint.new 'http://my.api.org' do |entry_point|
|
112
|
+
entry_point.connection(default: false) do |conn|
|
113
|
+
conn.request :json
|
114
|
+
conn.response :json, content_type: /\bjson$/
|
115
|
+
conn.adapter :net_http
|
116
|
+
end
|
117
|
+
|
118
|
+
entry_point.headers = {
|
119
|
+
'Content-Type' => 'application/foobar',
|
120
|
+
'Accept' => 'application/foobar'
|
121
|
+
}
|
104
122
|
end
|
105
|
-
entry_point.headers['Access-Token'] = 'token'
|
106
123
|
end
|
107
|
-
end
|
108
124
|
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
125
|
+
describe 'connection' do
|
126
|
+
it 'creates a Faraday connection with the entry point url' do
|
127
|
+
entry_point.connection.url_prefix.to_s.must_equal 'http://my.api.org/'
|
128
|
+
end
|
129
|
+
|
130
|
+
it 'creates a Faraday connection with non-default headers' do
|
131
|
+
entry_point.headers['Content-Type'].must_equal 'application/foobar'
|
132
|
+
entry_point.headers['Accept'].must_equal 'application/foobar'
|
133
|
+
end
|
134
|
+
|
135
|
+
it 'creates a Faraday connection with the default block' do
|
136
|
+
handlers = entry_point.connection.builder.handlers
|
137
|
+
handlers.wont_include Faraday::Response::RaiseError
|
138
|
+
handlers.wont_include FaradayMiddleware::FollowRedirects
|
139
|
+
handlers.must_include FaradayMiddleware::EncodeJson
|
140
|
+
handlers.must_include FaradayMiddleware::ParseJson
|
141
|
+
handlers.must_include Faraday::Adapter::NetHttp
|
142
|
+
end
|
114
143
|
end
|
144
|
+
end
|
115
145
|
|
116
|
-
|
117
|
-
|
146
|
+
describe 'inherited' do
|
147
|
+
let(:entry_point) do
|
148
|
+
EntryPoint.new 'http://my.api.org' do |entry_point|
|
149
|
+
entry_point.connection do |conn|
|
150
|
+
conn.use Faraday::Request::OAuth
|
151
|
+
end
|
152
|
+
entry_point.headers['Access-Token'] = 'token'
|
153
|
+
end
|
118
154
|
end
|
119
155
|
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
156
|
+
describe 'connection' do
|
157
|
+
it 'creates a Faraday connection with the default and additional headers' do
|
158
|
+
entry_point.headers['Content-Type'].must_equal 'application/hal+json'
|
159
|
+
entry_point.headers['Accept'].must_equal 'application/hal+json,application/json'
|
160
|
+
entry_point.headers['Access-Token'].must_equal 'token'
|
161
|
+
end
|
162
|
+
|
163
|
+
it 'creates a Faraday connection with the entry point url' do
|
164
|
+
entry_point.connection.url_prefix.to_s.must_equal 'http://my.api.org/'
|
165
|
+
end
|
166
|
+
|
167
|
+
it 'creates a Faraday connection with the default block plus any additional handlers' do
|
168
|
+
handlers = entry_point.connection.builder.handlers
|
169
|
+
handlers.must_include Faraday::Request::OAuth
|
170
|
+
handlers.must_include Faraday::Response::RaiseError
|
171
|
+
handlers.must_include FaradayMiddleware::FollowRedirects
|
172
|
+
handlers.must_include FaradayMiddleware::EncodeHalJson
|
173
|
+
handlers.must_include FaradayMiddleware::ParseHalJson
|
174
|
+
handlers.must_include Faraday::Adapter::NetHttp
|
175
|
+
end
|
128
176
|
end
|
129
177
|
end
|
130
178
|
end
|
@@ -108,7 +108,10 @@ module Hyperclient
|
|
108
108
|
Resource.expects(:new)
|
109
109
|
|
110
110
|
link = Link.new('key', { 'href' => '/' }, entry_point)
|
111
|
-
|
111
|
+
|
112
|
+
stub_request(entry_point.connection) do |stub|
|
113
|
+
stub.get('http://api.example.org/') { [200, {}, nil] }
|
114
|
+
end
|
112
115
|
|
113
116
|
link._resource
|
114
117
|
end
|
@@ -124,14 +127,20 @@ module Hyperclient
|
|
124
127
|
it 'sends a GET request with the link url' do
|
125
128
|
link = Link.new('key', { 'href' => '/productions/1' }, entry_point)
|
126
129
|
|
127
|
-
stub_request(
|
130
|
+
stub_request(entry_point.connection) do |stub|
|
131
|
+
stub.get('http://api.example.org/productions/1') { [200, {}, nil] }
|
132
|
+
end
|
133
|
+
|
128
134
|
link._get.must_be_kind_of Resource
|
129
135
|
end
|
130
136
|
|
131
137
|
it 'raises exceptions by default' do
|
132
138
|
link = Link.new('key', { 'href' => '/productions/1' }, entry_point)
|
133
139
|
|
134
|
-
stub_request(
|
140
|
+
stub_request(entry_point.connection) do |stub|
|
141
|
+
stub.get('http://api.example.org/productions/1') { [400, {}, nil] }
|
142
|
+
end
|
143
|
+
|
135
144
|
lambda { link._get }.must_raise Faraday::ClientError
|
136
145
|
end
|
137
146
|
end
|
@@ -140,7 +149,10 @@ module Hyperclient
|
|
140
149
|
it 'sends a OPTIONS request with the link url' do
|
141
150
|
link = Link.new('key', { 'href' => '/productions/1' }, entry_point)
|
142
151
|
|
143
|
-
stub_request(
|
152
|
+
stub_request(entry_point.connection) do |stub|
|
153
|
+
stub.options('http://api.example.org/productions/1') { [200, {}, nil] }
|
154
|
+
end
|
155
|
+
|
144
156
|
link._options.must_be_kind_of Resource
|
145
157
|
end
|
146
158
|
end
|
@@ -148,7 +160,11 @@ module Hyperclient
|
|
148
160
|
describe '_head' do
|
149
161
|
it 'sends a HEAD request with the link url' do
|
150
162
|
link = Link.new('key', { 'href' => '/productions/1' }, entry_point)
|
151
|
-
|
163
|
+
|
164
|
+
stub_request(entry_point.connection) do |stub|
|
165
|
+
stub.head('http://api.example.org/productions/1') { [200, {}, nil] }
|
166
|
+
end
|
167
|
+
|
152
168
|
link._head.must_be_kind_of Resource
|
153
169
|
end
|
154
170
|
end
|
@@ -156,7 +172,11 @@ module Hyperclient
|
|
156
172
|
describe '_delete' do
|
157
173
|
it 'sends a DELETE request with the link url' do
|
158
174
|
link = Link.new('key', { 'href' => '/productions/1' }, entry_point)
|
159
|
-
|
175
|
+
|
176
|
+
stub_request(entry_point.connection) do |stub|
|
177
|
+
stub.delete('http://api.example.org/productions/1') { [200, {}, nil] }
|
178
|
+
end
|
179
|
+
|
160
180
|
link._delete.must_be_kind_of Resource
|
161
181
|
end
|
162
182
|
end
|
@@ -165,12 +185,20 @@ module Hyperclient
|
|
165
185
|
let(:link) { Link.new('key', { 'href' => '/productions/1' }, entry_point) }
|
166
186
|
|
167
187
|
it 'sends a POST request with the link url and params' do
|
168
|
-
|
188
|
+
|
189
|
+
stub_request(entry_point.connection) do |stub|
|
190
|
+
stub.post('http://api.example.org/productions/1') { [200, {}, nil] }
|
191
|
+
end
|
192
|
+
|
169
193
|
link._post('foo' => 'bar').must_be_kind_of Resource
|
170
194
|
end
|
171
195
|
|
172
196
|
it 'defaults params to an empty hash' do
|
173
|
-
|
197
|
+
|
198
|
+
stub_request(entry_point.connection) do |stub|
|
199
|
+
stub.post('http://api.example.org/productions/1') { [200, {}, nil] }
|
200
|
+
end
|
201
|
+
|
174
202
|
link._post.must_be_kind_of Resource
|
175
203
|
end
|
176
204
|
end
|
@@ -179,12 +207,20 @@ module Hyperclient
|
|
179
207
|
let(:link) { Link.new('key', { 'href' => '/productions/1' }, entry_point) }
|
180
208
|
|
181
209
|
it 'sends a PUT request with the link url and params' do
|
182
|
-
|
210
|
+
|
211
|
+
stub_request(entry_point.connection) do |stub|
|
212
|
+
stub.put('http://api.example.org/productions/1', '{"foo":"bar"}') { [200, {}, nil] }
|
213
|
+
end
|
214
|
+
|
183
215
|
link._put('foo' => 'bar').must_be_kind_of Resource
|
184
216
|
end
|
185
217
|
|
186
218
|
it 'defaults params to an empty hash' do
|
187
|
-
|
219
|
+
|
220
|
+
stub_request(entry_point.connection) do |stub|
|
221
|
+
stub.put('http://api.example.org/productions/1') { [200, {}, nil] }
|
222
|
+
end
|
223
|
+
|
188
224
|
link._put.must_be_kind_of Resource
|
189
225
|
end
|
190
226
|
end
|
@@ -193,12 +229,19 @@ module Hyperclient
|
|
193
229
|
let(:link) { Link.new('key', { 'href' => '/productions/1' }, entry_point) }
|
194
230
|
|
195
231
|
it 'sends a PATCH request with the link url and params' do
|
196
|
-
|
232
|
+
|
233
|
+
stub_request(entry_point.connection) do |stub|
|
234
|
+
stub.patch('http://api.example.org/productions/1', '{"foo":"bar"}') { [200, {}, nil] }
|
235
|
+
end
|
236
|
+
|
197
237
|
link._patch('foo' => 'bar').must_be_kind_of Resource
|
198
238
|
end
|
199
239
|
|
200
240
|
it 'defaults params to an empty hash' do
|
201
|
-
stub_request(
|
241
|
+
stub_request(entry_point.connection) do |stub|
|
242
|
+
stub.patch('http://api.example.org/productions/1') { [200, {}, nil] }
|
243
|
+
end
|
244
|
+
|
202
245
|
link._patch.must_be_kind_of Resource
|
203
246
|
end
|
204
247
|
end
|
@@ -216,14 +259,22 @@ module Hyperclient
|
|
216
259
|
describe 'delegation' do
|
217
260
|
it 'delegates when link key matches' do
|
218
261
|
resource = Resource.new({ '_links' => { 'orders' => { 'href' => '/orders' } } }, entry_point)
|
219
|
-
|
262
|
+
|
263
|
+
stub_request(entry_point.connection) do |stub|
|
264
|
+
stub.get('http://api.example.org/orders') { [200, {}, { '_embedded' => { 'orders' => [{ 'id' => 1 }] } }] }
|
265
|
+
end
|
266
|
+
|
220
267
|
resource.orders._embedded.orders.first.id.must_equal 1
|
221
268
|
resource.orders.first.id.must_equal 1
|
222
269
|
end
|
223
270
|
|
224
271
|
it "doesn't delegate when link key doesn't match" do
|
225
272
|
resource = Resource.new({ '_links' => { 'foos' => { 'href' => '/orders' } } }, entry_point)
|
226
|
-
|
273
|
+
|
274
|
+
stub_request(entry_point.connection) do |stub|
|
275
|
+
stub.get('http://api.example.org/orders') { [200, {}, { '_embedded' => { 'orders' => [{ 'id' => 1 }] } }] }
|
276
|
+
end
|
277
|
+
|
227
278
|
resource.foos._embedded.orders.first.id.must_equal 1
|
228
279
|
resource.foos.first.must_equal nil
|
229
280
|
end
|
@@ -231,8 +282,11 @@ module Hyperclient
|
|
231
282
|
|
232
283
|
describe 'resource' do
|
233
284
|
before do
|
234
|
-
|
235
|
-
|
285
|
+
|
286
|
+
stub_request(entry_point.connection) do |stub|
|
287
|
+
stub.get('http://myapi.org/orders') { [200, {}, '{"resource": "This is the resource"}'] }
|
288
|
+
end
|
289
|
+
|
236
290
|
Resource.stubs(:new).returns(resource)
|
237
291
|
end
|
238
292
|
|
data/test/hyperclient_test.rb
CHANGED
@@ -20,8 +20,8 @@ describe Hyperclient do
|
|
20
20
|
end
|
21
21
|
|
22
22
|
it 'creates a Faraday connection with the default and additional headers' do
|
23
|
-
client.headers['Content-Type'].must_equal 'application/json'
|
24
|
-
client.headers['Accept'].must_equal 'application/json'
|
23
|
+
client.headers['Content-Type'].must_equal 'application/hal+json'
|
24
|
+
client.headers['Accept'].must_equal 'application/hal+json,application/json'
|
25
25
|
client.headers['Access-Token'].must_equal 'token'
|
26
26
|
end
|
27
27
|
|
@@ -34,8 +34,8 @@ describe Hyperclient do
|
|
34
34
|
handlers.must_include Faraday::Request::OAuth
|
35
35
|
handlers.must_include Faraday::Response::RaiseError
|
36
36
|
handlers.must_include FaradayMiddleware::FollowRedirects
|
37
|
-
handlers.must_include FaradayMiddleware::
|
38
|
-
handlers.must_include FaradayMiddleware::
|
37
|
+
handlers.must_include FaradayMiddleware::EncodeHalJson
|
38
|
+
handlers.must_include FaradayMiddleware::ParseHalJson
|
39
39
|
handlers.must_include Faraday::Adapter::NetHttp
|
40
40
|
end
|
41
41
|
end
|
data/test/test_helper.rb
CHANGED
@@ -5,6 +5,14 @@ require 'minitest/spec'
|
|
5
5
|
require 'minitest/autorun'
|
6
6
|
require 'mocha/setup'
|
7
7
|
require 'turn'
|
8
|
-
require 'webmock/minitest'
|
9
8
|
require 'json'
|
10
9
|
require 'pry'
|
10
|
+
|
11
|
+
MiniTest::Unit::TestCase.class_eval do
|
12
|
+
|
13
|
+
def stub_request(conn, adapter_class = Faraday::Adapter::Test, &stubs_block)
|
14
|
+
adapter_handler = conn.builder.handlers.find { |h| h.klass < Faraday::Adapter }
|
15
|
+
conn.builder.swap(adapter_handler, adapter_class, &stubs_block)
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hyperclient
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.7.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Oriol Gual
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-02-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faraday
|
@@ -52,6 +52,20 @@ dependencies:
|
|
52
52
|
- - ~>
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0.9'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: faraday_hal_middleware
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 0.0.1
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ~>
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 0.0.1
|
55
69
|
- !ruby/object:Gem::Dependency
|
56
70
|
name: uri_template
|
57
71
|
requirement: !ruby/object:Gem::Requirement
|
@@ -251,7 +265,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
251
265
|
version: '0'
|
252
266
|
requirements: []
|
253
267
|
rubyforge_project:
|
254
|
-
rubygems_version: 2.
|
268
|
+
rubygems_version: 2.4.5
|
255
269
|
signing_key:
|
256
270
|
specification_version: 4
|
257
271
|
summary: ''
|