Module: Docker::Util

Includes:
Error
Defined in:
lib/docker/util.rb

Overview

This module holds shared logic that doesn’t really belong anywhere else in the gem.

Class Method Summary collapse

Class Method Details

.create_dir_tar(directory) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/docker/util.rb', line 22

def create_dir_tar(directory)
  cwd = FileUtils.pwd
  tempfile = File.new('/tmp/out', 'wb')
  FileUtils.cd(directory)
  Archive::Tar::Minitar.pack('.', tempfile)
  File.new('/tmp/out', 'r')
ensure
  FileUtils.cd(cwd)
end

.create_tar(hash = {}) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/docker/util.rb', line 12

def create_tar(hash = {})
  output = StringIO.new
  Gem::Package::TarWriter.new(output) do |tar|
    hash.each do |file_name, input|
      tar.add_file(file_name, 0640) { |tar_file| tar_file.write(input) }
    end
  end
  output.tap(&:rewind)
end

.extract_id(body) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/docker/util.rb', line 32

def extract_id(body)
  line = body.lines.to_a[-1]
  if (id = line.match(/^Successfully built ([a-f0-9]+)$/)) && !id[1].empty?
    id[1]
  else
    raise UnexpectedResponseError, "Couldn't find id: #{body}"
  end
end

.parse_json(body) ⇒ Object



6
7
8
9
10
# File 'lib/docker/util.rb', line 6

def parse_json(body)
  JSON.parse(body) unless body.nil? || body.empty? || (body == 'null')
rescue JSON::ParserError => ex
  raise UnexpectedResponseError, ex.message
end