Class: Patron::Request

Inherits:
Object
  • Object
show all
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 =

Contains the valid HTTP verbs that can be used to perform requests

%w[GET PUT POST DELETE HEAD COPY PATCH]
READER_VARS =
[
  :url, :username, :password, :file_name, :proxy, :proxy_type, :insecure,
  :ignore_content_length, :multipart, :action, :timeout, :connect_timeout, :dns_cache_timeout,
  :max_redirects, :headers, :auth_type, :upload_data, :buffer_size, :cacert,
  :ssl_version, :http_version, :automatic_content_encoding, :force_ipv4, :download_byte_limit,
  :low_speed_time, :low_speed_limit, :progress_callback
]
WRITER_VARS =
[
  :url, :username, :password, :file_name, :proxy, :proxy_type, :insecure, :dns_cache_timeout,
  :ignore_content_length, :multipart, :cacert, :ssl_version, :http_version, :automatic_content_encoding, :force_ipv4, :download_byte_limit,
  :low_speed_time, :low_speed_limit, :progress_callback
]
AuthBasic =
LONG2NUM(CURLAUTH_BASIC)
AuthDigest =
LONG2NUM(CURLAUTH_DIGEST)
AuthAny =
LONG2NUM(CURLAUTH_ANY)

Instance Method Summary collapse

Constructor Details

#initializeRequest

Initializes a new Request, which defaults to the GET HTTP verb and has it's timeouts set to 0



15
16
17
18
19
20
21
# File 'lib/patron/request.rb', line 15

def initialize
  @action = :get
  @headers = {}
  @timeout = 0
  @connect_timeout = 0
  @max_redirects = -1
end

Instance Method Details

#action=(action) ⇒ Object

Sets the HTTP verb for the request



84
85
86
87
88
89
# File 'lib/patron/request.rb', line 84

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_nameString

Returns the set HTTP verb



163
164
165
# File 'lib/patron/request.rb', line 163

def action_name
  @action.to_s.upcase
end

#auth_type=(type = :basic) ⇒ Object

Set the type of authentication to use for this request.

Examples:

sess.username = "foo"
sess.password = "sekrit"
sess.auth_type = :digest


49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/patron/request.rb', line 49

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

Sets the receive buffer size. This is a recommendedation value, as CURL is not guaranteed to honor this value internally (see https://curl.haxx.se/libcurl/c/CURLOPT_BUFFERSIZE.html). By default, CURL uses the maximum possible buffer size, which will be the best especially for smaller and quickly-executing requests.



144
145
146
147
148
149
150
# File 'lib/patron/request.rb', line 144

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

Sets the connect timeout for the CURL request, in seconds. Can be set to less than a second using a floating-point value.



107
108
109
110
111
112
113
# File 'lib/patron/request.rb', line 107

def connect_timeout=(new_timeout)
  if new_timeout && new_timeout.to_f < 0
    raise ArgumentError, "Timeout must be a positive number"
  end

  @connect_timeout = new_timeout.to_f
end

#credentialsString, NilClass

Returns the set HTTP authentication string for basic authentication.



155
156
157
158
# File 'lib/patron/request.rb', line 155

def credentials
  return nil if username.nil? || password.nil?
  "#{username}:#{password}"
end

#eql?(request) ⇒ TrueClass, FalseClass Also known as: ==

Tells whether this Request is configured the same as the other request



169
170
171
172
173
174
175
# File 'lib/patron/request.rb', line 169

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

Sets the headers for the request. Headers muse be set with the right capitalization. The previously set headers will be replaced.



130
131
132
133
134
135
136
# File 'lib/patron/request.rb', line 130

def headers=(new_headers)
  if !new_headers.kind_of?(Hash)
    raise ArgumentError, "Headers must be a hash"
  end

  @headers = new_headers
end

#marshal_dumpArray

Returns a Marshalable representation of the Request



181
182
183
184
185
# File 'lib/patron/request.rb', line 181

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) ⇒ void

This method returns an undefined value.

Reinstates instance variables from a marshaled representation



190
191
192
193
194
# File 'lib/patron/request.rb', line 190

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

Sets the maximum number of redirects that are going to be followed.



118
119
120
121
122
123
124
# File 'lib/patron/request.rb', line 118

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

Sets the read timeout for the CURL request, in seconds. Can be set to less than a second using a floating-point value. Setting the value to 0 will disable the timeout.



96
97
98
99
100
101
# File 'lib/patron/request.rb', line 96

def timeout=(new_timeout)
  if new_timeout && new_timeout.to_f < 0
    raise ArgumentError, "Timeout must be a positive number"
  end
  @timeout = new_timeout.to_f
end

#upload_data=(data) ⇒ Object

Sets the upload data (request body) for the request. If the given argument is a Hash, the contents of the hash will be handled as form fields and will be form-encoded. The somposition of the request body is then going to be handled by Curl.

If the given data is any other object, it is going to be treated as a stringable request body (JSON or other verbatim type) and will have it's to_s method called before sending out the request.



72
73
74
75
76
77
78
79
# File 'lib/patron/request.rb', line 72

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