Class: Algo::Docker::Connection

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, opts) ⇒ Connection

Create a new Connection. This method takes a url (String) and options (Hash). These are passed to Excon, so any options valid for ‘Excon.new` can be passed here.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/algo/docker/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
    uri = URI.parse(url)
    if uri.scheme == "unix"
      @url, @options = 'unix:///', {:socket => uri.path}.merge(opts)
    elsif uri.scheme =~ /^(https?|tcp)$/
      @url, @options = url, opts
    else
      @url, @options = "http://#{uri}", opts
    end
  end
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/algo/docker/connection.rb', line 5

def options
  @options
end

#urlObject (readonly)

Returns the value of attribute url.



5
6
7
# File 'lib/algo/docker/connection.rb', line 5

def url
  @url
end

Instance Method Details

#request(*args, &block) ⇒ Object

Send a request to the server with the ‘



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/algo/docker/connection.rb', line 36

def request(*args, &block)
  request = compile_request_params(*args, &block)
  # log_request(request)
  response = resource.request(request).body
  JSON.parse(response) unless response.blank?
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



68
69
70
# File 'lib/algo/docker/connection.rb', line 68

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