Merged from rick.

This commit is contained in:
Zack Hobson 2010-01-05 11:23:10 -08:00
commit 5075a3e3e3
9 changed files with 112 additions and 24 deletions

View File

@ -1 +1 @@
0.0.2
0.1.0

View File

@ -5,11 +5,11 @@
Gem::Specification.new do |s|
s.name = %q{faraday}
s.version = "0.0.2"
s.version = "0.1.0"
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["rick"]
s.date = %q{2009-12-31}
s.date = %q{2010-01-02}
s.description = %q{HTTP/REST API client library with pluggable components}
s.email = %q{technoweenie@gmail.com}
s.extra_rdoc_files = [

View File

@ -58,7 +58,11 @@ module Faraday
end
def initialize &block
super
super nil
configure(&block) if block
end
def configure
yield stubs
end

View File

@ -8,7 +8,7 @@ module Faraday
def _perform(method, uri, data, request_headers)
http = Net::HTTP.new(uri.host, uri.port)
response_class.new do |resp|
http_resp = http.send_request(method, uri.path, data, request_headers)
http_resp = http.send_request(method, path_for(uri), data, request_headers)
raise Faraday::Error::ResourceNotFound if http_resp.code == '404'
resp.process http_resp.body
http_resp.each_header do |key, value|

View File

@ -12,12 +12,21 @@ module Faraday
include Addressable
attr_accessor :host, :port, :scheme
attr_accessor :host, :port, :scheme, :params, :headers
attr_reader :path_prefix
def initialize(url = nil)
@response_class = nil
@request_class = nil
# :url
# :params
# :headers
# :response
def initialize(url = nil, options = {})
if url.is_a?(Hash)
options = url
url = options[:url]
end
@response_class = options[:response]
@params = options[:params] || {}
@headers = options[:headers] || {}
self.url_prefix = url if url
end
@ -29,14 +38,6 @@ module Faraday
self.path_prefix = uri.path
end
# Override in a subclass, or include an adapter
#
# def _get(uri, headers)
# end
#
def get(uri, params = {}, headers = {})
_get build_uri(uri, params), headers
end
# Override in a subclass, or include an adapter
#
@ -44,7 +45,7 @@ module Faraday
# end
#
def post(uri, params = {}, headers = {})
_post build_uri(uri), params, headers
_post build_uri(uri), build_params(params), build_headers(headers)
end
# Override in a subclass, or include an adapter
@ -53,11 +54,26 @@ module Faraday
# end
#
def put(uri, params = {}, headers = {})
_put build_uri(uri), params, headers
_put build_uri(uri), build_params(params), build_headers(headers)
end
# Override in a subclass, or include an adapter
#
# def _delete(uri, headers)
# end
#
def delete(uri, params = {}, headers = {})
_delete build_uri(uri, params), headers
_delete build_uri(uri, build_params(params)), build_headers(headers)
end
# Override in a subclass, or include an adapter
#
# def _get(uri, headers)
# end
#
def get(url, params = nil, headers = nil)
uri = build_uri(url, build_params(params))
_get(uri, build_headers(headers))
end
def request_class
@ -106,7 +122,7 @@ module Faraday
@path_prefix = value
end
def build_uri(url, params = {})
def build_uri(url, params = nil)
uri = URI.parse(url)
uri.scheme ||= @scheme
uri.host ||= @host
@ -114,11 +130,35 @@ module Faraday
if @path_prefix && uri.path !~ /^\//
uri.path = "#{@path_prefix.size > 1 ? @path_prefix : nil}/#{uri.path}"
end
query = params_to_query(params)
if !query.empty? then uri.query = query end
if params && !params.empty?
uri.query = params_to_query(params)
end
uri
end
def path_for(uri)
uri.path.tap do |s|
s << "?#{uri.query}" if uri.query
s << "##{uri.fragment}" if uri.fragment
end
end
def build_params(existing)
build_hash :params, existing
end
def build_headers(existing)
build_hash(:headers, existing).tap do |headers|
headers.keys.each do |key|
headers[key] = headers.delete(key).to_s
end
end
end
def build_hash(method, existing)
existing ? send(method).merge(existing) : send(method)
end
def params_to_query(params)
params.inject([]) do |memo, (key, val)|
memo << "#{escape_for_querystring(key)}=#{escape_for_querystring(val)}"
@ -128,7 +168,7 @@ module Faraday
# Some servers convert +'s in URL query params to spaces.
# Go ahead and encode it.
def escape_for_querystring(s)
URI.encode_component(s, Addressable::URI::CharacterClasses::QUERY).tap do |escaped|
URI.encode_component(s.to_s, Addressable::URI::CharacterClasses::QUERY).tap do |escaped|
escaped.gsub! /\+/, "%2B"
end
end

View File

@ -23,6 +23,20 @@ class AdapterTest < Faraday::TestCase
end
end
it "passes params" do
@connection.params = {:a => 1}
assert_equal "params[:a] == 1", @connection.get('params').body
end
it "passes headers" do
@connection.headers = {"X-Test" => 1}
assert_equal "env[HTTP_X_TEST] == 1", @connection.get('headers').body
end
it "retrieves the response body" do
assert_equal 'hello world', @connection.get('hello_world').body
end
describe "#put" do
it "sends params" do
assert_equal 'hello zack', @connection.put('hello', 'name' => 'zack').body

View File

@ -31,6 +31,21 @@ class ConnectionTest < Faraday::TestCase
conn = Faraday::Connection.new "http://sushi.com/fish"
assert_equal '/fish', conn.path_prefix
end
it "parses @path_prefix out of given url option" do
conn = Faraday::Connection.new :url => "http://sushi.com/fish"
assert_equal '/fish', conn.path_prefix
end
it "stores default params from options" do
conn = Faraday::Connection.new :params => {:a => 1}
assert_equal 1, conn.params[:a]
end
it "stores default headers from options" do
conn = Faraday::Connection.new :headers => {:a => 1}
assert_equal 1, conn.headers[:a]
end
end
describe "#build_uri" do

View File

@ -9,6 +9,12 @@ $LOAD_PATH.unshift(File.dirname(__FILE__))
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
require 'faraday'
begin
require 'ruby-debug'
Debugger.start
rescue LoadError
end
module Faraday
class TestCase < Test::Unit::TestCase
LIVE_SERVER = 'http://localhost:4567'

View File

@ -36,3 +36,12 @@ end
delete '/delete_with_params' do
params[:deleted]
end
get '/params' do
%(params[:a] == #{params[:a]})
end
get "/headers" do
%(env[HTTP_X_TEST] == #{env["HTTP_X_TEST"]})
end