Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

* [#298](https://github.com/codegram/hyperclient/pull/300): Upgraded RuboCop to 1.80.2 - [@dblock](https://github.com/dblock).
* [#298](https://github.com/codegram/hyperclient/pull/298): Upgraded RuboCop to 1.63.5 - [@dblock](https://github.com/dblock).
* [#319](https://github.com/codegram/hyperclient/pull/319): Fixed `Resource#to_h` returning `nil` instead of the same result as `#to_hash` - [@dblock](https://github.com/dblock).
* [#320](https://github.com/codegram/hyperclient/pull/320): Documented how to handle non-`hal+json` responses via the Faraday response middleware's `content_type` matcher - [@dblock](https://github.com/dblock).
* [#321](https://github.com/codegram/hyperclient/pull/321): Added Ruby 4.0 to the CI test matrix - [@dblock](https://github.com/dblock).
* Your contribution here.
Expand Down
12 changes: 12 additions & 0 deletions lib/hyperclient/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,18 @@ def [](name)
send(name) if respond_to?(name)
end

# Public: Returns the attributes of the Resource as a Hash.
#
# Delegates explicitly (rather than relying on method_missing) since
# Array also defines #to_h (but not #to_hash), which meant the
# `Array.method_defined?(method)` guard in method_missing let #to_hash
# through but silently swallowed #to_h, returning nil instead of the
# attributes hash.
def to_h
_attributes.to_h
end
alias to_hash to_h

def fetch(key, *args)
return self[key] if respond_to?(key)

Expand Down
10 changes: 10 additions & 0 deletions test/hyperclient/collection_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ module Hyperclient
end
end

describe '#to_h' do
it 'returns the wrapped collection as a hash' do
_(collection.to_h).must_be_kind_of Hash
end

it 'returns the same result as #to_hash' do
_(collection.to_h).must_equal(collection.to_hash)
end
end

describe '#to_s' do
it 'returns the wrapped collection as a hash' do
_(collection.to_s).must_be_kind_of Hash
Expand Down
14 changes: 14 additions & 0 deletions test/hyperclient/resource_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -239,5 +239,19 @@ module Hyperclient
_(resource.inspect).must_include 'attributes:'
end
end

describe '#to_h and #to_hash' do
let(:resource) do
Resource.new({ '_links' => {}, 'title' => 'Order', 'total' => 42 }, entry_point)
end

it 'returns the attributes as a Hash from #to_hash' do
_(resource.to_hash).must_equal('title' => 'Order', 'total' => 42)
end

it 'returns the same result from #to_h as #to_hash' do
_(resource.to_h).must_equal(resource.to_hash)
end
end
end
end
Loading