Skip to content

Commit

Permalink
Swap out Faraday for Hurley
Browse files Browse the repository at this point in the history
  • Loading branch information
pengwynn committed Jan 5, 2015
1 parent 03fca4c commit 87d88ad
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 117 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
source "http://rubygems.org"

gem "hurley", :git => "https://github.com/lostisland/hurley"
gemspec
gem 'rake'
19 changes: 9 additions & 10 deletions lib/sawyer/agent.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require 'faraday'
require 'hurley'
require 'addressable/template'

module Sawyer
Expand Down Expand Up @@ -31,21 +31,20 @@ def self.decode(data)
# options - Hash of options.
# :allow_undefined_methods - Allow relations to call all the HTTP verbs,
# not just the ones defined.
# :faraday - Optional Faraday::Connection to use.
# :hurley - Optional Hurley::Client to use.
# :links_parser - Optional parser to parse link relations
# Defaults: Sawyer::LinkParsers::Hal.new
# :serializer - Optional serializer Class. Defaults to
# self.serializer_class.
#
# Yields the Faraday::Connection if a block is given.
# Yields the Hurley::Client if a block is given.
def initialize(endpoint, options = nil)
@endpoint = endpoint
@conn = (options && options[:faraday]) || Faraday.new
@client = (options && options[:hurley]) || Hurley::Client.new(@endpoint)
@serializer = (options && options[:serializer]) || self.class.serializer
@links_parser = (options && options[:links_parser]) || Sawyer::LinkParsers::Hal.new
@allow_undefined_methods = (options && options[:allow_undefined_methods])
@conn.url_prefix = @endpoint
yield @conn if block_given?
yield @client if block_given?
end

# Public: Retains a reference to the root relations of the API.
Expand All @@ -69,7 +68,7 @@ def start
call :get, @endpoint
end

# Makes a request through Faraday.
# Makes a request through Hurley.
#
# method - The Symbol name of an HTTP method.
# url - The String URL to access. This can be relative to the Agent's
Expand All @@ -91,15 +90,15 @@ def call(method, url, data = nil, options = nil)
options ||= {}
url = expand_url(url, options[:uri])
started = nil
res = @conn.send method, url do |req|
res = @client.send method, url do |req|
if data
req.body = data.is_a?(String) ? data : encode_body(data)
end
if params = options[:query]
req.params.update params
req.query.update params
end
if headers = options[:headers]
req.headers.update headers
req.header.update headers
end
started = Time.now
end
Expand Down
7 changes: 3 additions & 4 deletions lib/sawyer/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@ class Response
# Builds a Response after a completed request.
#
# agent - The Sawyer::Agent that is managing the API connection.
# res - A Faraday::Response.
# res - A Hurley::Response.
def initialize(agent, res, options = {})
@agent = agent
@status = res.status
@headers = res.headers
@env = res.env
@status = res.status_code
@headers = res.header
@data = @headers[:content_type] =~ /json|msgpack/ ? process_data(@agent.decode_body(res.body)) : res.body
@rels = process_rels
@started = options[:sawyer_started]
Expand Down
2 changes: 1 addition & 1 deletion sawyer.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Gem::Specification.new do |spec|
spec.homepage = 'https://github.com/lostisland/sawyer'
spec.licenses = ['MIT']

spec.add_dependency 'faraday', ['~> 0.8', '< 0.10']
spec.add_dependency 'hurley'
spec.add_dependency 'addressable', ['~> 2.3.5']

spec.files = %w(Gemfile LICENSE.md README.md Rakefile)
Expand Down
69 changes: 29 additions & 40 deletions test/agent_test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
require File.expand_path("../helper", __FILE__)

require 'faraday/adapter/test'

module Sawyer
class AgentTest < TestCase

Expand All @@ -15,16 +13,15 @@ def parse(data)
end

def setup
@stubs = Faraday::Adapter::Test::Stubs.new
@agent = Sawyer::Agent.new "http://foo.com/a/" do |conn|
conn.builder.handlers.delete(Faraday::Adapter::NetHttp)
conn.adapter :test, @stubs
@stubs = Hurley::Test.new
@agent = Sawyer::Agent.new "http://foo.com/a/" do |client|
client.connection = @stubs
end
end

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

[200, {'Content-Type' => 'application/json'}, Sawyer::Agent.encode(
:_links => {
Expand All @@ -38,32 +35,28 @@ def test_accesses_root_relations
end

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

[200, {'Content-Type' => 'application/json'}, Sawyer::Agent.encode(
:url => '/',
:users_url => '/users',
:repos_url => '/repos')]
end

agent = Sawyer::Agent.new "http://foo.com/a/" do |conn|
conn.builder.handlers.delete(Faraday::Adapter::NetHttp)
conn.adapter :test, @stubs
end
agent.links_parser = InlineRelsParser.new
@agent.links_parser = InlineRelsParser.new

assert_equal 200, agent.root.status
assert_equal 200, @agent.root.status

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

end

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

Expand All @@ -72,8 +65,8 @@ def test_saves_root_endpoint
end

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

[200, {'Content-Type' => 'application/json'}, Sawyer::Agent.encode(
:_links => {
Expand All @@ -90,10 +83,10 @@ def test_starts_a_session
end

def test_requests_with_body_and_options
@stubs.post '/a/b/c' do |env|
assert_equal '{"a":1}', env[:body]
assert_equal 'abc', env[:request_headers]['x-test']
assert_equal 'foo=bar', env[:url].query
@stubs.post '/a/b/c' do |req|
assert_equal '{"a":1}', req.body
assert_equal 'abc', req.header['x-test']
assert_equal 'foo=bar', req.query.to_s
[200, {}, "{}"]
end

Expand All @@ -104,10 +97,10 @@ def test_requests_with_body_and_options
end

def test_requests_with_body_and_options_to_get
@stubs.get '/a/b/c' do |env|
assert_nil env[:body]
assert_equal 'abc', env[:request_headers]['x-test']
assert_equal 'foo=bar', env[:url].query
@stubs.get '/a/b/c' do |req|
assert_nil req.body
assert_equal 'abc', req.header['x-test']
assert_equal 'foo=bar', req.url.query.to_s
[200, {}, "{}"]
end

Expand Down Expand Up @@ -156,8 +149,8 @@ def test_encodes_and_decodes_times
end

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

[200, {'Content-Type' => 'text/plain'}, "This is plain text"]
end
Expand All @@ -184,17 +177,13 @@ def test_handle_marshal_dump_and_load
end

def test_blank_response_doesnt_raise
@stubs.get "/a/" do |env|
assert_equal "foo.com", env[:url].host
@stubs.get "/a/" do |req|
assert_equal "foo.com", req.url.host
[200, { "Content-Type" => "application/json" }, " "]
end

agent = Sawyer::Agent.new "http://foo.com/a/" do |conn|
conn.adapter :test, @stubs
end

assert_nothing_raised do
assert_equal 200, agent.root.status
assert_equal 200, @agent.root.status
end
end
end
Expand Down
1 change: 1 addition & 0 deletions test/helper.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'test/unit'
require 'hurley/test'
require File.expand_path('../../lib/sawyer', __FILE__)

class Sawyer::TestCase < Test::Unit::TestCase
Expand Down
74 changes: 36 additions & 38 deletions test/relation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,15 @@ def test_builds_rels_from_nil
end

def test_relation_api_calls
agent = Sawyer::Agent.new "http://foo.com/a/" do |conn|
conn.builder.handlers.delete(Faraday::Adapter::NetHttp)
conn.adapter :test do |stubs|
stubs.get '/a/1' do
[200, {}, '{}']
end
stubs.delete '/a/1' do
[204, {}, '{}']
end
end
stubs = Hurley::Test.new
agent = Sawyer::Agent.new "http://foo.com/a/" do |client|
client.connection = stubs
end
stubs.get '/a/1' do
[200, {}, '{}']
end
stubs.delete '/a/1' do
[204, {}, '{}']
end

rel = Sawyer::Relation.new agent, :self, "/a/1", "get,put,delete"
Expand All @@ -104,18 +103,17 @@ def test_relation_api_calls
end

def test_relation_api_calls_with_uri_tempate
agent = Sawyer::Agent.new "http://foo.com/a" do |conn|
conn.builder.handlers.delete(Faraday::Adapter::NetHttp)
conn.adapter :test do |stubs|
stubs.get '/octocat/hello' do |env|
assert_equal "a=1&b=2", env[:url].query
[200, {}, '{}']
end

stubs.get '/a' do
[404, {}, '{}']
end
end
stubs = Hurley::Test.new
agent = Sawyer::Agent.new "http://foo.com/a/" do |client|
client.connection = stubs
end
stubs.get '/octocat/hello' do |req|
assert_equal "a=1&b=2", req.query.to_s
[200, {}, '{}']
end

stubs.get '/a' do
[404, {}, '{}']
end

rel = Sawyer::Relation.new agent, :repo, "{/user,repo}{?a,b}"
Expand All @@ -136,24 +134,24 @@ def test_handles_invalid_uri
end

def test_allows_all_methods_when_not_in_strict_mode
stubs = Hurley::Test.new
stubs.get '/a/1' do
[200, {}, '{}']
end
stubs.delete '/a/1' do
[204, {}, '{}']
end
stubs.post '/a/1' do
[200, {}, '{}']
end
stubs.put '/a/1' do
[204, {}, '{}']
end

agent = Sawyer::Agent.new "http://foo.com/a/", :allow_undefined_methods => true do |conn|
conn.builder.handlers.delete(Faraday::Adapter::NetHttp)
conn.adapter :test do |stubs|
stubs.get '/a/1' do
[200, {}, '{}']
end
stubs.delete '/a/1' do
[204, {}, '{}']
end
stubs.post '/a/1' do
[200, {}, '{}']
end
stubs.put '/a/1' do
[204, {}, '{}']
end
end
agent = Sawyer::Agent.new "http://foo.com/a/" do |client|
client.connection = stubs
end
agent.allow_undefined_methods = true

rel = Sawyer::Relation.new agent, :self, "/a/1"
assert_equal 200, rel.get.status
Expand Down
7 changes: 3 additions & 4 deletions test/resource_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ module Sawyer
class ResourceTest < TestCase

def setup
@stubs = Faraday::Adapter::Test::Stubs.new
@agent = Sawyer::Agent.new "http://foo.com/a/" do |conn|
conn.builder.handlers.delete(Faraday::Adapter::NetHttp)
conn.adapter :test, @stubs
@stubs = Hurley::Test.new
@agent = Sawyer::Agent.new "http://foo.com/a/" do |client|
client.connection = @stubs
end
end

Expand Down
Loading

0 comments on commit 87d88ad

Please sign in to comment.