Module: Cts::Mpx::Driver::Connections

Defined in:
lib/cts/mpx/driver/connections.rb

Overview

Container for active connections to the data service.

Class Method Summary collapse

Class Method Details

.[](uri = nil) ⇒ Excon, Excon[]

Addressable method for active connections. If you provide a string that is not active, an active one will be created.

Parameters:

  • uri (String) (defaults to: nil)

    uri of a service to connect to, must contain theplatform.

Returns:

  • (Excon)

    assembled excon objects with service defaults.

  • (Excon[])

    if nil, an array of all open connections.

Raises:

  • (ArgumentError)


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/cts/mpx/driver/connections.rb', line 13

def [](uri = nil)
  return @collection unless uri

  begin
    parsed_uri = URI.parse uri
  rescue URI::InvalidURIError
    raise ArgumentError, "(#{uri}) is not a uri"
  end

  raise ArgumentError, "(#{uri}) does not contain theplatform in it." unless parsed_uri.host&.include? "theplatform"

  c = create_connection parsed_uri unless @collection.include? parsed_uri.host
  @collection.push c
  @collection.last
  c
end

.collectionObject



34
35
36
# File 'lib/cts/mpx/driver/connections.rb', line 34

def collection
  @collection
end

.create_connection(parsed_uri) ⇒ Object



30
31
32
# File 'lib/cts/mpx/driver/connections.rb', line 30

def create_connection(parsed_uri)
  Excon.new([parsed_uri.scheme, parsed_uri.host].join("://"), persistent: true)
end

.initializeObject



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/cts/mpx/driver/connections.rb', line 38

def initialize
  Excon.defaults[:omit_nil] = true
  Excon.defaults[:persistent] = true
  Excon.defaults[:headers] = {
    'Content-Type'     => "application/json",
    "User-Agent"       => "cts-mpx ruby sdk version #{Cts::Mpx::VERSION}",
    'Content-Encoding' => 'bzip2,xz,gzip,deflate'
  }

  @collection = []
end