Class: Docker::Distribution::Connection

Inherits:
Object
  • Object
show all
Includes:
Error
Defined in:
lib/docker/distribution/api/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.



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

def initialize(url, opts)
  case
    when !url.is_a?(String)
      raise ArgumentError, "Expected a String, got: '#{url.class}'"
    when !opts.is_a?(Hash)
      raise ArgumentError, "Expected a Hash, got: '#{opts.class}'"
    else
      uri = URI.parse(url)
      if ['https', 'http'].include? uri.scheme
        @url, @options = url, opts
      else
        @url, @options = "http://#{uri}", opts
      end
  end
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



9
10
11
# File 'lib/docker/distribution/api/connection.rb', line 9

def options
  @options
end

#urlObject (readonly)

Returns the value of attribute url.



9
10
11
# File 'lib/docker/distribution/api/connection.rb', line 9

def url
  @url
end

Instance Method Details

#log_request(request) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/docker/distribution/api/connection.rb', line 57

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

#request(*args, &block) ⇒ Object

Send a request to the server with the ‘



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

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



70
71
72
# File 'lib/docker/distribution/api/connection.rb', line 70

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