Class: CommonRestUtils

Inherits:
Object
  • Object
show all
Includes:
ConfigReader
Defined in:
lib/common.rb

Instance Method Summary collapse

Methods included from ConfigReader

#config_dir, #config_file, #mkdir, #param

Constructor Details

#initialize(uri_prefix) ⇒ CommonRestUtils

Returns a new instance of CommonRestUtils.



46
47
48
49
50
51
52
53
54
55
# File 'lib/common.rb', line 46

def initialize(uri_prefix)
  @uri_prefix = uri_prefix
  proxy = param("proxy")
  if proxy != ""
    RestClient.proxy = proxy
  end
  @headers = {:Content_type => "Application/json",
    :Authorization => "TOK:" + param("token"),
    :Accept => "Application/json"}
end

Instance Method Details

#callMethod(url_suffix, http_method, body = nil) ⇒ Object



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
40
41
42
43
44
# File 'lib/common.rb', line 7

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 Exception => e
    puts "Unable to connect to REST server " + param("rest_base_url")
    if $debug
      puts "full URL is:" + url
      if (body)
        puts "body is: json_body"
      end
    end
    raise e.message + " (" + e.class.to_s + ")"
  end
  if ( ! response.nil? && response.respond_to?(:code) && response.code != 200)
    raise "Bad response code:" + response.code.to_s
  end
  if (response.length > 0)
    JSON.parse response
  else
    nil
  end
end