Class: BaseTask

Inherits:
Object
  • Object
show all
Defined in:
lib/tasks/baseTask.rb

Overview

A psuedo abstract class that provides common functionality for performing Thing tasks. These are: retrieving a thing, updating status and sending a feed.

Direct Known Subclasses

FeedTask, StatusTask, ThingTask

Instance Method Summary collapse

Constructor Details

#initialize(webService, thing, isThingTask) ⇒ BaseTask

Constructor method for base task. Initialize web service and the thing for which tasks will be performed.

Parameters:

  • webService (WebService)

    A web service to be used by the task.

  • thing (Thing)

    The thing for which the task is being performed.

  • isThingTask (boolean)

    True if the task is retrieving or creating a thing.



15
16
17
18
19
# File 'lib/tasks/baseTask.rb', line 15

def initialize( webService, thing, isThingTask )
  @web = webService
  @thing = thing
  @isThingTask = isThingTask
end

Instance Method Details

#do_task_workObject

This method should be overridden by the subclass and does the specific task work.



64
65
66
# File 'lib/tasks/baseTask.rb', line 64

def do_task_work
  return { :response => 'error', :msg => 'Task work method not implemented.' }
end

#performObject

Perform the task.



24
25
26
27
28
29
30
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
# File 'lib/tasks/baseTask.rb', line 24

def perform
  response = {}
  #
  # Make sure thing is valid (id and code exists) before doing any work.  The exception of course is if we
  # are creating or retrieving a thing, in which case, id and code will not exist.
  #
  if( (@thing.id.to_s.length > 0 && @thing.code.length > 0) || @isThingTask )
    #
    # Do the task work.  If token has expired, then reauthorize and try again.
    #
    begin
      response = do_task_work()
      if( response[:response] == 'token_expired')
        @web.get_authorization()
        response = do_task_work()
      end
    #
    # On any exception, return an error response with the exception description in the message.
    #
    rescue
      response[:response] = 'error'
      response[:msg] = "#{$!.class} - #{$!.message}"
      @thing.last_error = response[:msg]
    end
  #
  # Thing has not been created.  Return the appropriate error and message.
  #
  else
    response[:response] = 'error'
    response[:msg] = "Thing '#{@thing.name}' has not been created."
  end
  #
  # Return the response.
  #
  return response
end