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

#log_request(request) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/docker/connection.rb', line 55

def log_request(request)
  if Docker.logger
    Docker.logger.debug(
      [request[:method], request[:path], request[:query], request[:body]]
    )
  end
end

#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
# File 'lib/docker/connection.rb', line 37

def request(*args, &block)
  request = compile_request_params(*args, &block)
  log_request(request)
  resource.request(request).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



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

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