Class: HTTPI::Request
- Inherits:
-
Object
- Object
- HTTPI::Request
- Defined in:
- lib/httpi/request.rb
Overview
HTTPI::Request
Represents an HTTP request and contains various methods for customizing that request.
Constant Summary collapse
- ATTRIBUTES =
Available attribute writers.
[:url, :proxy, :headers, :body, :open_timeout, :read_timeout, :write_timeout, :follow_redirect, :redirect_limit, :query]
Instance Attribute Summary collapse
-
#body ⇒ Object
Returns the value of attribute body.
-
#follow_redirect ⇒ Object
writeonly
Sets the attribute follow_redirect.
-
#open_timeout ⇒ Object
Returns the value of attribute open_timeout.
-
#proxy ⇒ Object
Returns the
proxy
to use. -
#read_timeout ⇒ Object
Returns the value of attribute read_timeout.
-
#redirect_limit ⇒ Object
Returns how many redirects should be followed - defaults to 3 if not set.
-
#ssl ⇒ Object
writeonly
Sets whether to use SSL.
-
#url ⇒ Object
Returns the
url
to access. -
#write_timeout ⇒ Object
Returns the value of attribute write_timeout.
Instance Method Summary collapse
-
#auth ⇒ Object
Returns the
HTTPI::Authentication
object. -
#auth? ⇒ Boolean
Returns whether any authentication credentials were specified.
-
#follow_redirect? ⇒ Boolean
Returns whether or not redirects should be followed - defaults to false if not set.
-
#gzip ⇒ Object
Adds a header information to accept gzipped content.
-
#headers ⇒ Object
Returns a Hash of HTTP headers.
-
#headers=(headers) ⇒ Object
Sets the Hash of HTTP headers.
-
#initialize(args = {}) ⇒ Request
constructor
Accepts a Hash of
args
to mass assign attributes and authentication credentials. -
#mass_assign(args) ⇒ Object
Expects a Hash of
args
to assign. -
#on_body(&block) ⇒ Object
Sets the block to be called while processing the response.
-
#query ⇒ Object
Returns the
query
fromurl
. -
#query=(query) ⇒ Object
Sets the
query
fromurl
. -
#set_cookies(object_or_array) ⇒ Object
Sets the cookies from an object responding to ‘cookies` (e.g. `HTTPI::Response`) or an Array of `HTTPI::Cookie` objects.
-
#ssl? ⇒ Boolean
Returns whether to use SSL.
Constructor Details
#initialize(args = {}) ⇒ Request
Accepts a Hash of args
to mass assign attributes and authentication credentials.
17 18 19 20 21 22 23 |
# File 'lib/httpi/request.rb', line 17 def initialize(args = {}) if args.kind_of? String self.url = args elsif args.kind_of?(Hash) && !args.empty? mass_assign args end end |
Instance Attribute Details
#body ⇒ Object
Returns the value of attribute body.
94 95 96 |
# File 'lib/httpi/request.rb', line 94 def body @body end |
#follow_redirect=(value) ⇒ Object (writeonly)
Sets the attribute follow_redirect
126 127 128 |
# File 'lib/httpi/request.rb', line 126 def follow_redirect=(value) @follow_redirect = value end |
#open_timeout ⇒ Object
Returns the value of attribute open_timeout.
93 94 95 |
# File 'lib/httpi/request.rb', line 93 def open_timeout @open_timeout end |
#proxy ⇒ Object
Returns the proxy
to use.
55 56 57 |
# File 'lib/httpi/request.rb', line 55 def proxy @proxy end |
#read_timeout ⇒ Object
Returns the value of attribute read_timeout.
93 94 95 |
# File 'lib/httpi/request.rb', line 93 def read_timeout @read_timeout end |
#redirect_limit ⇒ Object
Returns how many redirects should be followed - defaults to 3 if not set.
136 137 138 |
# File 'lib/httpi/request.rb', line 136 def redirect_limit @redirect_limit ||= 3 end |
#ssl=(value) ⇒ Object (writeonly)
Sets whether to use SSL.
63 64 65 |
# File 'lib/httpi/request.rb', line 63 def ssl=(value) @ssl = value end |
#url ⇒ Object
Returns the url
to access.
32 33 34 |
# File 'lib/httpi/request.rb', line 32 def url @url end |
#write_timeout ⇒ Object
Returns the value of attribute write_timeout.
93 94 95 |
# File 'lib/httpi/request.rb', line 93 def write_timeout @write_timeout end |
Instance Method Details
#auth ⇒ Object
Returns the HTTPI::Authentication
object.
112 113 114 |
# File 'lib/httpi/request.rb', line 112 def auth @auth ||= Auth::Config.new end |
#auth? ⇒ Boolean
Returns whether any authentication credentials were specified.
117 118 119 |
# File 'lib/httpi/request.rb', line 117 def auth? !!auth.type end |
#follow_redirect? ⇒ Boolean
Returns whether or not redirects should be followed - defaults to false if not set.
129 130 131 |
# File 'lib/httpi/request.rb', line 129 def follow_redirect? @follow_redirect ||= false end |
#gzip ⇒ Object
Adds a header information to accept gzipped content.
76 77 78 |
# File 'lib/httpi/request.rb', line 76 def gzip headers["Accept-Encoding"] = "gzip,deflate" end |
#headers ⇒ Object
Returns a Hash of HTTP headers. Defaults to return an empty Hash.
66 67 68 |
# File 'lib/httpi/request.rb', line 66 def headers @headers ||= HTTPI::Utils::Headers.new end |
#headers=(headers) ⇒ Object
Sets the Hash of HTTP headers.
71 72 73 |
# File 'lib/httpi/request.rb', line 71 def headers=(headers) @headers = HTTPI::Utils::Headers.new.merge(headers) end |
#mass_assign(args) ⇒ Object
Expects a Hash of args
to assign.
122 123 124 |
# File 'lib/httpi/request.rb', line 122 def mass_assign(args) ATTRIBUTES.each { |key| send("#{key}=", args[key]) if args[key] } end |
#on_body(&block) ⇒ Object
Sets the block to be called while processing the response. The block accepts a single parameter - the chunked response body.
103 104 105 106 107 108 109 |
# File 'lib/httpi/request.rb', line 103 def on_body(&block) @on_body ||= nil if block_given? then @on_body = block end @on_body end |
#query ⇒ Object
Returns the query
from url
.
45 46 47 |
# File 'lib/httpi/request.rb', line 45 def query self.url.query if self.url.respond_to?(:query) end |
#query=(query) ⇒ Object
Sets the query
from url
. Raises an ArgumentError
unless the url
is valid.
35 36 37 38 39 40 41 42 |
# File 'lib/httpi/request.rb', line 35 def query=(query) raise ArgumentError, "Invalid URL: #{self.url}" unless self.url.respond_to?(:query) if query.kind_of?(Hash) query = build_query_from_hash(query) end query = query.to_s unless query.is_a?(String) self.url.query = query end |
#set_cookies(object_or_array) ⇒ Object
Sets the cookies from an object responding to ‘cookies` (e.g. `HTTPI::Response`) or an Array of `HTTPI::Cookie` objects.
82 83 84 85 86 87 88 89 90 91 |
# File 'lib/httpi/request.rb', line 82 def (object_or_array) if object_or_array.respond_to?(:cookies) .add(*object_or_array.) else .add(*object_or_array) end = .fetch headers["Cookie"] = if end |
#ssl? ⇒ Boolean
Returns whether to use SSL.
58 59 60 |
# File 'lib/httpi/request.rb', line 58 def ssl? @ssl ||= !!(url.to_s =~ /^https/) end |