lib/httpi/request.rb in httpi-2.4.1 vs lib/httpi/request.rb in httpi-2.4.2

- old
+ new

@@ -9,11 +9,11 @@ # # Represents an HTTP request and contains various methods for customizing that request. class Request # Available attribute writers. - ATTRIBUTES = [:url, :proxy, :headers, :body, :open_timeout, :read_timeout, :follow_redirect, :query] + ATTRIBUTES = [:url, :proxy, :headers, :body, :open_timeout, :read_timeout, :follow_redirect, :redirect_limit, :query] # Accepts a Hash of +args+ to mass assign attributes and authentication credentials. def initialize(args = {}) if args.kind_of? String self.url = args @@ -54,12 +54,11 @@ # Returns the +proxy+ to use. attr_reader :proxy # Returns whether to use SSL. def ssl? - return @ssl unless @ssl.nil? - !!(url.to_s =~ /^https/) + @ssl ||= !!(url.to_s =~ /^https/) end # Sets whether to use SSL. attr_writer :ssl @@ -100,10 +99,11 @@ end # Sets the block to be called while processing the response. The block # accepts a single parameter - the chunked response body. def on_body(&block) + @on_body ||= nil if block_given? then @on_body = block end @on_body end @@ -128,19 +128,26 @@ # Returns whether or not redirects should be followed - defaults to false if not set. def follow_redirect? @follow_redirect ||= false end + attr_writer :redirect_limit + + # Returns how many redirects should be followed - defaults to 3 if not set. + def redirect_limit + @redirect_limit ||= 3 + end + private # Stores the cookies from past requests. def cookie_store @cookie_store ||= CookieStore.new end # Expects a +url+, validates its validity and returns a +URI+ object. def normalize_url!(url) - raise ArgumentError, "Invalid URL: #{url}" unless url.to_s =~ /^http/ + raise ArgumentError, "Invalid URL: #{url}" unless url.to_s =~ /^http|socks/ url.kind_of?(URI) ? url : URI(url) end # Returns a +query+ string given a +Hash+ def build_query_from_hash(query)