Class: Docker::Connection

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

Overview

This class represents a Connection to a Docker server. 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

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.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/docker/connection.rb', line 11

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.



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

def options
  @options
end

#urlObject (readonly)

Returns the value of attribute url.



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

def url
  @url
end

Instance Method Details

#request(*args, &block) ⇒ Object

Send a request to the server with the ‘



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

def request(*args, &block)
  request = compile_request_params(*args, &block)
  if Docker.logger
    Docker.logger.debug(
      [request[:method], request[:path], request[:query], request[:body]]
    )
  end
  resource.request(request).body
rescue Excon::Errors::BadRequest => ex
  raise ClientError, ex.message
rescue Excon::Errors::Unauthorized => ex
  raise UnauthorizedError, ex.message
rescue Excon::Errors::NotFound => ex
  raise NotFoundError, ex.message
rescue Excon::Errors::InternalServerError => ex
  raise ServerError, ex.response.data[:body]
rescue Excon::Errors::Timeout => ex
  raise TimeoutError, ex.message
end

#to_sObject



62
63
64
# File 'lib/docker/connection.rb', line 62

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