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.



16
17
18
19
20
21
22
# 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

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



64
65
66
# File 'lib/httpi/request.rb', line 64

def body
  @body
end

#open_timeoutObject

Returns the value of attribute open_timeout.



64
65
66
# File 'lib/httpi/request.rb', line 64

def open_timeout
  @open_timeout
end

#proxyObject

Returns the proxy to use.



38
39
40
# File 'lib/httpi/request.rb', line 38

def proxy
  @proxy
end

#read_timeoutObject

Returns the value of attribute read_timeout.



64
65
66
# File 'lib/httpi/request.rb', line 64

def read_timeout
  @read_timeout
end

#ssl=(value) ⇒ Object (writeonly)

Sets whether to use SSL.



47
48
49
# File 'lib/httpi/request.rb', line 47

def ssl=(value)
  @ssl = value
end

#urlObject

Returns the url to access.



30
31
32
# File 'lib/httpi/request.rb', line 30

def url
  @url
end

Instance Method Details

#authObject

Returns the HTTPI::Authentication object.



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

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

#auth?Boolean

Returns whether any authentication credentials were specified.

Returns:

  • (Boolean)


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

def auth?
  !!auth.type
end

#gzipObject

Adds a header information to accept gzipped content.



60
61
62
# File 'lib/httpi/request.rb', line 60

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

#headersObject

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



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

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

#headers=(headers) ⇒ Object

Sets the Hash of HTTP headers.



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

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

#mass_assign(args) ⇒ Object

Expects a Hash of args to assign.



77
78
79
# File 'lib/httpi/request.rb', line 77

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

#ssl?Boolean

Returns whether to use SSL.

Returns:

  • (Boolean)


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

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