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 16 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 74 def auth @auth ||= Auth::Config.new end
Returns whether any authentication credentials were specified.
# File lib/httpi/request.rb, line 79 def auth? !!auth.type end
Sets a body request given a String or a Hash.
# File lib/httpi/request.rb, line 69 def body=(params) @body = params.kind_of?(Hash) ? Rack::Utils.build_query(params) : params end
Adds a header information to accept gzipped content.
# File lib/httpi/request.rb, line 61 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 51 def headers @headers ||= Rack::Utils::HeaderHash.new end
Sets the Hash of HTTP headers.
# File lib/httpi/request.rb, line 56 def headers=(headers) @headers = Rack::Utils::HeaderHash.new(headers) end
Expects a Hash of args
to assign.
# File lib/httpi/request.rb, line 84 def mass_assign(args) ATTRIBUTES.each { |key| send("#{key}=", args[key]) if args[key] } end
Sets the proxy
to use. Raises an ArgumentError
unless the proxy
is valid.
# File lib/httpi/request.rb, line 34 def proxy=(proxy) @proxy = normalize_url! proxy end
Returns whether to use SSL.
# File lib/httpi/request.rb, line 42 def ssl? return @ssl unless @ssl.nil? !!(url.to_s =~ /^https/) end
Sets the url
to access. Raises an ArgumentError
unless the url
is valid.
# File lib/httpi/request.rb, line 25 def url=(url) @url = normalize_url! url auth.basic @url.user, @url.password || '' if @url.user end
Private Instance Methods
Expects a url
, validates its validity and returns a
URI
object.
# File lib/httpi/request.rb, line 91 def normalize_url!(url) raise ArgumentError, "Invalid URL: #{url}" unless url.to_s =~ /^http/ url.kind_of?(URI) ? url : URI(url) end