Module: Overwatch::CLI::Helpers

Defined in:
lib/overwatch/cli/helpers.rb

Instance Method Summary collapse

Instance Method Details

#default_headersObject



12
13
14
# File 'lib/overwatch/cli/helpers.rb', line 12

def default_headers
  { 'Content-Type' => 'application/json', 'Accept' => 'application/json' }
end

#delete(url, headers = {}) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/overwatch/cli/helpers.rb', line 40

def delete(url, headers={})
  begin
    res = RestClient.delete(url, headers.merge!(default_headers))
    JSON.parse(res)
  rescue RestClient::Exception => e
  end
end

#display_line(string) ⇒ Object



8
9
10
# File 'lib/overwatch/cli/helpers.rb', line 8

def display_line(string)
  f.display_line(string)
end

#fObject



4
5
6
# File 'lib/overwatch/cli/helpers.rb', line 4

def f
  @f ||= Formatador.new
end

#format_output(rows, options) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/overwatch/cli/helpers.rb', line 48

def format_output(rows, options)
  data = rows.is_a?(Array) ? rows : [rows]
  
  case options[:format]
  when 'text'
    puts Hirb::Helpers::TextTable.render(data, options)
  when 'json'
    pp JSON.generate(data)
  else
    puts Hirb::Helpers::Table.render(data, options)
  end
end

#get(url, options = {}, headers = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/overwatch/cli/helpers.rb', line 16

def get(url, options={}, headers={})
  begin
    response = JSON.parse(RestClient.get(url, headers.merge!(default_headers)))
    format_output(response, options)
  rescue RestClient::Exception => e
    response = {:code => e.http_code.to_s, :errors => JSON.parse(e.http_body)}
    options.merge!(:fields => [ :code, :errors ])
    format_output(response, options)
    exit 1
  end
end

#post(url, payload = {}, options = {}, headers = {}) ⇒ Object



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

def post(url, payload={}, options={}, headers={})
  begin
    response = JSON.parse(RestClient.post(url, payload, headers.merge!(default_headers)))
    format_output(response, options)
  rescue RestClient::Exception => e
    response = {:code => e.http_code.to_s, :errors => JSON.parse(e.http_body)}
    options[:fields] = [ :code, :errors ]
    format_output(response, options)
    exit 1
  end
end