Module: Overwatch::CLI::Helpers
- Defined in:
- lib/overwatch/cli/helpers.rb
Instance Method Summary collapse
- #default_headers ⇒ Object
- #delete(url, headers = {}) ⇒ Object
- #display_line(string) ⇒ Object
- #f ⇒ Object
- #format_output(rows, options) ⇒ Object
- #get(url, options = {}, headers = {}) ⇒ Object
- #post(url, payload = {}, options = {}, headers = {}) ⇒ Object
Instance Method Details
#default_headers ⇒ Object
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 |
#f ⇒ Object
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, ) data = rows.is_a?(Array) ? rows : [rows] case [:format] when 'text' puts Hirb::Helpers::TextTable.render(data, ) when 'json' pp JSON.generate(data) else puts Hirb::Helpers::Table.render(data, ) 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, ={}, headers={}) begin response = JSON.parse(RestClient.get(url, headers.merge!(default_headers))) format_output(response, ) rescue RestClient::Exception => e response = {:code => e.http_code.to_s, :errors => JSON.parse(e.http_body)} .merge!(:fields => [ :code, :errors ]) format_output(response, ) 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={}, ={}, headers={}) begin response = JSON.parse(RestClient.post(url, payload, headers.merge!(default_headers))) format_output(response, ) rescue RestClient::Exception => e response = {:code => e.http_code.to_s, :errors => JSON.parse(e.http_body)} [:fields] = [ :code, :errors ] format_output(response, ) exit 1 end end |