Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support #to_h and #to_hash on the Hyperclient::Resource #104

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
3 changes: 2 additions & 1 deletion lib/hyperclient/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,10 @@ def _self_link
def method_missing(method, *args, &block)
if args.any? && args.first.is_a?(Hash)
_links.send(method, [], &block)._expand(*args)
elsif !Array.method_defined?(method)
else
[:_attributes, :_embedded, :_links].each do |target|
target = send(target)
puts target.inspect
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stray puts

return target.send(method, *args, &block) if target.respond_to?(method.to_s)
end
super
Expand Down
6 changes: 6 additions & 0 deletions test/hyperclient/collection_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ 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
end

describe 'include?' do
it 'returns true for keys that exist' do
collection.include?('_links').must_equal true
Expand Down
12 changes: 12 additions & 0 deletions test/hyperclient/resource_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,18 @@ module Hyperclient
resource['foo'].must_equal 'bar'
end

describe '#to_h' do
it 'returns the resource attributes as a hash' do
resource.to_h.must_be_kind_of Hash
end
end

describe '#to_hash' do
it 'returns the resource attributes as a hash' do
resource.to_hash.must_be_kind_of Hash
end
end

describe '#fetch' do
it 'returns the value for keys that exist' do
resource._attributes.expects(:foo).returns('bar')
Expand Down