Class: Delphix::Connection

Inherits:
Object
  • Object
show all
Includes:
Error
Defined in:
lib/delphix/connection.rb

Overview

This class represents a Connection to a Delphix Engine. The Connection is immutable in that once the url and options is set they cannot be changed.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, opts) ⇒ Connection

Returns a new instance of Connection.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/delphix/connection.rb', line 10

def initialize(url, opts)
  case
  when !url.is_a?(String)
    raise ArgumentError, "Expected a String, got: '#{url}'"
  when !opts.is_a?(Hash)
    raise ArgumentError, "Expected a Hash, got: '#{opts}'"
  else
    @url, @options = url, opts
    
    # new HTTP client and session
    @session = Excon.new(url, options)
    @session_cookie = nil
  end
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



8
9
10
# File 'lib/delphix/connection.rb', line 8

def options
  @options
end

#urlObject (readonly)

Returns the value of attribute url.



8
9
10
# File 'lib/delphix/connection.rb', line 8

def url
  @url
end

Instance Method Details

#log_request(request) ⇒ Object



59
60
61
# File 'lib/delphix/connection.rb', line 59

def log_request(request)
  puts "#{[request[:method], request[:path], request[:body]]}" if Delphix.debug
end

#log_response(response) ⇒ Object



63
64
65
# File 'lib/delphix/connection.rb', line 63

def log_response(response)
  puts "#{[response.headers, response.body]}" if Delphix.debug
end

#request(*args, &block) ⇒ Object

Send a request to the server



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/delphix/connection.rb', line 26

def request(*args, &block)
  
  request = compile_request_params(*args, &block)
  
  log_request(request) if Delphix.debug
  
  # execute the request and grao the session cookie if not already set
  response = @session.request(request)
  @session_cookie ||= cookie?(response)
  
  log_response(response) if Delphix.debug
  
  return Delphix::Util.parse_json( response.body)
  
rescue Excon::Errors::BadRequest => ex
  raise ClientError, ex.response.body
rescue Excon::Errors::Unauthorized => ex
  raise UnauthorizedError, ex.response.body
rescue Excon::Errors::NotFound => ex
  raise NotFoundError, ex.response.body
rescue Excon::Errors::Conflict => ex
  raise ConflictError, ex.response.body
rescue Excon::Errors::InternalServerError => ex
  raise ServerError, ex.response.body
rescue Excon::Errors::Timeout => ex
  raise TimeoutError, ex.message
end

#to_sObject



67
68
69
# File 'lib/delphix/connection.rb', line 67

def to_s
  "Delphix::Connection { :url => #{url}, :options => #{options} }"
end