hyperclient 0.9.1 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,6 +4,8 @@ module Hyperclient
4
4
  # Internal: The Link is used to let a Resource interact with the API.
5
5
  #
6
6
  class Link
7
+ include Enumerable
8
+
7
9
  # Public: Initializes a new Link.
8
10
  #
9
11
  # key - The key or name of the link.
@@ -19,6 +21,25 @@ module Hyperclient
19
21
  @resource = nil
20
22
  end
21
23
 
24
+ # Public: Each implementation to allow the class to use the Enumerable
25
+ # benefits for paginated, embedded items.
26
+ #
27
+ # Returns an Enumerator.
28
+ def each(&block)
29
+ if block_given?
30
+ current = self
31
+ while current
32
+ coll = current.respond_to?(@key) ? current.send(@key) : _resource
33
+ coll.each(&block)
34
+ break unless current._links[:next]
35
+
36
+ current = current._links.next
37
+ end
38
+ else
39
+ to_enum(:each)
40
+ end
41
+ end
42
+
22
43
  # Public: Indicates if the link is an URITemplate or a regular URI.
23
44
  #
24
45
  # Returns true if it is templated.
@@ -15,7 +15,9 @@ module Hyperclient
15
15
  # curies - The Hash with link curies.
16
16
  # entry_point - The EntryPoint object to inject the configuration.
17
17
  def initialize(collection, curies, entry_point)
18
- raise "Invalid response for LinkCollection. The response was: #{collection.inspect}" if collection && !collection.respond_to?(:collect)
18
+ if collection && !collection.respond_to?(:collect)
19
+ raise "Invalid response for LinkCollection. The response was: #{collection.inspect}"
20
+ end
19
21
 
20
22
  @_curies = (curies || {}).reduce({}) do |hash, curie_hash|
21
23
  curie = build_curie(curie_hash, entry_point)
@@ -111,7 +111,9 @@ module Hyperclient
111
111
  elsif !Array.method_defined?(method)
112
112
  %i[_attributes _embedded _links].each do |target|
113
113
  target = send(target)
114
- return target.send(method, *args, &block) if target.respond_to?(method.to_s)
114
+ if target.respond_to?(method.to_s)
115
+ return target.send(method, *args, &block)
116
+ end
115
117
  end
116
118
  super
117
119
  end
@@ -23,7 +23,9 @@ module Hyperclient
23
23
  private
24
24
 
25
25
  def build_resource(representation)
26
- return representation.map(&method(:build_resource)) if representation.is_a?(Array)
26
+ if representation.is_a?(Array)
27
+ return representation.map(&method(:build_resource))
28
+ end
27
29
 
28
30
  Resource.new(representation, @entry_point)
29
31
  end
@@ -1,3 +1,3 @@
1
1
  module Hyperclient
2
- VERSION = '0.9.1'.freeze
2
+ VERSION = '1.0.1'.freeze
3
3
  end
@@ -12,29 +12,29 @@ module Hyperclient
12
12
  end
13
13
 
14
14
  it 'does not set _links as an attribute' do
15
- attributes.wont_respond_to :_links
15
+ _(attributes).wont_respond_to :_links
16
16
  end
17
17
 
18
18
  it 'does not set _embedded as an attribute' do
19
- attributes.wont_respond_to :_embedded
19
+ _(attributes).wont_respond_to :_embedded
20
20
  end
21
21
 
22
22
  it 'sets normal attributes' do
23
- attributes.must_respond_to :permitted
24
- attributes.permitted.must_equal true
23
+ _(attributes).must_respond_to :permitted
24
+ _(attributes.permitted).must_equal true
25
25
 
26
- attributes.must_respond_to :title
27
- attributes.title.must_equal 'Real World ASP.NET MVC3'
26
+ _(attributes).must_respond_to :title
27
+ _(attributes.title).must_equal 'Real World ASP.NET MVC3'
28
28
  end
29
29
 
30
30
  # Underscores should be allowed per http://tools.ietf.org/html/draft-kelly-json-hal#appendix-B.4
31
31
  it 'sets _hidden_attribute as an attribute' do
32
- attributes.must_respond_to :_hidden_attribute
33
- attributes._hidden_attribute.must_equal 'useful value'
32
+ _(attributes).must_respond_to :_hidden_attribute
33
+ _(attributes._hidden_attribute).must_equal 'useful value'
34
34
  end
35
35
 
36
36
  it 'is a collection' do
37
- Attributes.ancestors.must_include Collection
37
+ _(Attributes.ancestors).must_include Collection
38
38
  end
39
39
  end
40
40
  end
@@ -12,19 +12,19 @@ module Hyperclient
12
12
  end
13
13
 
14
14
  it 'exposes the collection as methods' do
15
- collection.title.must_equal 'Real World ASP.NET MVC3'
16
- collection.description.must_match(/production/)
17
- collection.permitted.must_equal true
15
+ _(collection.title).must_equal 'Real World ASP.NET MVC3'
16
+ _(collection.description).must_match(/production/)
17
+ _(collection.permitted).must_equal true
18
18
  end
19
19
 
20
20
  it 'exposes collection as a hash' do
21
- collection['title'].must_equal 'Real World ASP.NET MVC3'
22
- collection['description'].must_match(/production/)
23
- collection['permitted'].must_equal true
21
+ _(collection['title']).must_equal 'Real World ASP.NET MVC3'
22
+ _(collection['description']).must_match(/production/)
23
+ _(collection['permitted']).must_equal true
24
24
  end
25
25
 
26
26
  it 'correctly responds to methods' do
27
- collection.must_respond_to :title
27
+ _(collection).must_respond_to :title
28
28
  end
29
29
 
30
30
  it 'acts as enumerable' do
@@ -32,41 +32,41 @@ module Hyperclient
32
32
  name
33
33
  end
34
34
 
35
- names.must_equal %w[_links title description permitted _hidden_attribute _embedded]
35
+ _(names).must_equal %w[_links title description permitted _hidden_attribute _embedded]
36
36
  end
37
37
 
38
38
  describe '#to_hash' do
39
39
  it 'returns the wrapped collection as a hash' do
40
- collection.to_hash.must_be_kind_of Hash
40
+ _(collection.to_hash).must_be_kind_of Hash
41
41
  end
42
42
  end
43
43
 
44
44
  describe 'include?' do
45
45
  it 'returns true for keys that exist' do
46
- collection.include?('_links').must_equal true
46
+ _(collection.include?('_links')).must_equal true
47
47
  end
48
48
 
49
49
  it 'returns false for missing keys' do
50
- collection.include?('missing key').must_equal false
50
+ _(collection.include?('missing key')).must_equal false
51
51
  end
52
52
  end
53
53
 
54
54
  describe '#fetch' do
55
55
  it 'returns the value for keys that exist' do
56
- collection.fetch('title').must_equal 'Real World ASP.NET MVC3'
56
+ _(collection.fetch('title')).must_equal 'Real World ASP.NET MVC3'
57
57
  end
58
58
 
59
59
  it 'raises an error for missing keys' do
60
- proc { collection.fetch('missing key') }.must_raise KeyError
60
+ _(proc { collection.fetch('missing key') }).must_raise KeyError
61
61
  end
62
62
 
63
63
  describe 'with a default value' do
64
64
  it 'returns the value for keys that exist' do
65
- collection.fetch('title', 'default').must_equal 'Real World ASP.NET MVC3'
65
+ _(collection.fetch('title', 'default')).must_equal 'Real World ASP.NET MVC3'
66
66
  end
67
67
 
68
68
  it 'returns the default value for missing keys' do
69
- collection.fetch('missing key', 'default').must_equal 'default'
69
+ _(collection.fetch('missing key', 'default')).must_equal 'default'
70
70
  end
71
71
  end
72
72
  end
@@ -11,13 +11,13 @@ module Hyperclient
11
11
  it 'returns true if the curie is templated' do
12
12
  curie = Curie.new({ 'name' => 'image', 'templated' => true }, entry_point)
13
13
 
14
- curie.templated?.must_equal true
14
+ _(curie.templated?).must_equal true
15
15
  end
16
16
 
17
17
  it 'returns false if the curie is not templated' do
18
18
  curie = Curie.new({ 'name' => 'image' }, entry_point)
19
19
 
20
- curie.templated?.must_equal false
20
+ _(curie.templated?).must_equal false
21
21
  end
22
22
  end
23
23
 
@@ -26,12 +26,12 @@ module Hyperclient
26
26
  end
27
27
  describe '_name' do
28
28
  it 'returns curie name' do
29
- curie.name.must_equal 'image'
29
+ _(curie.name).must_equal 'image'
30
30
  end
31
31
  end
32
32
  describe 'expand' do
33
33
  it 'expands link' do
34
- curie.expand('thumbnail').must_equal '/images/thumbnail'
34
+ _(curie.expand('thumbnail')).must_equal '/images/thumbnail'
35
35
  end
36
36
  end
37
37
  end
@@ -10,53 +10,52 @@ module Hyperclient
10
10
 
11
11
  describe 'connection' do
12
12
  it 'creates a Faraday connection with the entry point url' do
13
- entry_point.connection.url_prefix.to_s.must_equal 'http://my.api.org/'
13
+ _(entry_point.connection.url_prefix.to_s).must_equal 'http://my.api.org/'
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/hal+json'
18
- entry_point.headers['Accept'].must_equal 'application/hal+json,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
22
- entry_point.connection.must_be_kind_of Faraday::Connection
22
+ _(entry_point.connection).must_be_kind_of Faraday::Connection
23
23
  entry_point.headers.update('Content-Type' => 'application/foobar')
24
- entry_point.headers['Content-Type'].must_equal 'application/foobar'
24
+ _(entry_point.headers['Content-Type']).must_equal 'application/foobar'
25
25
  end
26
26
 
27
27
  it 'can insert additional middleware after a connection has been constructed' do
28
- entry_point.connection.must_be_kind_of Faraday::Connection
28
+ _(entry_point.connection).must_be_kind_of Faraday::Connection
29
29
  entry_point.connection.use :instrumentation
30
30
  handlers = entry_point.connection.builder.handlers
31
- handlers.must_include FaradayMiddleware::Instrumentation
31
+ _(handlers).must_include FaradayMiddleware::Instrumentation
32
32
  end
33
33
 
34
34
  it 'creates a Faraday connection with the default block' do
35
35
  handlers = entry_point.connection.builder.handlers
36
36
 
37
- handlers.must_include Faraday::Response::RaiseError
38
- handlers.must_include FaradayMiddleware::FollowRedirects
39
- handlers.must_include FaradayMiddleware::EncodeHalJson
40
- handlers.must_include FaradayMiddleware::ParseHalJson
41
- handlers.must_include Faraday::Adapter::NetHttp
37
+ _(handlers).must_include Faraday::Response::RaiseError
38
+ _(handlers).must_include FaradayMiddleware::FollowRedirects
39
+ _(handlers).must_include FaradayMiddleware::EncodeHalJson
40
+ _(handlers).must_include FaradayMiddleware::ParseHalJson
42
41
 
43
- entry_point.connection.options.params_encoder.must_equal Faraday::FlatParamsEncoder
42
+ _(entry_point.connection.options.params_encoder).must_equal Faraday::FlatParamsEncoder
44
43
  end
45
44
 
46
45
  it 'raises a ConnectionAlreadyInitializedError if attempting to modify headers' do
47
- entry_point.connection.must_be_kind_of Faraday::Connection
48
- -> { entry_point.headers = {} }.must_raise ConnectionAlreadyInitializedError
46
+ _(entry_point.connection).must_be_kind_of Faraday::Connection
47
+ _(-> { entry_point.headers = {} }).must_raise ConnectionAlreadyInitializedError
49
48
  end
50
49
 
51
50
  it 'raises a ConnectionAlreadyInitializedError if attempting to modify the faraday block' do
52
- entry_point.connection.must_be_kind_of Faraday::Connection
53
- -> { entry_point.connection {} }.must_raise ConnectionAlreadyInitializedError
51
+ _(entry_point.connection).must_be_kind_of Faraday::Connection
52
+ _(-> { entry_point.connection {} }).must_raise ConnectionAlreadyInitializedError
54
53
  end
55
54
  end
56
55
 
57
56
  describe 'initialize' do
58
57
  it 'sets a Link with the entry point url' do
59
- entry_point._url.must_equal 'http://my.api.org'
58
+ _(entry_point._url).must_equal 'http://my.api.org'
60
59
  end
61
60
  end
62
61
  end
@@ -70,17 +69,17 @@ module Hyperclient
70
69
 
71
70
  describe 'connection' do
72
71
  it 'creates a Faraday connection with the entry point url' do
73
- entry_point.connection.url_prefix.to_s.must_equal 'http://my.api.org/'
72
+ _(entry_point.connection.url_prefix.to_s).must_equal 'http://my.api.org/'
74
73
  end
75
74
 
76
75
  it 'creates a Faraday connection with the default headers' do
77
- entry_point.headers['Content-Type'].must_equal 'application/hal+json'
78
- entry_point.headers['Accept'].must_equal 'application/hal+json,application/json'
76
+ _(entry_point.headers['Content-Type']).must_equal 'application/hal+json'
77
+ _(entry_point.headers['Accept']).must_equal 'application/hal+json,application/json'
79
78
  end
80
79
 
81
80
  it 'creates a Faraday connection with options' do
82
- entry_point.connection.proxy.must_be_kind_of Faraday::ProxyOptions
83
- entry_point.connection.proxy.uri.to_s.must_equal 'http://my.proxy:8080'
81
+ _(entry_point.connection.proxy).must_be_kind_of Faraday::ProxyOptions
82
+ _(entry_point.connection.proxy.uri.to_s).must_equal 'http://my.proxy:8080'
84
83
  end
85
84
  end
86
85
  end
@@ -94,17 +93,17 @@ module Hyperclient
94
93
 
95
94
  describe 'connection' do
96
95
  it 'creates a Faraday connection with the entry point url' do
97
- entry_point.connection.url_prefix.to_s.must_equal 'http://my.api.org/'
96
+ _(entry_point.connection.url_prefix.to_s).must_equal 'http://my.api.org/'
98
97
  end
99
98
 
100
99
  it 'creates a Faraday connection with the default headers' do
101
- entry_point.headers['Content-Type'].must_equal 'application/hal+json'
102
- entry_point.headers['Accept'].must_equal 'application/hal+json,application/json'
100
+ _(entry_point.headers['Content-Type']).must_equal 'application/hal+json'
101
+ _(entry_point.headers['Accept']).must_equal 'application/hal+json,application/json'
103
102
  end
104
103
 
105
104
  it 'creates a Faraday connection with options' do
106
- entry_point.connection.proxy.must_be_kind_of Faraday::ProxyOptions
107
- entry_point.connection.proxy.uri.to_s.must_equal 'http://my.proxy:8080'
105
+ _(entry_point.connection.proxy).must_be_kind_of Faraday::ProxyOptions
106
+ _(entry_point.connection.proxy.uri.to_s).must_equal 'http://my.proxy:8080'
108
107
  end
109
108
  end
110
109
  end
@@ -127,21 +126,20 @@ module Hyperclient
127
126
 
128
127
  describe 'connection' do
129
128
  it 'creates a Faraday connection with the entry point url' do
130
- entry_point.connection.url_prefix.to_s.must_equal 'http://my.api.org/'
129
+ _(entry_point.connection.url_prefix.to_s).must_equal 'http://my.api.org/'
131
130
  end
132
131
 
133
132
  it 'creates a Faraday connection with non-default headers' do
134
- entry_point.headers['Content-Type'].must_equal 'application/foobar'
135
- entry_point.headers['Accept'].must_equal 'application/foobar'
133
+ _(entry_point.headers['Content-Type']).must_equal 'application/foobar'
134
+ _(entry_point.headers['Accept']).must_equal 'application/foobar'
136
135
  end
137
136
 
138
137
  it 'creates a Faraday connection with the default block' do
139
138
  handlers = entry_point.connection.builder.handlers
140
- handlers.wont_include Faraday::Response::RaiseError
141
- handlers.wont_include FaradayMiddleware::FollowRedirects
142
- handlers.must_include FaradayMiddleware::EncodeJson
143
- handlers.must_include FaradayMiddleware::ParseJson
144
- handlers.must_include Faraday::Adapter::NetHttp
139
+ _(handlers).wont_include Faraday::Response::RaiseError
140
+ _(handlers).wont_include FaradayMiddleware::FollowRedirects
141
+ _(handlers).must_include FaradayMiddleware::EncodeJson
142
+ _(handlers).must_include FaradayMiddleware::ParseJson
145
143
  end
146
144
  end
147
145
  end
@@ -158,26 +156,25 @@ module Hyperclient
158
156
 
159
157
  describe 'connection' do
160
158
  it 'creates a Faraday connection with the default and additional headers' do
161
- entry_point.headers['Content-Type'].must_equal 'application/hal+json'
162
- entry_point.headers['Accept'].must_equal 'application/hal+json,application/json'
163
- entry_point.headers['Access-Token'].must_equal 'token'
159
+ _(entry_point.headers['Content-Type']).must_equal 'application/hal+json'
160
+ _(entry_point.headers['Accept']).must_equal 'application/hal+json,application/json'
161
+ _(entry_point.headers['Access-Token']).must_equal 'token'
164
162
  end
165
163
 
166
164
  it 'creates a Faraday connection with the entry point url' do
167
- entry_point.connection.url_prefix.to_s.must_equal 'http://my.api.org/'
165
+ _(entry_point.connection.url_prefix.to_s).must_equal 'http://my.api.org/'
168
166
  end
169
167
 
170
168
  it 'creates a Faraday connection with the default block plus any additional handlers' do
171
169
  handlers = entry_point.connection.builder.handlers
172
170
 
173
- handlers.must_include Faraday::Request::OAuth
174
- handlers.must_include Faraday::Response::RaiseError
175
- handlers.must_include FaradayMiddleware::FollowRedirects
176
- handlers.must_include FaradayMiddleware::EncodeHalJson
177
- handlers.must_include FaradayMiddleware::ParseHalJson
178
- handlers.must_include Faraday::Adapter::NetHttp
171
+ _(handlers).must_include Faraday::Request::OAuth
172
+ _(handlers).must_include Faraday::Response::RaiseError
173
+ _(handlers).must_include FaradayMiddleware::FollowRedirects
174
+ _(handlers).must_include FaradayMiddleware::EncodeHalJson
175
+ _(handlers).must_include FaradayMiddleware::ParseHalJson
179
176
 
180
- entry_point.connection.options.params_encoder.must_equal Faraday::FlatParamsEncoder
177
+ _(entry_point.connection.options.params_encoder).must_equal Faraday::FlatParamsEncoder
181
178
  end
182
179
  end
183
180
  end
@@ -14,33 +14,33 @@ module Hyperclient
14
14
  end
15
15
 
16
16
  it 'is a collection' do
17
- LinkCollection.ancestors.must_include Collection
17
+ _(LinkCollection.ancestors).must_include Collection
18
18
  end
19
19
 
20
20
  it 'initializes the collection with links' do
21
- links.must_respond_to :search
22
- links.must_respond_to :gizmos
21
+ _(links).must_respond_to :search
22
+ _(links).must_respond_to :gizmos
23
23
  end
24
24
 
25
25
  it 'returns link objects for each link' do
26
- links.search.must_be_kind_of Link
27
- links['self'].must_be_kind_of Link
26
+ _(links.search).must_be_kind_of Link
27
+ _(links['self']).must_be_kind_of Link
28
28
 
29
- links.gizmos.must_be_kind_of Array
30
- links['gizmos'].must_be_kind_of Array
29
+ _(links.gizmos).must_be_kind_of Array
30
+ _(links['gizmos']).must_be_kind_of Array
31
31
  end
32
32
 
33
33
  describe 'plain link' do
34
34
  let(:plain_link) { links.self }
35
35
  it 'must be correct' do
36
- plain_link._url.must_equal '/productions/1'
36
+ _(plain_link._url).must_equal '/productions/1'
37
37
  end
38
38
  end
39
39
 
40
40
  describe 'templated link' do
41
41
  let(:templated_link) { links.search }
42
42
  it 'must expand' do
43
- templated_link._expand(search: 'gizmos')._url.must_equal '/productions/1?categories=gizmos'
43
+ _(templated_link._expand(search: 'gizmos')._url).must_equal '/productions/1?categories=gizmos'
44
44
  end
45
45
  end
46
46
 
@@ -48,10 +48,10 @@ module Hyperclient
48
48
  let(:curied_link) { links['image:thumbnail'] }
49
49
  let(:curie) { links._curies['image'] }
50
50
  it 'must expand' do
51
- curied_link._expand(version: 'small')._url.must_equal '/images/thumbnails/small.jpg'
51
+ _(curied_link._expand(version: 'small')._url).must_equal '/images/thumbnails/small.jpg'
52
52
  end
53
53
  it 'exposes curie' do
54
- curie.expand('thumbnail').must_equal '/docs/images/thumbnail'
54
+ _(curie.expand('thumbnail')).must_equal '/docs/images/thumbnail'
55
55
  end
56
56
  end
57
57
 
@@ -59,12 +59,12 @@ module Hyperclient
59
59
  let(:gizmos) { links.gizmos }
60
60
 
61
61
  it 'should have 2 items' do
62
- gizmos.length.must_equal 2
62
+ _(gizmos.length).must_equal 2
63
63
  end
64
64
 
65
65
  it 'must be an array of Links' do
66
66
  gizmos.each do |link|
67
- link.must_be_kind_of Link
67
+ _(link).must_be_kind_of Link
68
68
  end
69
69
  end
70
70
  end
@@ -72,7 +72,7 @@ module Hyperclient
72
72
  describe 'null link value' do
73
73
  let(:null_link) { links.null_link }
74
74
  it 'must be nil' do
75
- null_link.must_be_nil
75
+ _(null_link).must_be_nil
76
76
  end
77
77
  end
78
78
  end