class HTTPI::Request
HTTPI::Request
¶ ↑
Represents an HTTP request and contains various methods for customizing that request.
Constants
- ATTRIBUTES
Available attribute writers.
Attributes
Returns the proxy
to use.
Sets whether to use SSL.
Returns the url
to access.
Public Class Methods
Accepts a Hash of args
to mass assign attributes and authentication credentials.
# 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
Public Instance Methods
Returns the HTTPI::Authentication
object.
# File lib/httpi/request.rb, line 112 def auth @auth ||= Auth::Config.new end
Returns whether any authentication credentials were specified.
# File lib/httpi/request.rb, line 117 def auth? !!auth.type end
Sets a body request given a String or a Hash.
# File lib/httpi/request.rb, line 97 def body=(params) @body = params.kind_of?(Hash) ? build_query_from_hash(params) : params end
Returns whether or not redirects should be followed - defaults to false if not set.
# File lib/httpi/request.rb, line 129 def follow_redirect? @follow_redirect ||= false end
Adds a header information to accept gzipped content.
# File lib/httpi/request.rb, line 76 def gzip headers["Accept-Encoding"] = "gzip,deflate" end
Returns a Hash of HTTP headers. Defaults to return an empty Hash.
# File lib/httpi/request.rb, line 66 def headers @headers ||= HTTPI::Utils::Headers.new end
Sets the Hash of HTTP headers.
# File lib/httpi/request.rb, line 71 def headers=(headers) @headers = HTTPI::Utils::Headers.new.merge(headers) end
Expects a Hash of args
to assign.
# File lib/httpi/request.rb, line 122 def mass_assign(args) ATTRIBUTES.each { |key| send("#{key}=", args[key]) if args[key] } end
Sets the block to be called while processing the response. The block accepts a single parameter - the chunked response body.
# 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
Sets the proxy
to use. Raises an ArgumentError
unless the proxy
is valid.
# File lib/httpi/request.rb, line 50 def proxy=(proxy) @proxy = normalize_url! proxy end
Returns the query
from url
.
# File lib/httpi/request.rb, line 45 def query self.url.query if self.url.respond_to?(:query) end
Sets the query
from url
. Raises an ArgumentError
unless the url
is valid.
# 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
Returns how many redirects should be followed - defaults to 3 if not set.
# File lib/httpi/request.rb, line 136 def redirect_limit @redirect_limit ||= 3 end
Returns whether to use SSL.
# File lib/httpi/request.rb, line 58 def ssl? @ssl ||= !!(url.to_s =~ /^https/) end
Sets the url
to access. Raises an ArgumentError
unless the url
is valid.
# File lib/httpi/request.rb, line 26 def url=(url) @url = normalize_url! url auth.basic @url.user, @url.password || '' if @url.user end
Private Instance Methods
Returns a query
string given a Hash
# File lib/httpi/request.rb, line 154 def build_query_from_hash(query) HTTPI.query_builder.build(query) end
Expects a url
, validates its validity and returns a URI
object.
# File lib/httpi/request.rb, line 148 def normalize_url!(url) raise ArgumentError, "Invalid URL: #{url}" unless url.to_s =~ /^http|socks/ url.kind_of?(URI) ? url : URI(url) end