Module: Babl

Defined in:
lib/babl.rb

Defined Under Namespace

Classes: ModuleError, UnknownModuleError

Class Method Summary collapse

Class Method Details

.bin_pathObject



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/babl.rb', line 22

def self.bin_path
  system = `which babl-rpc`.strip
  if system.empty?
    bin = 'babl-rpc_'
    bin += "linux_amd64" if RUBY_PLATFORM =~ /linux/
    bin += "darwin_amd64" if RUBY_PLATFORM =~ /darwin/
    File.expand_path("../../bin/#{bin}", __FILE__)
  else
    system
  end
end

.clientObject



38
39
40
41
# File 'lib/babl.rb', line 38

def self.client
  STDERR.puts "Using bin '#{bin_path}'"
  @client ||= Quartz::Client.new(bin_path: bin_path)
end

.module!(name, opts = {}) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/babl.rb', line 43

def self.module! name, opts = {}
  params = {'Name' => name}
  if opts[:in]
    params['Stdin'] = Base64.encode64(opts[:in]).strip
  end
  if opts[:env]
    params['Env'] = opts[:env].inject({}) { |h, (k,v)| h[k.to_s] = v.to_s; h }
  end
  begin
    res = client[:babl].call('Module', params)
  rescue Quartz::ResponseError => e
    if e.message == 'babl-rpc: unknown module'
      raise UnknownModuleError.new('Unknown Module')
    else
      raise
    end
  end
  stdout = Base64.decode64(res["Stdout"]).strip
  exitcode = res['Exitcode']
  if exitcode != 0
    stderr = Base64.decode64(res["Stderr"]).strip
    raise ModuleError.new(stdout: stdout, stderr: stderr, exitcode: exitcode)
  end
  stdout
end

.versionObject



34
35
36
# File 'lib/babl.rb', line 34

def self.version
  `#{bin_path} -version`.strip
end