Module: Overwatch::Helpers

Included in:
Command
Defined in:
lib/overwatch/helpers.rb

Instance Method Summary collapse

Instance Method Details

#default_headersObject



17
18
19
# File 'lib/overwatch/helpers.rb', line 17

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

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



45
46
47
48
49
50
51
# File 'lib/overwatch/helpers.rb', line 45

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



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

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

#fObject



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

def f
  @f ||= Formatador.new
end

#format_output(rows, options) ⇒ Object



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

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'
    puts JSON.generate(data)
  when 'none'
    return
  else
    puts Hirb::Helpers::Table.render(data, options)
  end

end

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



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/overwatch/helpers.rb', line 21

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



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/overwatch/helpers.rb', line 33

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