lib/httpi/request.rb in httpi-0.3.0 vs lib/httpi/request.rb in httpi-0.4.0
- old
+ new
@@ -9,11 +9,11 @@
# Request accessor methods.
ACCESSORS = [:url, :proxy, :headers, :body, :open_timeout, :read_timeout]
# Request authentication methods.
- AUTHENTICATION = [:basic_auth]
+ 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
@@ -42,16 +42,37 @@
end
# Sets the Hash of HTTP headers.
attr_writer :headers
- attr_accessor :body, :open_timeout, :read_timeout
+ attr_accessor :body, :open_timeout, :read_timeout, :auth_type
+ # Returns whether any authentication credentials were specified.
+ def auth?
+ !!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"
+ 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)