Class: QuickbooksWebConnector::Job

Inherits:
Object
  • Object
show all
Defined in:
lib/quickbooks_web_connector/job.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(payload) ⇒ Job

Returns a new instance of Job.



6
7
8
# File 'lib/quickbooks_web_connector/job.rb', line 6

def initialize(payload)
  @payload = payload
end

Instance Attribute Details

#response_xmlObject

Returns the value of attribute response_xml.



4
5
6
# File 'lib/quickbooks_web_connector/job.rb', line 4

def response_xml
  @response_xml
end

Class Method Details

.create(request_builder, response_handler, *args) ⇒ Object

Creates a job by placing it on the queue. Expects a request builder class name, a response handler class name, and an optional array of arguments to pass to the class’ ‘perform` method.

Raises an exception if no class is given.



15
16
17
18
19
20
21
# File 'lib/quickbooks_web_connector/job.rb', line 15

def self.create(request_builder, response_handler, *args)
  QuickbooksWebConnector.push(
    'request_builder_class' => request_builder.to_s,
    'response_handler_class' => response_handler.to_s,
    'args' => args
  )
end

.destroy(request_builder, response_handler, *args) ⇒ Object

Destroys a job on the queue. Expects a request builder class name, a response handler class name, and an optional array of arguments to pass to the class’ ‘perform` method.



26
27
28
29
30
31
32
# File 'lib/quickbooks_web_connector/job.rb', line 26

def self.destroy(request_builder, response_handler, *args)
  QuickbooksWebConnector.remove(
    'request_builder_class' => request_builder.to_s,
    'response_handler_class' => response_handler.to_s,
    'args' => args
  )
end

.peekObject

Return an instance of QuickbooksWebConnector::job if any jobs are available, without removing the job from the queue



43
44
45
46
# File 'lib/quickbooks_web_connector/job.rb', line 43

def self.peek
  return unless payload = QuickbooksWebConnector.peek
  new(payload)
end

.queuedObject

Find jobs from the queue.

Returns the list of jobs queued.

This method can be potentially very slow and memory intensive, depending on the size of your queue, as it loads all jobs into a Ruby array.



55
56
57
58
59
# File 'lib/quickbooks_web_connector/job.rb', line 55

def self.queued
  QuickbooksWebConnector.list_range(:queue, 0, -1).map do |item|
    new(item)
  end
end

.reserveObject

Returns an instance of QuickbooksWebConnector::Job if any jobs are available. If not, returns nil.



36
37
38
39
# File 'lib/quickbooks_web_connector/job.rb', line 36

def self.reserve
  return unless payload = QuickbooksWebConnector.pop
  new(payload)
end

Instance Method Details

#argsObject

Returns an array of args represented in this job’s payload.



96
97
98
# File 'lib/quickbooks_web_connector/job.rb', line 96

def args
  @payload['args']
end

#fail(exception) ⇒ Object

Given an exception object, hands off the needed parameters to the Failure module.



106
107
108
109
110
111
# File 'lib/quickbooks_web_connector/job.rb', line 106

def fail(exception)
  Failure.create(
    payload: @payload,
    exception: exception
  )
end

#job_argsObject



100
101
102
# File 'lib/quickbooks_web_connector/job.rb', line 100

def job_args
  args || []
end

#performObject

Attempts to perform the work represented by this job instance. Calls #perform on the class given in the payload with the Quickbooks response and the arguments given in the payload..



64
65
66
67
68
69
70
71
72
73
# File 'lib/quickbooks_web_connector/job.rb', line 64

def perform
  begin
    job = response_handler_class

    # Execute the job.
    job.perform(response_xml, *job_args)
  rescue Object => ex
    fail(ex)
  end
end

#request_builder_classObject

Returns the actual class constant for building the request from the job’s payload.



86
87
88
# File 'lib/quickbooks_web_connector/job.rb', line 86

def request_builder_class
  @request_builder_class ||= @payload['request_builder_class'].constantize
end

#request_xmlObject

Returns the request XML from the payload.



76
77
78
79
80
81
82
83
# File 'lib/quickbooks_web_connector/job.rb', line 76

def request_xml
  begin
    request_builder_class.perform(*job_args)
  rescue Object => ex
    fail(ex)
    nil
  end
end

#response_handler_classObject

Returns the actual class constant represented in this job’s payload.



91
92
93
# File 'lib/quickbooks_web_connector/job.rb', line 91

def response_handler_class
  @response_handler_class ||= @payload['response_handler_class'].constantize
end