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



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

def NetUtil.build_header(options)
  headers = options[:headers] || { 'Content-Type' => 'application/json' }
  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’



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

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



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

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

  response = req.get(uri.path)
  return response.body
end

.do_post(options) ⇒ Object



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

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

.do_put(options) ⇒ Object



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

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

.run_p(options, method_name) ⇒ Object



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

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



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

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