lib/faraday/connection.rb in faraday-0.0.2 vs lib/faraday/connection.rb in faraday-0.1.0
- old
+ new
@@ -10,31 +10,44 @@
alias supports_async? supports_async
end
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
- if url
- uri = URI.parse(url)
- self.scheme = uri.scheme
- self.host = uri.host
- self.port = uri.port
- self.path_prefix = uri.path
+ # :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
+ def url_prefix=(url)
+ uri = URI.parse(url)
+ self.scheme = uri.scheme
+ self.host = uri.host
+ self.port = uri.port
+ self.path_prefix = uri.path
+ end
+
# Override in a subclass, or include an adapter
#
# def _get(uri, headers)
# end
#
- def get(url, params = {}, headers = {})
- _get(build_uri(url, params), headers)
+ def get(url, params = nil, headers = nil)
+ uri = build_uri(url, build_params(params))
+ _get(uri, build_headers(headers))
end
def response_class
@response_class || Response
end
@@ -47,20 +60,20 @@
end
def in_parallel?
!!@parallel_manager
end
-
+
def in_parallel(options = {})
@parallel_manager = true
yield
@parallel_manager = false
end
-
+
def setup_parallel_manager(options = {})
end
-
+
def run_parallel_requests
end
def path_prefix=(value)
if value
@@ -68,32 +81,56 @@
value.replace "/#{value}" if value !~ /^\//
end
@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
uri.port ||= @port
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)}"
end.join("&")
end
# 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
end
end