Module: Overwatch::Helpers
- Included in:
- Command
- Defined in:
- lib/overwatch/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
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 |
#f ⇒ Object
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, ) data = rows.is_a?(Array) ? rows : [rows] case [:format] when 'text' puts Hirb::Helpers::TextTable.render(data, ) when 'json' puts JSON.generate(data) when 'none' return else puts Hirb::Helpers::Table.render(data, ) 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, ={}, 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
33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/overwatch/helpers.rb', line 33 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 |