Class: CFoundry::RestClient

Inherits:
Object
  • Object
show all
Includes:
TraceHelpers
Defined in:
lib/cfoundry/rest_client.rb

Constant Summary collapse

LOG_LENGTH =
10
HTTP_METHODS =
{
  "GET" => Net::HTTP::Get,
  "PUT" => Net::HTTP::Put,
  "POST" => Net::HTTP::Post,
  "DELETE" => Net::HTTP::Delete,
  "HEAD" => Net::HTTP::Head,
}
DEFAULT_OPTIONS =
{
  :follow_redirects => true
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from TraceHelpers

#request_trace, #response_trace

Constructor Details

#initialize(target, token = nil) ⇒ RestClient

Returns a new instance of RestClient.



29
30
31
32
33
34
35
# File 'lib/cfoundry/rest_client.rb', line 29

def initialize(target, token = nil)
  @target = target
  @token = token
  @trace = false
  @backtrace = false
  @log = false
end

Instance Attribute Details

#backtraceObject

Returns the value of attribute backtrace.



27
28
29
# File 'lib/cfoundry/rest_client.rb', line 27

def backtrace
  @backtrace
end

#logObject

Returns the value of attribute log.



27
28
29
# File 'lib/cfoundry/rest_client.rb', line 27

def log
  @log
end

#proxyObject

Returns the value of attribute proxy.



27
28
29
# File 'lib/cfoundry/rest_client.rb', line 27

def proxy
  @proxy
end

#request_idObject

Returns the value of attribute request_id.



27
28
29
# File 'lib/cfoundry/rest_client.rb', line 27

def request_id
  @request_id
end

#targetObject

Returns the value of attribute target.



25
26
27
# File 'lib/cfoundry/rest_client.rb', line 25

def target
  @target
end

#tokenObject

Returns the value of attribute token.



27
28
29
# File 'lib/cfoundry/rest_client.rb', line 27

def token
  @token
end

#traceObject

Returns the value of attribute trace.



27
28
29
# File 'lib/cfoundry/rest_client.rb', line 27

def trace
  @trace
end

Instance Method Details

#generate_headers(payload, options) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/cfoundry/rest_client.rb', line 41

def generate_headers(payload, options)
  headers = {}

  if payload.is_a?(String)
    headers["Content-Length"] = payload.size
  elsif !payload
    headers["Content-Length"] = 0
  end

  headers["X-Request-Id"] = @request_id if @request_id
  headers["Authorization"] = @token.auth_header if @token
  headers["Proxy-User"] = @proxy if @proxy

  if accept_type = mimetype(options[:accept])
    headers["Accept"] = accept_type
  end

  if content_type = mimetype(options[:content])
    headers["Content-Type"] = content_type
  end

  headers.merge!(options[:headers]) if options[:headers]
  headers
end

#request(method, path, options = {}) ⇒ Object



37
38
39
# File 'lib/cfoundry/rest_client.rb', line 37

def request(method, path, options = {})
  request_uri(method, construct_url(path), DEFAULT_OPTIONS.merge(options))
end