Class: NetUtil

Inherits:
Object
  • Object
show all
Defined in:
lib/utils/net_util.rb

Constant Summary collapse

READ_TIMEOUT =

10 minutes

600
RETRY_TIMES =
3
WAIT_TIME =

Wait for 5 seconds before retry

5

Class Method Summary collapse

Class Method Details

.build_header(options) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/utils/net_util.rb', line 73

def NetUtil.build_header(options)
  headers = (options[:headers] || { 'Content-Type' => 'application/json' }).dup
  conntent_type = ( headers['Content-Type'].nil? )? 'application/json' : headers['Content-Type']
  headers.delete('Content-Type')
  {'Content-Type' => conntent_type}.merge(headers)

end

.call_webservices(url, method_name = 'get', data = '', options = { headers: {'Content-Type' => 'application/json'} }) ⇒ Object

This method performs GET, PUT and POST requests to web services Call it like this: response = NetUtil.call_web_services(url) <= This will perform a GET, with url provided by the caller response = NetUtil.call_web_services(url, ‘post’, doc) <= This will perform a POST, doc is the data to post, can be REXML::Document or XML String In the case of GET, the returned XML data is ‘response’ In the case of POST and PUT, the returned XML data is ‘response.body’



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/utils/net_util.rb', line 16

def NetUtil.call_webservices(url, method_name = 'get', data = '', options = { headers: {'Content-Type' => 'application/json'} })
  method_name = method_name.to_s.downcase
  try_time = 0
  begin
    NetUtil.send("do_#{method_name}", {url: url, data: data}.merge(options))
  rescue StandardError => error
    try_time += 1
    if try_time > RETRY_TIMES
      puts ("\n#{Time.now} Unrecoverable error in NetUtil.call_webservices: "\
                                 "#{error}\n#{error.backtrace.join("\n")}\n")
      # It is an unrecoverable error, throw the exception back, don't suppress it.
      raise "Unrecoverable error calling web services.\nURL: #{url}.\nError message: #{error.message}." 
    end
    
    puts ("NetUtil.call_webservices #{url}:\nError happens: #{error}. Try #{try_time} time(s).")
    sleep(WAIT_TIME)
    retry
  end
end

.do_get(options) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/utils/net_util.rb', line 36

def NetUtil.do_get(options)
  # headers   = {'Content-Type' => 'text/xml'}
  headers   = build_header(options)
  url       = options[:url]
  uri       = URI.parse(url)
  req       = Net::HTTP.new(uri.host, uri.port)
  req = set_ssl(req, url)
  response = req.request_get(uri.path, headers)
  return response.body
end

.do_post(options) ⇒ Object



47
48
49
# File 'lib/utils/net_util.rb', line 47

def NetUtil.do_post(options)
  run_p(options, 'post')
end

.do_put(options) ⇒ Object



51
52
53
# File 'lib/utils/net_util.rb', line 51

def NetUtil.do_put(options)
  run_p(options, 'put')
end

.run_p(options, method_name) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/utils/net_util.rb', line 56

def NetUtil.run_p(options, method_name)
  data = options[:data].to_s
  
  headers   = build_header(options)
  url       = options[:url]
  uri       = URI.parse(url)
  req       = Net::HTTP.new(uri.host, uri.port)
  req       = set_ssl(req, url)
  
  req.read_timeout  = READ_TIMEOUT
  req.open_timeout  = READ_TIMEOUT
  
  response, body   = req.send(method_name, uri.path, data, headers)
  
  return response
end

.set_ssl(request, url) ⇒ Object



81
82
83
84
85
86
87
# File 'lib/utils/net_util.rb', line 81

def NetUtil.set_ssl(request, url)
  if url.start_with? 'https'
    request.use_ssl = true 
    request.verify_mode = OpenSSL::SSL::VERIFY_NONE
  end
  request
end