Module: Babl

Defined in:
lib/babl.rb,
lib/babl/module_response.rb

Defined Under Namespace

Classes: ModuleError, ModuleNameFormatIncorrectError, ModuleResponse, UnknownModuleError

Class Method Summary collapse

Class Method Details

.bin_pathObject



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/babl.rb', line 27

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
    STDERR.puts "Warn: Using locally installed binary '#{system}'"
    system
  end
end

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



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/babl.rb', line 54

def self.call! name, opts = {}
  begin
    ModuleResponse.new(client[:babl].call('Module', options_to_rpc_parameter(name, opts)))
  rescue Quartz::ResponseError => e
    if e.message == 'babl-rpc: module name format incorrect'
      raise ModuleNameFormatIncorrectError.new('Module Name Format Incorrect')
    elsif e.message =~ /^rpc error: code = 12 desc = unknown service/
      raise UnknownModuleError.new('Unknown Module')
    else
      raise
    end
  end
end

.clientObject



44
45
46
# File 'lib/babl.rb', line 44

def self.client
  @client ||= Quartz::Client.new(bin_path: bin_path)
end

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



48
49
50
51
52
# File 'lib/babl.rb', line 48

def self.module! name, opts = {}
  res = call! name, opts
  res.raise_exception_when_unsuccessful!
  res.stdout
end

.options_to_rpc_parameter(name, opts) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/babl.rb', line 68

def self.options_to_rpc_parameter name, opts
  params = {'Name' => name}
  if input = opts[:in]
    if input.is_a?(ModuleResponse)
      if input.payload_url
        params['PayloadUrl'] = input.payload_url
      else
        params['Stdin'] = Base64.encode64(input.stdout)
      end
    else
      params['Stdin'] = Base64.encode64(input)
    end
  end
  if opts[:payload_url]
    params['PayloadUrl'] = opts[:payload_url]
  end
  if opts[:env]
    params['Env'] = opts[:env].inject({}) { |h, (k,v)| h[k.to_s] = v.to_s; h }
  end
  if opts[:endpoint]
    params['BablEndpoint'] = opts[:endpoint]
  end
  params
end

.rpc_versionObject



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

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