class HTTPI::Request

HTTPI::Request

Represents an HTTP request and contains various methods for customizing that request.

Constants

ATTRIBUTES

Available attribute writers.

Attributes

body[R]
follow_redirect[W]
open_timeout[RW]
proxy[R]

Returns the proxy to use.

read_timeout[RW]
redirect_limit[W]
ssl[W]

Sets whether to use SSL.

url[R]

Returns the url to access.

write_timeout[RW]

Public Class Methods

new(args = {}) click to toggle source

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

auth() click to toggle source

Returns the HTTPI::Authentication object.

# File lib/httpi/request.rb, line 112
def auth
  @auth ||= Auth::Config.new
end
auth?() click to toggle source

Returns whether any authentication credentials were specified.

# File lib/httpi/request.rb, line 117
def auth?
  !!auth.type
end
body=(params) click to toggle source

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
follow_redirect?() click to toggle source

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
gzip() click to toggle source

Adds a header information to accept gzipped content.

# File lib/httpi/request.rb, line 76
def gzip
  headers["Accept-Encoding"] = "gzip,deflate"
end
headers() click to toggle source

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
headers=(headers) click to toggle source

Sets the Hash of HTTP headers.

# File lib/httpi/request.rb, line 71
def headers=(headers)
  @headers = HTTPI::Utils::Headers.new.merge(headers)
end
mass_assign(args) click to toggle source

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
on_body(&block) click to toggle source

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
proxy=(proxy) click to toggle source

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
query() click to toggle source

Returns the query from url.

# File lib/httpi/request.rb, line 45
def query
  self.url.query if self.url.respond_to?(:query)
end
query=(query) click to toggle source

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
redirect_limit() click to toggle source

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
set_cookies(object_or_array) click to toggle source

Sets the cookies from an object responding to ‘cookies` (e.g. `HTTPI::Response`) or an Array of `HTTPI::Cookie` objects.

# File lib/httpi/request.rb, line 82
def set_cookies(object_or_array)
  if object_or_array.respond_to?(:cookies)
    cookie_store.add(*object_or_array.cookies)
  else
    cookie_store.add(*object_or_array)
  end

  cookies = cookie_store.fetch
  headers["Cookie"] = cookies if cookies
end
ssl?() click to toggle source

Returns whether to use SSL.

# File lib/httpi/request.rb, line 58
def ssl?
  @ssl ||= !!(url.to_s =~ /^https/)
end
url=(url) click to toggle source

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

build_query_from_hash(query) click to toggle source

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
normalize_url!(url) click to toggle source

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