Class: Delphix::Request

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/delphix/request.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

#request_id, #utc_httpdate

Constructor Details

#initialize(method, url, headers = {}, body = nil) ⇒ Request

Returns a new instance of Request.

Parameters:

  • method (Symnol)

    A valid HTTP verb ‘GET`, `POST`, `PUT`, `PATCH` or `DELETE`.

  • url (String)

    Endpoint (address or uri) to send request.

  • body (String, Hash, Object) (defaults to: nil)

    The request Body.

  • callback (Proc)

    Asychronous callback method to be invoked upon result.



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/delphix/request.rb', line 64

def initialize(method, url, headers = {}, body = nil)
  @method = method

  if method == :get
    if body.respond_to?(:keys) && body.respond_to?(:[]) && body.length > 0
      url += url.include?('?') ? '&' : '?'
      uri = Addressable::URI.new
      uri.query_values = body
      url += uri.query
    end
  else
    @body = body
  end

  if url =~ URI.regexp
    @url = url.gsub(/\s+/, '%20')
  else
    raise "Invalid URL: #{url}"
  end

  @headers = { 'Date' => utc_httpdate, 'Request-ID' => request_id }
  headers.each_pair { |key, value| @headers[key.downcase] = value }
  Delphix.last_request = { headers: @headers, method: @method, url: @url }
  begin
    Delphix.last_request[:body] = JSON.parse(@body) if body.length > 2
  rescue Exception
  end
end

Instance Attribute Details

#allObject (readonly)

Returns the value of attribute all.



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

def all
  @all
end

#bodyObject (readonly)

Returns the value of attribute body.



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

def body
  @body
end

#headersObject (readonly)

Returns the value of attribute headers.



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

def headers
  @headers
end

#methodObject (readonly)

Returns the value of attribute method.



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

def method
  @method
end

#urlObject (readonly)

Returns the value of attribute url.



33
34
35
# File 'lib/delphix/request.rb', line 33

def url
  @url
end

Instance Method Details

#add_header(name, value) ⇒ Object



48
49
50
# File 'lib/delphix/request.rb', line 48

def add_header(name, value)
  @headers[name] = value
end