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

Add support for arrays of Links #27

Merged
merged 1 commit into from
Apr 3, 2013
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
21 changes: 16 additions & 5 deletions lib/hyperclient/link_collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,24 @@ class LinkCollection < Collection
# Public: Initializes a LinkCollection.
#
# collection - The Hash with the links.
# entry_point - The EntryPoint object to inject the cofnigutation.
# entry_point - The EntryPoint object to inject the configuration.
Copy link
Contributor

Choose a reason for hiding this comment

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

LOL, @oriolgual's typing abilities are shown here :trollface:

Thanks! :)

Copy link
Member

Choose a reason for hiding this comment

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

It was an easter egg :P

#
def initialize(collection, entry_point)
raise "Invalid response for LinkCollection. The response was: #{collection.inspect}" if collection && !collection.respond_to?(:inject)
@collection = (collection || {}).inject({}) do |hash, (name, link)|
hash.update(name => Link.new(link, entry_point))
end
raise "Invalid response for LinkCollection. The response was: #{collection.inspect}" if collection && !collection.respond_to?(:collect)

@collection = Hash[
(collection || {}).collect do |name, link_or_links|
value = if link_or_links.class == Array
link_or_links.collect do |link|
Link.new(link, entry_point)
end
else
Link.new(link_or_links, entry_point)
end

[name, value]
end
]
end
end
end
10 changes: 9 additions & 1 deletion test/fixtures/element.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,15 @@
"filter": {
"href": "/productions/1?categories={filter}",
"templated": true
}
},
"gizmos": [
{
"href": "/gizmos/1"
},
{
"href": "/gizmos/2"
}
]
},
"title": "Real World ASP.NET MVC3",
"description": "In this advanced, somewhat-opinionated production you'll get your very own startup off the ground using ASP.NET MVC 3...",
Expand Down
18 changes: 18 additions & 0 deletions test/hyperclient/link_collection_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,29 @@ module Hyperclient

it 'initializes the collection with links' do
links.must_respond_to :filter
links.must_respond_to :gizmos
end

it 'returns link objects for each link' do
links.filter.must_be_kind_of Link
links['self'].must_be_kind_of Link

links.gizmos.must_be_kind_of Array
links['gizmos'].must_be_kind_of Array
end

describe "array of links" do
let(:gizmos) { links.gizmos }

it 'should have 2 items' do
gizmos.length.must_equal 2
end

it 'must be an array of Links' do
gizmos.each do |link|
link.must_be_kind_of Link
end
end
end
end
end