Skip to content

Commit

Permalink
Sawyer::Agent objects keep track of the root relations
Browse files Browse the repository at this point in the history
  • Loading branch information
technoweenie committed Sep 26, 2012
1 parent 11d5010 commit 0af7deb
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
14 changes: 14 additions & 0 deletions lib/sawyer/agent.rb
Expand Up @@ -15,6 +15,20 @@ def initialize(endpoint, conn = nil)
yield @conn if block_given?
end

# Public: Retains a reference to the root relations of the API.
#
# Returns a Sawyer::Relation::Map.
def rels
@rels ||= root.data.rels
end

# Public: Retains a reference to the root response of the API.
#
# Returns a Sawyer::Response.
def root
@root ||= start
end

# Public: Hits the root of the API to get the initial actions.
#
# Returns a Sawyer::Response.
Expand Down
24 changes: 24 additions & 0 deletions test/agent_test.rb
Expand Up @@ -10,6 +10,30 @@ def setup
end
end

def test_accesses_root_relations
@stubs.get '/a/' do |env|
assert_equal 'foo.com', env[:url].host

[200, {}, Yajl.dump(
:_links => {
:users => {:href => '/users'}})]
end

assert_equal 200, @agent.root.status

assert_equal '/users', @agent.rels[:users].href
assert_equal :get, @agent.rels[:users].method
end

def test_saves_root_endpoint
@stubs.get '/a/' do |env|
[200, {}, '{}']
end

assert_kind_of Sawyer::Response, @agent.root
assert_not_equal @agent.root.time, @agent.start.time
end

def test_starts_a_session
@stubs.get '/a/' do |env|
assert_equal 'foo.com', env[:url].host
Expand Down

0 comments on commit 0af7deb

Please sign in to comment.