hal-client 3.10.1 → 3.11.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 +8 -3
- data/lib/hal_client/collection.rb +1 -1
- data/lib/hal_client/representation.rb +20 -0
- data/lib/hal_client/representation_set.rb +1 -1
- data/lib/hal_client/version.rb +1 -1
- data/spec/hal_client/collection_spec.rb +56 -26
- data/spec/hal_client/representation_spec.rb +10 -0
- metadata +3 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 253c1dbaeef9a44aa3d64d9f6a440cdcd04567ad
|
4
|
+
data.tar.gz: 74898c74f70a37334b50b8ce1284759c0043e4be
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5b8bece5e9c60060a29711057b029d768c6661cdf654f3d3ff507f1894ceebfbd92111d7675bfa310d20037c0d9b4719191b445e058f5d08eb253d8752f1e9f4
|
7
|
+
data.tar.gz: f72e017752ebbcf1a3274cc798a68ae9b8724f5bd78c850abceb941632b3649d3c14bf48476d37153186a7df8c23ea5d7e57efa05dfc6585f7634d5d71233e74
|
data/README.md
CHANGED
@@ -14,11 +14,16 @@ The first step in using a HAL based API is getting a representation of one of it
|
|
14
14
|
blog = HalClient.get("http://blog.me/")
|
15
15
|
# => #<Representation: http://blog.me/>
|
16
16
|
|
17
|
-
`HalClient::Representation`s expose a `#property` method to retrieve
|
17
|
+
`HalClient::Representation`s expose a `#property` method to retrieve a single property from the HAL document.
|
18
18
|
|
19
19
|
blog.property('title')
|
20
20
|
#=> "Some Person's Blog"
|
21
21
|
|
22
|
+
They also expose a `#properties` method to retrieve all the properties from the document, as a `Hash`.
|
23
|
+
|
24
|
+
blog.properties
|
25
|
+
#=> {"title"=>"Some Person's Blog", "description"=>"Some description"}
|
26
|
+
|
22
27
|
### Link navigation
|
23
28
|
|
24
29
|
Once we have a representation we will want to navigate its links. This can be accomplished using the `#related` method.
|
@@ -79,12 +84,12 @@ All `HalClient::Representation`s exposed an `#href` attribute which is its ident
|
|
79
84
|
|
80
85
|
HalClient provides a high level abstraction for paged collections encoded using [standard `item`, `next` and `prev` link relations](http://tools.ietf.org/html/rfc6573).
|
81
86
|
|
82
|
-
articles = blog.
|
87
|
+
articles = blog.to_enum
|
83
88
|
articles.each do |an_article|
|
84
89
|
# do something with each article representation
|
85
90
|
end
|
86
91
|
|
87
|
-
If the collection is paged this will navigate to the next page after yielding all the items on the current page.
|
92
|
+
If the collection is paged this will navigate to the next page after yielding all the items on the current page. The return is an `Enumerable` so all your favorite collection methods are available.
|
88
93
|
|
89
94
|
### PUT/POST/PATCH requests
|
90
95
|
|
@@ -52,7 +52,7 @@ class HalClient
|
|
52
52
|
end
|
53
53
|
end
|
54
54
|
|
55
|
-
# Returns one or more randomly selected
|
55
|
+
# Returns one or more randomly selected items from the first page
|
56
56
|
# of the collection.
|
57
57
|
#
|
58
58
|
# count - number of items to return. If specified return type will
|
@@ -11,6 +11,10 @@ class HalClient
|
|
11
11
|
class Representation
|
12
12
|
extend Forwardable
|
13
13
|
|
14
|
+
# Collection of reserved properties
|
15
|
+
# https://tools.ietf.org/html/draft-kelly-json-hal-07#section-4.1
|
16
|
+
RESERVED_PROPERTIES = ['_links', '_embedded'].freeze
|
17
|
+
|
14
18
|
# Create a new Representation
|
15
19
|
#
|
16
20
|
# options - name parameters
|
@@ -90,6 +94,13 @@ class HalClient
|
|
90
94
|
raw.fetch(name.to_s, &default_proc)
|
91
95
|
end
|
92
96
|
|
97
|
+
# Returns a Hash including the key-value pairs of all the properties
|
98
|
+
# in the resource. It does not include HAL's reserved
|
99
|
+
# properties (`_links` and `_embedded`).
|
100
|
+
def properties
|
101
|
+
raw.reject { |k, _| RESERVED_PROPERTIES.include? k }
|
102
|
+
end
|
103
|
+
|
93
104
|
# Returns the URL of the resource this representation represents.
|
94
105
|
def href
|
95
106
|
@href ||= if has_related? "self"
|
@@ -219,6 +230,15 @@ class HalClient
|
|
219
230
|
Collection.new(self)
|
220
231
|
end
|
221
232
|
|
233
|
+
# Returns an Enumerator of the items in the collection resource
|
234
|
+
# if this is an rfc 6573 collection.
|
235
|
+
#
|
236
|
+
# Raises HalClient::NotACollectionError if this is not a
|
237
|
+
# collection resource.
|
238
|
+
def to_enum(method=:each, *args, &blk)
|
239
|
+
as_enum.to_enum(method, *args, &blk)
|
240
|
+
end
|
241
|
+
|
222
242
|
# Resets this representation such that it will be requested from
|
223
243
|
# the upstream on it's next use.
|
224
244
|
def reset
|
@@ -11,7 +11,7 @@ class HalClient
|
|
11
11
|
@reprs = reprs
|
12
12
|
end
|
13
13
|
|
14
|
-
def_delegators :reprs, :each, :count, :empty?, :any
|
14
|
+
def_delegators :reprs, :each, :count, :empty?, :any?, :sample
|
15
15
|
|
16
16
|
# Returns representations of resources related via the specified
|
17
17
|
# link rel or the specified default value.
|
data/lib/hal_client/version.rb
CHANGED
@@ -3,6 +3,34 @@ require_relative "../spec_helper"
|
|
3
3
|
require 'hal_client/collection'
|
4
4
|
|
5
5
|
describe HalClient::Collection do
|
6
|
+
# BACKGROUND
|
7
|
+
|
8
|
+
shared_context "multi-item, multi-page" do
|
9
|
+
subject(:collection) { described_class.new(first_page) }
|
10
|
+
let!(:second_page_req) { stub_request(:get, second_page.href)
|
11
|
+
.to_return body: second_page.to_json }
|
12
|
+
|
13
|
+
let(:first_page_href) { "http://example.com/p1" }
|
14
|
+
let(:first_page) { collection_page(next_href: second_page_href,
|
15
|
+
self_href: first_page_href,
|
16
|
+
items: ["foo", "bar"]) }
|
17
|
+
|
18
|
+
let(:second_page_href) { "http://example.com/p2" }
|
19
|
+
let(:second_page) { collection_page(items: ["baz"],
|
20
|
+
self_href: second_page_href,
|
21
|
+
prev_href: first_page_href) }
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
shared_context "multi-item, single page" do
|
26
|
+
subject(:collection) { described_class.new(only_page) }
|
27
|
+
|
28
|
+
let(:only_page) { collection_page(self_href: "http://example.com/p1",
|
29
|
+
items: ["foo", "bar"]) }
|
30
|
+
end
|
31
|
+
|
32
|
+
# END OF BACKGROUND
|
33
|
+
|
6
34
|
describe "creation" do
|
7
35
|
subject { described_class }
|
8
36
|
|
@@ -25,45 +53,47 @@ describe HalClient::Collection do
|
|
25
53
|
let(:non_first_page) { collection_page(prev_href: "http://example.com/p1") }
|
26
54
|
end
|
27
55
|
|
28
|
-
describe "
|
29
|
-
|
30
|
-
|
31
|
-
.to_return body: second_page.to_json }
|
56
|
+
describe "#each" do
|
57
|
+
context do
|
58
|
+
include_context "multi-item, multi-page"
|
32
59
|
|
33
|
-
|
34
|
-
|
60
|
+
it "fetches all the pages when iterating" do
|
61
|
+
collection.each do |it| end
|
35
62
|
|
36
|
-
|
37
|
-
|
63
|
+
expect(a_request(:get, second_page.href)).to have_been_made
|
64
|
+
end
|
65
|
+
|
66
|
+
it "yields all the items" do
|
67
|
+
yielded = collection.map { |it| it.href }
|
68
|
+
expect(yielded).to eq ["foo", "bar", "baz"]
|
69
|
+
end
|
38
70
|
|
39
|
-
it "iteration yields all the items" do
|
40
|
-
yielded = collection.map { |it| it.href }
|
41
|
-
expect(yielded).to eq ["foo", "bar", "baz"]
|
42
71
|
end
|
72
|
+
end
|
43
73
|
|
44
|
-
|
74
|
+
describe "#count" do
|
75
|
+
context do
|
76
|
+
include_context "multi-item, single page"
|
45
77
|
|
78
|
+
specify { expect(collection.count).to eq 2 }
|
79
|
+
end
|
46
80
|
|
47
|
-
|
48
|
-
|
49
|
-
self_href: first_page_href,
|
50
|
-
items: ["foo", "bar"]) }
|
81
|
+
context do
|
82
|
+
include_context "multi-item, multi-page"
|
51
83
|
|
52
|
-
|
53
|
-
|
54
|
-
self_href: second_page_href,
|
55
|
-
prev_href: first_page_href) }
|
84
|
+
specify { expect { collection.count }.to raise_exception }
|
85
|
+
end
|
56
86
|
end
|
57
87
|
|
58
|
-
describe "
|
59
|
-
|
60
|
-
|
61
|
-
specify { expect(collection.count).to eq 2 }
|
88
|
+
describe "#sample" do
|
89
|
+
include_context "multi-item, multi-page"
|
62
90
|
|
63
|
-
|
64
|
-
|
91
|
+
specify { expect( collection.sample ).to be }
|
92
|
+
specify { expect( collection.sample ).to be_kind_of HalClient::Representation }
|
65
93
|
end
|
66
94
|
|
95
|
+
# BACKGROUND
|
96
|
+
|
67
97
|
let(:hal_client) { HalClient.new }
|
68
98
|
|
69
99
|
def collection_page(opts={})
|
@@ -5,6 +5,7 @@ require "hal_client/representation"
|
|
5
5
|
describe HalClient::Representation do
|
6
6
|
let(:raw_repr) { <<-HAL }
|
7
7
|
{ "prop1": 1
|
8
|
+
,"prop2": 2
|
8
9
|
,"_links": {
|
9
10
|
"self": { "href": "http://example.com/foo" }
|
10
11
|
,"link1": { "href": "http://example.com/bar" }
|
@@ -161,6 +162,9 @@ HAL
|
|
161
162
|
specify { expect(repr.property? "nonexistent-prop").to be false }
|
162
163
|
specify { expect(repr.has_property? "nonexistent-prop").to be false }
|
163
164
|
|
165
|
+
specify { expect(repr.properties).to include("prop1" => 1, "prop2" => 2) }
|
166
|
+
specify { expect(repr.properties).to_not include("_links" => 1,
|
167
|
+
"_embedded" => 2) }
|
164
168
|
|
165
169
|
specify { expect(subject.href).to eq "http://example.com/foo" }
|
166
170
|
|
@@ -361,6 +365,7 @@ HAL
|
|
361
365
|
end
|
362
366
|
|
363
367
|
specify { expect(repr).to respond_to :as_enum }
|
368
|
+
specify { expect(repr).to respond_to :to_enum }
|
364
369
|
|
365
370
|
context "collection" do
|
366
371
|
let(:raw_repr) { <<-HAL }
|
@@ -378,10 +383,15 @@ HAL
|
|
378
383
|
|
379
384
|
specify { expect(repr.as_enum).to a_kind_of Enumerable }
|
380
385
|
specify { expect(repr.as_enum).to have(2).items }
|
386
|
+
|
387
|
+
specify { expect( repr.to_enum ).to be_kind_of Enumerator }
|
388
|
+
specify { expect( repr.to_enum(:each) ).to be_kind_of Enumerator }
|
389
|
+
specify { expect( repr.to_enum ).to have(2).items }
|
381
390
|
end
|
382
391
|
|
383
392
|
context "non-collection" do
|
384
393
|
specify { expect{repr.as_enum}.to raise_error }
|
394
|
+
specify { expect{repr.to_enum}.to raise_error }
|
385
395
|
end
|
386
396
|
|
387
397
|
# Background
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hal-client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.
|
4
|
+
version: 3.11.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Peter Williams
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-12-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: http
|
@@ -181,7 +181,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
181
181
|
version: '0'
|
182
182
|
requirements: []
|
183
183
|
rubyforge_project:
|
184
|
-
rubygems_version: 2.2.
|
184
|
+
rubygems_version: 2.2.5
|
185
185
|
signing_key:
|
186
186
|
specification_version: 4
|
187
187
|
summary: Use HAL APIs easily
|
@@ -195,4 +195,3 @@ test_files:
|
|
195
195
|
- spec/hal_client_spec.rb
|
196
196
|
- spec/spec_helper.rb
|
197
197
|
- spec/support/custom_matchers.rb
|
198
|
-
has_rdoc:
|