Skip to content

Commit e5b42e7

Browse files
committed
Merge pull request #90 from alabeduarte/httpclient-resource-fetch-89
Add method fetch for Hyperclient::Resource
2 parents 21552df + a703bfe commit e5b42e7

File tree

4 files changed

+60
-0
lines changed

4 files changed

+60
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
### 0.7.1 (Next)
22

33
* [#87](https://github.com/codegram/hyperclient/pull/87): Fix: eager delegation causes link skipping - [@dblock](https://github.com/dblock).
4+
* [#89](https://github.com/codegram/hyperclient/issues/89): Add method fetch for Hyperclient::Resource - [@alabeduarte](https://github.com/alabeduarte).
45
* Your contribution here.
56

67
### 0.7.0 (February 23, 2015)

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,15 @@ api.splines.each do |spline|
9595
end
9696
```
9797

98+
Other methods, including `[]` and `fetch` are also available
99+
100+
```ruby
101+
api.splines.each do |spline|
102+
puts "A spline with ID #{spline[:uuid]}."
103+
puts "Maybe with reticulated: #{spline.fetch(:reticulated, '-- no reticulated')}"
104+
end
105+
```
106+
98107
### Links and Embedded Resources
99108

100109
The splines example above followed a link called "splines". While you can, you do not need to specify the HAL navigational structure, including links or embedded resources. Hyperclient will resolve these for you. If you prefer, you can explicitly navigate the link structure via `_links`. In the following example the "splines" link leads to a collection of embedded splines. Invoking `api.splines` is equivalent to `api._links.splines._embedded.splines`.

lib/hyperclient/resource.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,18 @@ def [](name)
5757
send(name) if respond_to?(name)
5858
end
5959

60+
def fetch(key, *args)
61+
return self[key] if respond_to?(key)
62+
63+
if args.any?
64+
args.first
65+
elsif block_given?
66+
yield key
67+
else
68+
fail KeyError
69+
end
70+
end
71+
6072
private
6173

6274
# Internal: Returns the self Link of the Resource. Used to handle the HTTP

test/hyperclient/resource_test.rb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,44 @@ module Hyperclient
101101
resource._attributes.expects(:foo).returns('bar')
102102
resource['foo'].must_equal 'bar'
103103
end
104+
105+
describe '#fetch' do
106+
it 'returns the value for keys that exist' do
107+
resource._attributes.expects(:foo).returns('bar')
108+
109+
resource.fetch('foo').must_equal 'bar'
110+
end
111+
112+
it 'raises an error for missing keys' do
113+
proc { resource.fetch('missing key') }.must_raise KeyError
114+
end
115+
116+
describe 'with a default value' do
117+
it 'returns the value for keys that exist' do
118+
resource._attributes.expects(:foo).returns('bar')
119+
resource.fetch('foo', 'default value').must_equal 'bar'
120+
end
121+
122+
it 'returns the default value for missing keys' do
123+
resource.fetch('missing key', 'default value').must_equal 'default value'
124+
end
125+
end
126+
127+
describe 'with a block' do
128+
it 'returns the value for keys that exist' do
129+
resource._attributes.expects(:foo).returns('bar')
130+
resource.fetch('foo') { 'default value' }.must_equal 'bar'
131+
end
132+
133+
it 'returns the value from the block' do
134+
resource.fetch('z') { 'go fish!' }.must_equal 'go fish!'
135+
end
136+
137+
it 'returns the value with args from the block' do
138+
resource.fetch('z') { |el| "go fish, #{el}" }.must_equal 'go fish, z'
139+
end
140+
end
141+
end
104142
end
105143
end
106144

0 commit comments

Comments
 (0)