Class: CommonRestUtils
- Inherits:
-
Object
- Object
- CommonRestUtils
- Defined in:
- lib/common.rb
Instance Method Summary collapse
- #callMethod(url_suffix, http_method, body = nil) ⇒ Object
-
#initialize(uri_prefix) ⇒ CommonRestUtils
constructor
A new instance of CommonRestUtils.
Constructor Details
#initialize(uri_prefix) ⇒ CommonRestUtils
Returns a new instance of CommonRestUtils.
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/common.rb', line 41 def initialize(uri_prefix) @uri_prefix = uri_prefix proxy = Configure.config_parameter("proxy") if proxy != "" RestClient.proxy = proxy end @headers = {:Content_type => "Application/json", :Authorization => "TOK:" + Configure.config_parameter("token"), :Accept => "Application/json"} end |
Instance Method Details
#callMethod(url_suffix, http_method, body = nil) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/common.rb', line 5 def callMethod(url_suffix, http_method, body=nil) url = @uri_prefix + url_suffix if (body) json_body = JSON.pretty_generate body end begin response = case http_method when :put RestClient.put(url, json_body, @headers) when :get RestClient.get(url, @headers) when :post RestClient.post(url, json_body, @headers) when :delete RestClient.(url, @headers) else "unsupported http method:" + http_method end rescue RestClient::Exception => e puts "URL is:" + url if (body) puts "body is: json_body" end raise("Exception occurred:" + e.) end if (response.code != 200) raise "Bad response code:" + response.code.to_s end if (response.length > 0) JSON.parse response else response end end |