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

Make response available from Resource. #46

Merged
merged 4 commits into from
Nov 22, 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
10 changes: 9 additions & 1 deletion lib/hyperclient/link.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,15 @@ def hreflang

# Public: Returns the Resource which the Link is pointing to.
def resource
@resource ||=Resource.new(get.body, @entry_point)
@resource ||= begin
response = get

if response.success?
Resource.new(response.body, @entry_point, response)
else
Resource.new(nil, @entry_point, response)
end
end
end

def connection
Expand Down
16 changes: 15 additions & 1 deletion lib/hyperclient/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ class Resource
# ResourceCollection.
attr_reader :embedded

# Public: Returns the response object for the HTTP request that created this
# resource, if one exists.
attr_reader :response

# Public: Delegate all HTTP methods (get, post, put, delete, options and
# head) to its self link.
def_delegators :self_link, :get, :post, :put, :delete, :options, :head
Expand All @@ -26,17 +30,27 @@ class Resource
#
# representation - The hash with the HAL representation of the Resource.
# entry_point - The EntryPoint object to inject the configutation.
def initialize(representation, entry_point)
def initialize(representation, entry_point, response=nil)
representation ||= {}
@links = LinkCollection.new(representation['_links'], entry_point)
@embedded = ResourceCollection.new(representation['_embedded'], entry_point)
@attributes = Attributes.new(representation)
@entry_point = entry_point
@response = response
end

def inspect
"#<#{self.class.name} self_link:#{self_link.inspect} attributes:#{@attributes.inspect}>"
end

def success?
response && response.success?
end

def status
response && response.status
end

private
# Internal: Returns the self Link of the Resource. Used to handle the HTTP
# methods.
Expand Down
17 changes: 15 additions & 2 deletions test/hyperclient/link_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,23 @@ module Hyperclient

describe 'resource' do
it 'builds a resource with the link href representation' do
Resource.expects(:new).with({}, entry_point)
mock_response = mock(body: {}, success?: true)

Resource.expects(:new).with({}, entry_point, mock_response)

link = Link.new({'href' => '/'}, entry_point)
link.expects(:get).returns(mock_response)

link.resource
end

it "has an empty body when the response fails" do
mock_response = mock(success?: false)

Resource.expects(:new).with(nil, entry_point, mock_response)

link = Link.new({'href' => '/'}, entry_point)
link.expects(:get).returns(mock(body: {}))
link.expects(:get).returns(mock_response)

link.resource
end
Expand Down
60 changes: 60 additions & 0 deletions test/hyperclient/resource_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ module Hyperclient

Resource.new({'_embedded' => {"orders" => [] }}, entry_point)
end

it "initializes the response" do
mock_response = mock(body: {})

resource = Resource.new(mock_response.body, entry_point, mock_response)

resource.response.must_equal mock_response
end
end

describe 'accessors' do
Expand Down Expand Up @@ -58,5 +66,57 @@ module Hyperclient

resource.get
end

describe ".success?" do
describe "with a response object" do
let(:resource) do
Resource.new({}, entry_point, mock_response)
end

let(:mock_response) do
mock(success?: true)
end

it "proxies to the response object" do
resource.success?.must_equal true
end
end

describe "without a response object" do
let(:resource) do
Resource.new({}, entry_point)
end

it "returns nil" do
resource.success?.must_be_nil
end
end
end

describe ".status" do
describe "with a response object" do
let(:resource) do
Resource.new({}, entry_point, mock_response)
end

let(:mock_response) do
mock(status: 200)
end

it "proxies to the response object" do
resource.status.must_equal 200
end
end

describe "without a response object" do
let(:resource) do
Resource.new({}, entry_point)
end

it "returns nil" do
resource.status.must_be_nil
end
end
end
end
end