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, #user_file

Constructor Details

#initialize(uri_prefix) ⇒ CommonRestUtils

Returns a new instance of CommonRestUtils.



42
43
44
45
46
47
# File 'lib/common.rb', line 42

def initialize(uri_prefix)
  @uri_prefix = uri_prefix
  @headers = {"Content-Type" => "Application/json",
    "Authorization" => "TOK:" + param("token"),
    "Accept" => "Application/json"}
end

Instance Method Details

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



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
# File 'lib/common.rb', line 9

def callMethod(url_suffix, http_method, body=nil)
  url = @uri_prefix + url_suffix
  if (body)
    json_body = JSON.pretty_generate body
  else
    json_body = nil
  end
  begin
    if ($mock_env)
      code, response = MockHttpSend.new($mock_env).send(url, http_method, @headers, json_body)
    else
      code, response = HttpSend.new.send(url, http_method, @headers, json_body)
    end
  rescue Exception => e
    puts "Exception thrown by HttpSend.new.send(); should not occur"
    puts e.message
    raise e.message + " (#{e.class})"
  end
  if code != 200
    puts "Problem invoking REST method at server #{param "rest_base_url"}"
    if code > 0
      raise "Bad http response code: #{code} --- response:#{response}"
    else
      raise response
    end
  end
  if (response.length >= 2)
    JSON.parse response
  else
    nil
  end
end