Class: Docker::Connection
- Inherits:
-
Object
- Object
- Docker::Connection
- 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
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Instance Method Summary collapse
-
#initialize(url = 'http://localhost', options = {}) ⇒ Connection
constructor
Create a new Connection.
-
#json_request(method, path, query = {}, &block) ⇒ Object
Send a request to the server and then parse it into a Hash.
-
#reset! ⇒ Object
Nil out the connection.
-
#resource ⇒ Object
The actual client that sends HTTP methods to the docker server.
- #to_s ⇒ Object
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', = {}) unless .is_a?(Hash) raise Docker::Error::ArgumentError, "Expected a Hash, got: #{}" end self.url = url self. = { :port => 4243 }.merge() end |
Instance Attribute Details
#options ⇒ Object
Returns the value of attribute options.
6 7 8 |
# File 'lib/docker/connection.rb', line 6 def @options end |
#url ⇒ Object
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. 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 |
#resource ⇒ Object
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.) end |
#to_s ⇒ Object
52 53 54 |
# File 'lib/docker/connection.rb', line 52 def to_s "Docker::Connection { :url => #{self.url}, :options => #{self.} }" end |