Class: Mqlight::Util

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/mqlight/util.rb

Class Method Summary collapse

Methods included from Logging

#logger, logger

Class Method Details

.generate_services(service, property_user, property_pass) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/mqlight/util.rb', line 94

def self.generate_services(service, property_user, property_pass)
  Logging.logger.entry(@id) { self.class.to_s + '#' + __method__.to_s }
  parms = Hash[method(__method__).parameters.map do |parm|
    [parm[1], eval(parm[1].to_s)]
  end]
  Logging.logger.parms(@id, parms) do
    self.class.to_s + '#' + __method__.to_s
  end

  # if 'service' param is an http(s) URI then fetch the service list from it
  if service.is_a?(String)
    uri = URI(service)
    if uri.scheme.eql?('http') || uri.scheme.eql?('https')
      service = get_service_urls(uri)
    end
  end
  service_uris = validate_services(service, property_user, property_pass)

  Logging.logger.exit(@id, [service_uris]) \
      { self.class.to_s + '#' + __method__.to_s }
  return service_uris
end

.get_service_urls(lookup_uri) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/mqlight/util.rb', line 118

def self.get_service_urls(lookup_uri)
  Logging.logger.entry(@id) { self.class.to_s + '#' + __method__.to_s }
  parms = Hash[method(__method__).parameters.map do |parm|
    [parm[1], eval(parm[1].to_s)]
  end]
  Logging.logger.parms(@id, parms) do
    self.class.to_s + '#' + __method__.to_s
  end

  fail ArgumentError, 'lookup_uri must be a String or URI' unless
    (lookup_uri.is_a?(String)) || (lookup_uri.is_a?(URI))
  res = http_get(URI(lookup_uri))
  fail Mqlight::NetworkError, "http request to #{lookup_uri} failed "\
    "with status code of #{res.code}" unless res.code == '200'
  result = JSON.parse(res.body)['service']
  Logging.logger.exit(@id, result) \
      { self.class.to_s + '#' + __method__.to_s }
  result
rescue => e
  Logging.logger.throw(nil, e) { self.class.to_s + '#' + __method__.to_s }
  raise e
end

.http_get(lookup_uri) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/mqlight/util.rb', line 142

def self.http_get(lookup_uri)
  Logging.logger.entry(@id) { self.class.to_s + '#' + __method__.to_s }
  parms = Hash[method(__method__).parameters.map do |parm|
    [parm[1], eval(parm[1].to_s)]
  end]
  Logging.logger.parms(@id, parms) do
    self.class.to_s + '#' + __method__.to_s
  end

  validate_uri_scheme(lookup_uri)
  Net::HTTP.start(lookup_uri.host, lookup_uri.port,
                  use_ssl: (lookup_uri.scheme == 'https')) do |http|
    path = lookup_uri.path
    path += '?' + lookup_uri.query if lookup_uri.query
    get = Net::HTTP::Get.new(path)
    http.request(get)
  end
rescue => e
  Logging.logger.throw(nil, e) { self.class.to_s + '#' + __method__.to_s }
  raise ArgumentError, "Could not access lookup details because #{e}"
end

.truncate(text) ⇒ Object



173
174
175
176
177
# File 'lib/mqlight/util.rb', line 173

def self.truncate(text)
  text
  text[0..200] + '... (truncated from ' + text.length.to_s + ')' \
    if text.length > 200
end

.validate_services(service, property_user, property_pass) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/mqlight/util.rb', line 31

def self.validate_services(service, property_user, property_pass)
  Logging.logger.entry(@id) { self.class.to_s + '#' + __method__.to_s }
  parms = Hash[method(__method__).parameters.map do |parm|
    [parm[1], eval(parm[1].to_s)]
  end]
  Logging.logger.parms(@id, parms) do
    self.class.to_s + '#' + __method__.to_s
  end

  property_auth = nil
  if property_user && property_pass
    property_auth = "#{URI.encode_www_form_component(property_user)}:"\
                    "#{URI.encode_www_form_component(property_pass)}"
  end

  service_strings = []

  # Convert argument into an array
  if service.is_a?(Array)
    service_strings = service
  elsif service.is_a?(String)
    service_strings << service
  end

  service_uris = []
  # For each entry convert to URI and validate.
  service_strings.each do |s|
    begin
      uri = URI(s)
    rescue
      raise ArgumentError, "#{s} is not a valid service"
    end
    fail ArgumentError, "#{s} is not a valid service" if uri.host.nil?
    next if uri.scheme.eql?('http') || uri.scheme.eql?('https')

    fail ArgumentError, "#{s} is not a supported scheme" \
      unless uri.scheme.eql?('amqp') || uri.scheme.eql?('amqps')

    if uri.userinfo
      fail ArgumentError,
           "URLs supplied via the 'service' property must specify both a "\
           'user name and a password value, or omit both values' unless
      uri.userinfo.split(':').size == 2
      fail ArgumentError,
           "User name supplied as an argument (#{property_auth}) does not"\
           ' match user name supplied via a service url'\
           "(#{uri.userinfo})" if
        property_auth && !(property_auth.eql? uri.userinfo)
    end

    fail ArgumentError,
         "One of the supplied services (#{uri}) #{uri.path} " \
         'is not a valid URL' \
         unless uri.path.nil? || uri.path.length == 0 \
           || uri.path == '/'

    service_uris << uri
  end
  Logging.logger.exit(@id, [service_uris]) \
      { self.class.to_s + '#' + __method__.to_s }
  return service_uris
end

.validate_uri_scheme(lookup_uri) ⇒ Object



165
166
167
168
# File 'lib/mqlight/util.rb', line 165

def self.validate_uri_scheme(lookup_uri)
  fail ArgumentError, 'lookup_uri must be a http or https URI.' unless
    (lookup_uri.scheme.eql? 'http') || (lookup_uri.scheme.eql? 'https')
end