Class: RestMan::Request

Inherits:
Object
  • Object
show all
Includes:
ActiveMethod, Init
Defined in:
lib/restman/request.rb,
lib/restman/request/init.rb,
lib/restman/request/init/url.rb,
lib/restman/request/transmit.rb,
lib/restman/request/proxy_uri.rb,
lib/restman/request/log_request.rb,
lib/restman/request/make_headers.rb,
lib/restman/request/init/ssl_opts.rb,
lib/restman/request/process_result.rb,
lib/restman/request/init/cookie_jar.rb,
lib/restman/request/net_http_object.rb,
lib/restman/request/stringify_headers.rb,
lib/restman/request/make_cookie_header.rb,
lib/restman/request/default_ssl_cert_store.rb,
lib/restman/request/fetch_body_to_tempfile.rb,
lib/restman/request/init/url/normalize_url.rb,
lib/restman/request/maybe_convert_extension.rb,
lib/restman/request/init/url/add_query_from_headers.rb

Overview

:include: _doc/lib/restman/request.rdoc

Defined Under Namespace

Modules: Init Classes: DefaultSSLCertStore, FetchBodyToTempfile, LogRequest, MakeCookieHeader, MakeHeaders, MaybeConvertExtension, NetHTTPObject, ProcessResult, ProxyURI, StringifyHeaders, Transmit

Constant Summary collapse

SSLOptionList =
%w{client_cert client_key ca_file ca_path cert_store
version ciphers verify_callback verify_callback_warnings
min_version max_version timeout}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Init

auth, headers, http_method, keep_alive_timeout, open_timeout, read_timeout, stream_log_percent, uri, write_timeout

Constructor Details

#initialize(args) ⇒ Request

Returns a new instance of Request.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/restman/request.rb', line 46

def initialize args
  @method = Init.http_method(args)
  @headers = Init.headers(args)
  @url = Init.url(args, headers)
  @uri = Init.uri(url)
  @user, @password = Init.auth(uri, args)
  @cookie_jar = Init.cookie_jar(uri, headers, args)
  @payload = Payload.generate(args[:payload])
  Init.read_timeout(args) {|value| @read_timeout = value}
  Init.open_timeout(args) {|value| @open_timeout = value}
  Init.write_timeout(args) {|value| @write_timeout = value}
  @block_response = args[:block_response]
  @raw_response = args[:raw_response] || false
  @local_host = args[:local_host]
  @local_port = args[:local_port]
  Init.keep_alive_timeout(args) {|value| @keep_alive_timeout = value}
  @close_on_empty_response = args[:close_on_empty_response]
  @stream_log_percent = Init.stream_log_percent(args)
  @proxy = args.fetch(:proxy) if args.include?(:proxy)
  @ssl_opts = Init.ssl_opts(args, uri)

  @log = args[:log]
  @max_redirects = args[:max_redirects] || 10
  @max_retries = args[:max_retries] || 1
  @processed_headers = make_headers headers
  @processed_headers_lowercase = Hash[@processed_headers.map {|k, v| [k.downcase, v]}]
  @args = args

  @before_execution_proc = args[:before_execution_proc]
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



23
24
25
# File 'lib/restman/request.rb', line 23

def args
  @args
end

#before_execution_procObject (readonly)

Returns the value of attribute before_execution_proc.



23
24
25
# File 'lib/restman/request.rb', line 23

def before_execution_proc
  @before_execution_proc
end

#block_responseObject (readonly)

Returns the value of attribute block_response.



23
24
25
# File 'lib/restman/request.rb', line 23

def block_response
  @block_response
end

#close_on_empty_responseObject (readonly)

Returns the value of attribute close_on_empty_response.



23
24
25
# File 'lib/restman/request.rb', line 23

def close_on_empty_response
  @close_on_empty_response
end

#headersObject (readonly)

Returns the value of attribute headers.



23
24
25
# File 'lib/restman/request.rb', line 23

def headers
  @headers
end

#keep_alive_timeoutObject (readonly)

Returns the value of attribute keep_alive_timeout.



23
24
25
# File 'lib/restman/request.rb', line 23

def keep_alive_timeout
  @keep_alive_timeout
end

#local_hostObject (readonly)

Returns the value of attribute local_host.



23
24
25
# File 'lib/restman/request.rb', line 23

def local_host
  @local_host
end

#local_portObject (readonly)

Returns the value of attribute local_port.



23
24
25
# File 'lib/restman/request.rb', line 23

def local_port
  @local_port
end

#max_redirectsObject (readonly)

Returns the value of attribute max_redirects.



23
24
25
# File 'lib/restman/request.rb', line 23

def max_redirects
  @max_redirects
end

#max_retriesObject (readonly)

Returns the value of attribute max_retries.



23
24
25
# File 'lib/restman/request.rb', line 23

def max_retries
  @max_retries
end

#methodObject (readonly)

Returns the value of attribute method.



23
24
25
# File 'lib/restman/request.rb', line 23

def method
  @method
end

#open_timeoutObject (readonly)

Returns the value of attribute open_timeout.



23
24
25
# File 'lib/restman/request.rb', line 23

def open_timeout
  @open_timeout
end

#passwordObject (readonly)

Returns the value of attribute password.



23
24
25
# File 'lib/restman/request.rb', line 23

def password
  @password
end

#payloadObject (readonly)

Returns the value of attribute payload.



23
24
25
# File 'lib/restman/request.rb', line 23

def payload
  @payload
end

#processed_headersObject (readonly)

Returns the value of attribute processed_headers.



23
24
25
# File 'lib/restman/request.rb', line 23

def processed_headers
  @processed_headers
end

#proxyObject (readonly)

Returns the value of attribute proxy.



23
24
25
# File 'lib/restman/request.rb', line 23

def proxy
  @proxy
end

#raw_responseObject (readonly)

Returns the value of attribute raw_response.



23
24
25
# File 'lib/restman/request.rb', line 23

def raw_response
  @raw_response
end

#read_timeoutObject (readonly)

Returns the value of attribute read_timeout.



23
24
25
# File 'lib/restman/request.rb', line 23

def read_timeout
  @read_timeout
end

#redirection_historyObject

An array of previous redirection responses



32
33
34
# File 'lib/restman/request.rb', line 32

def redirection_history
  @redirection_history
end

#ssl_optsObject (readonly)

Returns the value of attribute ssl_opts.



23
24
25
# File 'lib/restman/request.rb', line 23

def ssl_opts
  @ssl_opts
end

#uriObject (readonly)

Returns the value of attribute uri.



23
24
25
# File 'lib/restman/request.rb', line 23

def uri
  @uri
end

#urlObject (readonly)

Returns the value of attribute url.



23
24
25
# File 'lib/restman/request.rb', line 23

def url
  @url
end

#userObject (readonly)

Returns the value of attribute user.



23
24
25
# File 'lib/restman/request.rb', line 23

def user
  @user
end

#write_timeoutObject (readonly)

Returns the value of attribute write_timeout.



23
24
25
# File 'lib/restman/request.rb', line 23

def write_timeout
  @write_timeout
end

Class Method Details

.default_ssl_cert_storeObject

:include: _doc/lib/restman/request/default_ssl_cert_store.rdoc



132
133
134
# File 'lib/restman/request.rb', line 132

def self.default_ssl_cert_store
  DefaultSSLCertStore.call
end

.execute(args, &block) ⇒ Object



34
35
36
# File 'lib/restman/request.rb', line 34

def self.execute(args, & block)
  new(args).execute(& block)
end

Instance Method Details

:include: _doc/lib/restman/request/cookie_jar.rdoc



112
113
114
# File 'lib/restman/request.rb', line 112

def cookie_jar
  @cookie_jar
end

#cookiesObject

:include: _doc/lib/restman/request/cookies.rdoc



101
102
103
104
105
106
107
108
109
# File 'lib/restman/request.rb', line 101

def cookies
  hash = {}

  @cookie_jar.cookies(uri).each do |c|
    hash[c.name] = c.value
  end

  hash
end

#default_headersObject

:include: _doc/lib/restman/request/default_headers.rdoc



147
148
149
150
151
152
# File 'lib/restman/request.rb', line 147

def default_headers
  {
    :accept => '*/*',
    :user_agent => RestMan::Platform.default_user_agent,
  }
end

#execute(&block) ⇒ Object



77
78
79
80
81
82
83
# File 'lib/restman/request.rb', line 77

def execute & block
  # With 2.0.0+, net/http accepts URI objects in requests and handles wrapping
  # IPv6 addresses in [] for use in the Host request header.
  transmit uri, net_http_request_class(method).new(uri, processed_headers), payload, & block
ensure
  payload.close if payload
end

#inspectObject



42
43
44
# File 'lib/restman/request.rb', line 42

def inspect
  "<RestMan::Request @method=#{@method.inspect}, @url=#{@url.inspect}>"
end

#logObject

Default to the global logger if there’s not a request-specific one



137
138
139
# File 'lib/restman/request.rb', line 137

def log
  @log || RestMan.log
end

#net_http_request_class(method) ⇒ Object



127
128
129
# File 'lib/restman/request.rb', line 127

def net_http_request_class(method)
  Net::HTTP.const_get(method.capitalize, false)
end

#parserObject



167
168
169
# File 'lib/restman/request.rb', line 167

def parser
  URI.const_defined?(:Parser) ? URI::Parser.new : URI
end

#setup_credentials(req) ⇒ Object



156
157
158
159
160
# File 'lib/restman/request.rb', line 156

def setup_credentials(req)
  if user && !@processed_headers_lowercase.include?('authorization')
    req.basic_auth(user, password)
  end
end

#use_ssl?Boolean

:include: _doc/lib/restman/request/use_ssl.rdoc

Returns:

  • (Boolean)


96
97
98
# File 'lib/restman/request.rb', line 96

def use_ssl?
  uri.is_a?(URI::HTTPS)
end

#verify_sslObject

SSL-related options



86
87
88
# File 'lib/restman/request.rb', line 86

def verify_ssl
  @ssl_opts.fetch(:verify_ssl)
end