lib/httpx/request.rb in httpx-0.3.1 vs lib/httpx/request.rb in httpx-0.4.0
- old
+ new
@@ -3,10 +3,11 @@
require "forwardable"
module HTTPX
class Request
extend Forwardable
+ include Callbacks
using URIExtensions
METHODS = [
# RFC 2616: Hypertext Transfer Protocol -- HTTP/1.1
:options, :get, :head, :post, :put, :delete, :trace, :connect,
@@ -32,10 +33,12 @@
USER_AGENT = "httpx.rb/#{VERSION}"
attr_reader :verb, :uri, :headers, :body, :state
+ attr_reader :options
+
attr_accessor :response
def_delegator :@body, :<<
def_delegator :@body, :empty?
@@ -82,20 +85,22 @@
@uri.origin
end
def query
return @query if defined?(@query)
+
query = []
if (q = @options.params)
query << URI.encode_www_form(q)
end
query << @uri.query if @uri.query
@query = query.join("&")
end
def drain_body
return nil if @body.nil?
+
@drainer ||= @body.each
chunk = @drainer.next
chunk.dup
rescue StopIteration
nil
@@ -107,10 +112,11 @@
class Body
class << self
def new(*, options)
return options.body if options.body.is_a?(self)
+
super
end
end
def initialize(headers, options)
@@ -121,17 +127,19 @@
Transcoder.registry("form").encode(options.form)
elsif options.json
Transcoder.registry("json").encode(options.json)
end
return if @body.nil?
+
@headers["content-type"] ||= @body.content_type
@headers["content-length"] = @body.bytesize unless unbounded_body?
end
def each(&block)
return enum_for(__method__) unless block_given?
return if @body.nil?
+
body = stream(@body)
if body.respond_to?(:read)
::IO.copy_stream(body, ProcIO.new(block))
elsif body.respond_to?(:each)
body.each(&block)
@@ -141,14 +149,16 @@
end
def empty?
return true if @body.nil?
return false if chunked?
+
bytesize.zero?
end
def bytesize
return 0 if @body.nil?
+
if @body.respond_to?(:bytesize)
@body.bytesize
elsif @body.respond_to?(:size)
@body.size
else