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.



15
16
17
18
19
20
21
# File 'lib/httpi/request.rb', line 15

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.



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

def body
  @body
end

#headersObject

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



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

def headers
  @headers ||= {}
end

#open_timeoutObject

Returns the value of attribute open_timeout.



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

def open_timeout
  @open_timeout
end

#proxyObject

Returns the proxy to use.



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

def proxy
  @proxy
end

#read_timeoutObject

Returns the value of attribute read_timeout.



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

def read_timeout
  @read_timeout
end

#ssl=(value) ⇒ Object (writeonly)

Sets whether to use SSL.



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

def ssl=(value)
  @ssl = value
end

#urlObject

Returns the url to access.



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

def url
  @url
end

Instance Method Details

#authObject

Returns the HTTPI::Authentication object.



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

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

#auth?Boolean

Returns whether any authentication credentials were specified.

Returns:

  • (Boolean)


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

def auth?
  !!auth.type
end

#gzipObject

Adds a header information to accept gzipped content.



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

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

#mass_assign(args) ⇒ Object

Expects a Hash of args to assign.



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

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

#ssl?Boolean

Returns whether to use SSL.

Returns:

  • (Boolean)


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

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