Class: HTTPI::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/httpi/request.rb

Overview

HTTPI::Request

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

Constant Summary

ATTRIBUTES =

Available attribute writers.

[:url, :proxy, :headers, :body, :open_timeout, :read_timeout, :follow_redirect, :redirect_limit, :query]

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#bodyObject

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

Parameters:

  • value

    the value to set the attribute follow_redirect to.



126
127
128
# File 'lib/httpi/request.rb', line 126

def follow_redirect=(value)
  @follow_redirect = value
end

#open_timeoutObject

Returns the value of attribute open_timeout



93
94
95
# File 'lib/httpi/request.rb', line 93

def open_timeout
  @open_timeout
end

#proxyObject

Returns the proxy to use.



55
56
57
# File 'lib/httpi/request.rb', line 55

def proxy
  @proxy
end

#read_timeoutObject

Returns the value of attribute read_timeout



93
94
95
# File 'lib/httpi/request.rb', line 93

def read_timeout
  @read_timeout
end

#redirect_limitObject

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

#urlObject

Returns the url to access.



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

def url
  @url
end

Instance Method Details

#authObject

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.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


129
130
131
# File 'lib/httpi/request.rb', line 129

def follow_redirect?
  @follow_redirect ||= false
end

#gzipObject

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

#headersObject

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 ||= Rack::Utils::HeaderHash.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 = Rack::Utils::HeaderHash.new(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

#queryObject

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.

Raises:

  • (ArgumentError)


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 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?Boolean

Returns whether to use SSL.

Returns:

  • (Boolean)


58
59
60
# File 'lib/httpi/request.rb', line 58

def ssl?
  @ssl ||= !!(url.to_s =~ /^https/)
end