Class: Patron::Request
- Inherits:
-
Object
- Object
- Patron::Request
- Defined in:
- lib/patron/request.rb,
ext/patron/session_ext.c
Overview
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.
Constant Summary collapse
- VALID_ACTIONS =
%w[GET PUT POST DELETE HEAD COPY]
- 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, :automatic_content_encoding, :force_ipv4 ]
- WRITER_VARS =
[ :url, :username, :password, :file_name, :proxy, :proxy_type, :insecure, :ignore_content_length, :multipart, :cacert, :ssl_version, :automatic_content_encoding, :force_ipv4 ]
- AuthBasic =
INT2FIX(CURLAUTH_BASIC)
- AuthDigest =
INT2FIX(CURLAUTH_DIGEST)
- AuthAny =
INT2FIX(CURLAUTH_ANY)
Instance Method Summary collapse
- #action=(action) ⇒ Object
- #action_name ⇒ Object
-
#auth_type=(type = :basic) ⇒ Object
Set the type of authentication to use for this request.
- #buffer_size=(buffer_size) ⇒ Object
- #connect_timeout=(new_timeout) ⇒ Object
- #credentials ⇒ Object
- #eql?(request) ⇒ Boolean (also: #==)
- #headers=(new_headers) ⇒ Object
-
#initialize ⇒ Request
constructor
A new instance of Request.
- #marshal_dump ⇒ Object
- #marshal_load(data) ⇒ Object
- #max_redirects=(new_max_redirects) ⇒ Object
- #timeout=(new_timeout) ⇒ Object
- #upload_data=(data) ⇒ Object
Constructor Details
#initialize ⇒ Request
Returns a new instance of Request.
37 38 39 40 41 42 43 |
# File 'lib/patron/request.rb', line 37 def initialize @action = :get @headers = {} @timeout = 0 @connect_timeout = 0 @max_redirects = -1 end |
Instance Method Details
#action=(action) ⇒ Object
91 92 93 94 95 96 |
# File 'lib/patron/request.rb', line 91 def action=(action) if !VALID_ACTIONS.include?(action.to_s.upcase) raise ArgumentError, "Action must be one of #{VALID_ACTIONS.join(', ')}" end @action = action.downcase.to_sym end |
#action_name ⇒ Object
143 144 145 |
# File 'lib/patron/request.rb', line 143 def action_name @action.to_s.upcase end |
#auth_type=(type = :basic) ⇒ Object
Set the type of authentication to use for this request.
69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/patron/request.rb', line 69 def auth_type=(type=:basic) @auth_type = case type when :basic, "basic" Request::AuthBasic when :digest, "digest" Request::AuthDigest when :any, "any" Request::AuthAny else raise "#{type.inspect} is an unknown authentication type" end end |
#buffer_size=(buffer_size) ⇒ Object
130 131 132 133 134 135 136 |
# File 'lib/patron/request.rb', line 130 def buffer_size=(buffer_size) if buffer_size != nil && buffer_size.to_i < 1 raise ArgumentError, "Buffer size must be a positive integer greater than 0 or nil" end @buffer_size = buffer_size != nil ? buffer_size.to_i : nil end |
#connect_timeout=(new_timeout) ⇒ Object
106 107 108 109 110 111 112 |
# File 'lib/patron/request.rb', line 106 def connect_timeout=(new_timeout) if new_timeout && new_timeout.to_i < 1 raise ArgumentError, "Timeout must be a positive integer greater than 0" end @connect_timeout = new_timeout.to_i end |
#credentials ⇒ Object
138 139 140 141 |
# File 'lib/patron/request.rb', line 138 def credentials return nil if username.nil? || password.nil? "#{username}:#{password}" end |
#eql?(request) ⇒ Boolean Also known as: ==
147 148 149 150 151 152 153 |
# File 'lib/patron/request.rb', line 147 def eql?(request) return false unless Request === request READER_VARS.inject(true) do |memo, name| memo && (self.send(name) == request.send(name)) end end |
#headers=(new_headers) ⇒ Object
122 123 124 125 126 127 128 |
# File 'lib/patron/request.rb', line 122 def headers=(new_headers) if !new_headers.kind_of?(Hash) raise ArgumentError, "Headers must be a hash" end @headers = new_headers end |
#marshal_dump ⇒ Object
157 158 159 160 161 |
# File 'lib/patron/request.rb', line 157 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 |
#marshal_load(data) ⇒ Object
163 164 165 166 167 |
# File 'lib/patron/request.rb', line 163 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 |
#max_redirects=(new_max_redirects) ⇒ Object
114 115 116 117 118 119 120 |
# File 'lib/patron/request.rb', line 114 def max_redirects=(new_max_redirects) if new_max_redirects.to_i < -1 raise ArgumentError, "Max redirects must be a positive integer, 0 or -1" end @max_redirects = new_max_redirects.to_i end |
#timeout=(new_timeout) ⇒ Object
98 99 100 101 102 103 104 |
# File 'lib/patron/request.rb', line 98 def timeout=(new_timeout) if new_timeout && new_timeout.to_i < 1 raise ArgumentError, "Timeout must be a positive integer greater than 0" end @timeout = new_timeout.to_i end |
#upload_data=(data) ⇒ Object
82 83 84 85 86 87 88 89 |
# File 'lib/patron/request.rb', line 82 def upload_data=(data) @upload_data = case data when Hash self.multipart ? data : Util.build_query_string_from_hash(data, action == :post) else data end end |