Class: Request

Inherits:
MESD
  • Object
show all
Defined in:
lib/me_sd.rb

Constant Summary

Constants inherited from MESD

MESD::EXCEPTIONS

Instance Attribute Summary

Attributes inherited from MESD

#curobj, #current_body, #last_error, #session

Instance Method Summary collapse

Methods inherited from MESD

#get_all_requests, #get_last_requests, #session_healthy?

Constructor Details

#initialize(args) ⇒ Request

Returns a new instance of Request.



241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/me_sd.rb', line 241

def initialize(args)
  if args[:id]
    @id = args[:id]
  elsif args[:url]
    if args[:url] =~ /WorkOrder\.do\?woMode=viewWO&woID=(?<ID>\d+)&&fromListView=true/
      @id = Regexp.last_match("ID")
    else
      return false
    end
  end
  @session = args[:session]
  true
end

Instance Method Details

#data(*args) ⇒ Object



255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
# File 'lib/me_sd.rb', line 255

def data(*args)
  unless self.id
    @last_error = "id error"
    return false
  end
  if args.size == 0
    only = []
  else
    only = args
  end
  unless session_healthy?(@session)
    @last_error = "session error"
    return false
  end
  props = [
    {
      name: :description,
      url: "WorkOrder.do?woMode=viewWO&woID=#{self.id}",
      search_function: {
        name: "value_between",
        args: ["<td style=\"padding-left:10px;\" colspan=\"3\" valign=\"top\" class=\"fontBlack textareadesc\">", "</td>"],
      },
      post_processing_functions: [:strip],
    },
    {
      name: :resolution,
      url: "AddResolution.do?mode=viewWOResolution&woID=#{self.id}",
      search_function: {
        name: "value_between",
        args: ["<td colspan=\"3\" valign=\"top\" class=\"fontBlack textareadesc\">", "</td>"],
      },
      post_processing_functions: [:strip],
    },
    {
      name: :status,
      url: "WorkOrder.do?woMode=viewWO&woID=#{self.id}",
      search_function: {
        name: "html_parse",
        args: [["css", "#WOHeaderSummary_DIV"], ["css", "#status_PH"], "text"],
      },
      post_processing_functions: [:semicolon_space_value, :symbolize],
    },
    {
      name: :priority,
      url: "WorkOrder.do?woMode=viewWO&woID=#{self.id}",
      search_function: {
        name: "html_parse",
        args: [["css", "#WOHeaderSummary_DIV"], ["css", "#priority_PH"], "text"],
      },
      post_processing_functions: [:semicolon_space_value, :symbolize],
    },
    {
      name: :author_name,
      url: "WorkOrder.do?woMode=viewWO&woID=#{self.id}",
      search_function: {
        name: "html_parse",
        args: [["css", "#requesterName_PH"], "text"],
      },
    },
    {
      name: :create_date,
      url: "WorkOrder.do?woMode=viewWO&woID=#{self.id}",
      search_function: {
        name: "html_parse",
        args: [["css", "#CREATEDTIME_CUR"], "text"],
      },
      post_processing_functions: [:parse_date],
    },
    {
      name: :name,
      url: "WorkOrder.do?woMode=viewWO&woID=#{self.id}",
      search_function: {
        name: "html_parse",
        args: [["css", "#requestSubject_ID"], "text"],
      },
      post_processing_functions: [:strip],
    },
  ]
  props.each do |property|
    next if !only.empty? && !only.include?(property[:name])
    uri = URI("http://#{@session[:host]}:#{@session[:port]}/#{property[:url]}")
    begin
      Net::HTTP.start(uri.host, uri.port) do |http|
        http_request = Net::HTTP::Get.new(uri)
        http_request.add_field("Cookie", "#{@session[:cookie]}")
        http_request = http.request(http_request)
        @current_body = http_request.response.body
        auth_error_pos = @current_body.index("AuthError")
        if auth_error_pos
          @last_error = "auth error"
          return false
        end
        permitions_error_pos = @current_body.index("Request does not fall under your permitted scope")
        if permitions_error_pos
          @last_error = "no permitions error"
          return false
        end
        operational_error_pos = @current_body.index("failurebox")
        if operational_error_pos
          @last_error = "operational error"
          return false
        end
        value = self.method(property[:search_function][:name]).call(property[:search_function][:args])
        if property[:post_processing_functions]
          functions = property[:post_processing_functions]
          functions.each do |function|
            if value.methods.include?(function)
              value = value.method(function).call
            elsif self.private_methods.include?(function)
              value = self.method(function).call(value)
            end
          end
        end
        self.send("#{property[:name]}=", value)
      end
    rescue *EXCEPTIONS => @last_error
    end
  end
  self
end