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 = 'http://localhost', options = {}) ⇒ Connection

Create a new Connection. By default, the Connection points to localhost at port 4243, but this can be changed via an options Hash.



10
11
12
13
14
15
16
# File 'lib/docker/connection.rb', line 10

def initialize(url = 'http://localhost', options = {})
  unless options.is_a?(Hash)
    raise Docker::Error::ArgumentError, "Expected a Hash, got: #{options}"
  end
  self.url = url
  self.options = { :port => 4243 }.merge(options)
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



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

def options
  @options
end

#urlObject

Returns the value of attribute url.



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

def url
  @url
end

Instance Method Details

#json_request(method, path, query = {}, &block) ⇒ Object

Send a request to the server and then parse it into a Hash.



44
45
46
47
48
49
50
# File 'lib/docker/connection.rb', line 44

def json_request(method, path, query = {}, &block)
  params = compile_request_params(method, path, query, &block)
  body = self.request(params).body
  JSON.parse(body) unless body.nil? || body.empty? || (body == 'null')
rescue JSON::ParserError => ex
  raise UnexpectedResponseError, ex.message
end

#reset!Object

Nil out the connection. This now happens on every request to prevent socket errors.



25
26
27
# File 'lib/docker/connection.rb', line 25

def reset!
  @resource = nil
end

#resourceObject

The actual client that sends HTTP methods to the docker server.



19
20
21
# File 'lib/docker/connection.rb', line 19

def resource
  @resource ||= Excon.new(self.url, self.options)
end

#to_sObject



52
53
54
# File 'lib/docker/connection.rb', line 52

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