lib/httpi/request.rb in httpi-0.5.0 vs lib/httpi/request.rb in httpi-0.6.0

- old
+ new

@@ -1,25 +1,25 @@ require "uri" +require "httpi/auth/config" module HTTPI # = HTTPI::Request # # Represents an HTTP request and contains various methods for customizing that request. class Request - # Request accessor methods. - ACCESSORS = [:url, :proxy, :headers, :body, :open_timeout, :read_timeout] + # Available attribute writers. + ATTRIBUTES = [:url, :proxy, :headers, :body, :open_timeout, :read_timeout] - # Request authentication methods. - AUTHENTICATION = [:basic_auth, :digest_auth] - - # Accepts a Hash of +options+ which may contain any number of ACCESSORS and/or - # AUTHENTICATION credentials to set. - def initialize(options = {}) - assign_accessors options - assign_authentication options + # Accepts a Hash of +args+ to mass assign attributes and authentication credentials. + def initialize(args = {}) + if args.kind_of? String + self.url = args + elsif args.kind_of?(Hash) && !args.empty? + mass_assign args + end end # Sets the +url+ to access. Raises an +ArgumentError+ unless the +url+ is valid. def url=(url) @url = normalize_url! url @@ -34,10 +34,19 @@ end # Returns the +proxy+ to use. attr_reader :proxy + # Returns whether to use SSL. + def ssl? + return @ssl unless @ssl.nil? + !!(url.to_s =~ /^https/) + end + + # Sets whether to use SSL. + attr_writer :ssl + # Returns a Hash of HTTP headers. Defaults to return an empty Hash. def headers @headers ||= {} end @@ -47,57 +56,32 @@ # Adds a header information to accept gzipped content. def gzip headers["Accept-Encoding"] = "gzip,deflate" end - attr_accessor :body, :open_timeout, :read_timeout, :auth_type + attr_accessor :body, :open_timeout, :read_timeout + # Returns the <tt>HTTPI::Authentication</tt> object. + def auth + @auth ||= Auth::Config.new + end + # Returns whether any authentication credentials were specified. def auth? - !!auth_type + !!auth.type end - # Shortcut method for returning the credentials for the authentication specified. - # Return +nil+ unless any authentication credentials were specified. - def credentials - return unless auth? - send "#{auth_type}_auth" + # Expects a Hash of +args+ to assign. + def mass_assign(args) + ATTRIBUTES.each { |key| send("#{key}=", args[key]) if args[key] } end - - # Sets the HTTP basic auth credentials. Accepts an Array or two arguments for the - # +username+ and +password+. Resets the credentials when +nil+ is passed and returns - # an Array of credentials when no +args+ where given. - def basic_auth(*args) - self.auth_type = :basic - @basic_auth = extract_credentials @basic_auth, args.flatten - end - - # Sets the HTTP digest auth credentials. Accepts an Array or two arguments for the - # +username+ and +password+. Resets the credentials when +nil+ is passed and returns - # an Array of credentials when no +args+ where given. - def digest_auth(*args) - self.auth_type = :digest - @digest_auth = extract_credentials @digest_auth, args.flatten - end - - private - - def assign_accessors(options) - ACCESSORS.each { |a| send("#{a}=", options[a]) if options[a] } - end - def assign_authentication(options) - AUTHENTICATION.each { |c| send(c, options[c]) if options[c] } - end - + private + + # 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/ url.kind_of?(URI) ? url : URI(url) - end - - def extract_credentials(credentials, args) - return unless args.empty? || args.first - args[1] ? args[0, 2] : credentials end end end