lib/patron/request.rb in patron-0.4.18 vs lib/patron/request.rb in patron-0.4.20
- old
+ new
@@ -30,24 +30,35 @@
# Represents the information necessary for an HTTP request.
# This is basically a data object with validation. Not all fields will be
# used in every request.
class Request
- VALID_ACTIONS = [:get, :put, :post, :delete, :head, :copy]
+ VALID_ACTIONS = %w[GET PUT POST DELETE HEAD COPY]
def initialize
@action = :get
@headers = {}
@timeout = 0
@connect_timeout = 0
@max_redirects = -1
end
- attr_accessor :url, :username, :password, :file_name, :proxy, :proxy_type, :auth_type, :insecure, :ignore_content_length, :multipart
- attr_reader :action, :timeout, :connect_timeout, :max_redirects, :headers, :buffer_size
- attr_reader :auth_type
+ READER_VARS = [
+ :url, :username, :password, :file_name, :proxy, :proxy_type, :insecure,
+ :ignore_content_length, :multipart, :action, :timeout, :connect_timeout,
+ :max_redirects, :headers, :auth_type, :upload_data, :buffer_size, :cacert,
+ :ssl_version
+ ]
+ WRITER_VARS = [
+ :url, :username, :password, :file_name, :proxy, :proxy_type, :insecure,
+ :ignore_content_length, :multipart, :cacert, :ssl_version
+ ]
+
+ attr_reader *READER_VARS
+ attr_writer *WRITER_VARS
+
# Set the type of authentication to use for this request.
#
# @param [String, Symbol] type - The type of authentication to use for this request, can be one of
# :basic, :digest, or :any
#
@@ -69,26 +80,21 @@
end
def upload_data=(data)
@upload_data = case data
when Hash
- self.multipart ? data : Util.build_query_string_from_hash(data, @action == :post)
+ self.multipart ? data : Util.build_query_string_from_hash(data, action == :post)
else
data
end
end
- def upload_data
- @upload_data
- end
-
- def action=(new_action)
- if !VALID_ACTIONS.include?(new_action)
+ def action=(action)
+ if !VALID_ACTIONS.include?(action.to_s.upcase)
raise ArgumentError, "Action must be one of #{VALID_ACTIONS.join(', ')}"
end
-
- @action = new_action
+ @action = action.downcase.to_sym
end
def timeout=(new_timeout)
if new_timeout && new_timeout.to_i < 1
raise ArgumentError, "Timeout must be a positive integer greater than 0"
@@ -127,16 +133,38 @@
end
@buffer_size = buffer_size != nil ? buffer_size.to_i : nil
end
+ def credentials
+ return nil if username.nil? || password.nil?
+ "#{username}:#{password}"
+ end
+
def action_name
@action.to_s.upcase
end
- def credentials
- return nil if username.nil? || password.nil?
- "#{username}:#{password}"
+ def eql?(request)
+ return false unless Request === request
+
+ READER_VARS.inject(true) do |memo, name|
+ memo && (self.send(name) == request.send(name))
+ end
+ end
+
+ alias_method :==, :eql?
+
+ def marshal_dump
+ [ @url, @username, @password, @file_name, @proxy, @proxy_type, @insecure,
+ @ignore_content_length, @multipart, @action, @timeout, @connect_timeout,
+ @max_redirects, @headers, @auth_type, @upload_data, @buffer_size, @cacert ]
+ end
+
+ def marshal_load(data)
+ @url, @username, @password, @file_name, @proxy, @proxy_type, @insecure,
+ @ignore_content_length, @multipart, @action, @timeout, @connect_timeout,
+ @max_redirects, @headers, @auth_type, @upload_data, @buffer_size, @cacert = data
end
end
end