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 collapse

ATTRIBUTES =

Available attribute writers.

[:url, :proxy, :headers, :body, :open_timeout, :read_timeout]

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.



74
75
76
# File 'lib/httpi/request.rb', line 74

def body
  @body
end

#open_timeoutObject

Returns the value of attribute open_timeout.



73
74
75
# File 'lib/httpi/request.rb', line 73

def open_timeout
  @open_timeout
end

#proxyObject

Returns the proxy to use.



40
41
42
# File 'lib/httpi/request.rb', line 40

def proxy
  @proxy
end

#read_timeoutObject

Returns the value of attribute read_timeout.



73
74
75
# File 'lib/httpi/request.rb', line 73

def read_timeout
  @read_timeout
end

#ssl=(value) ⇒ Object (writeonly)

Sets whether to use SSL.



49
50
51
# File 'lib/httpi/request.rb', line 49

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.



82
83
84
# File 'lib/httpi/request.rb', line 82

def auth
  @auth ||= Auth::Config.new
end

#auth?Boolean

Returns whether any authentication credentials were specified.

Returns:

  • (Boolean)


87
88
89
# File 'lib/httpi/request.rb', line 87

def auth?
  !!auth.type
end

#gzipObject

Adds a header information to accept gzipped content.



62
63
64
# File 'lib/httpi/request.rb', line 62

def gzip
  headers["Accept-Encoding"] = "gzip,deflate"
end

#headersObject

Returns a Hash of HTTP headers. Defaults to return an empty Hash.



52
53
54
# File 'lib/httpi/request.rb', line 52

def headers
  @headers ||= Rack::Utils::HeaderHash.new
end

#headers=(headers) ⇒ Object

Sets the Hash of HTTP headers.



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

def headers=(headers)
  @headers = Rack::Utils::HeaderHash.new(headers)
end

#mass_assign(args) ⇒ Object

Expects a Hash of args to assign.



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

def mass_assign(args)
  ATTRIBUTES.each { |key| send("#{key}=", args[key]) if args[key] }
end

#set_cookies(http_response) ⇒ Object

Sets the cookies from a given http_response.



67
68
69
70
71
# File 'lib/httpi/request.rb', line 67

def set_cookies(http_response)
  cookie_store.add *http_response.cookies
  cookies = cookie_store.fetch
  headers["Cookie"] = cookies if cookies
end

#ssl?Boolean

Returns whether to use SSL.

Returns:

  • (Boolean)


43
44
45
46
# File 'lib/httpi/request.rb', line 43

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