Module: SharedWorkforce::Task

Defined in:
lib/shared_workforce/task.rb

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#attributesObject (readonly)

ends ClassMethods



68
69
70
# File 'lib/shared_workforce/task.rb', line 68

def attributes
  @attributes
end

Class Method Details

.included(base) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/shared_workforce/task.rb', line 4

def self.included(base)
  base.extend(ClassMethods)
  base.default_attributes(
    :title,
    :answer_type,
    :instruction,
    :guidelines,
    :responses_required,
    :replace,
    :answer_options,
    :image_url,
    :image_crop_ratio,
    :text,
    :html,
    :on_success,
    :on_failure,
    :on_complete
  )
end

Instance Method Details

#cancel(options = {}) ⇒ Object



121
122
123
124
# File 'lib/shared_workforce/task.rb', line 121

def cancel(options = {})
  task_request = remote_request(self, options)
  task_request.cancel
end

#complete!(result) ⇒ Object



103
104
105
# File 'lib/shared_workforce/task.rb', line 103

def complete!(result)
  send(@on_complete.to_sym, resource, result.responses) if @on_complete
end

#fail!(result) ⇒ Object



107
108
109
# File 'lib/shared_workforce/task.rb', line 107

def fail!(result)
  send(@on_failure.to_sym, resource, result.responses) if @on_failure
end

#initialize(resource_or_result = nil, attributes = nil) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/shared_workforce/task.rb', line 76

def initialize(resource_or_result=nil, attributes=nil)
  initialize_default_attributes
  if resource_or_result.is_a?(TaskResult)
    @result = resource_or_result
    process_result(@result)
  elsif resource_or_result
    unless resource_or_result.respond_to?(:id) && resource_or_result.class.respond_to?(:find)
      raise ArgumentError, "The resource you pass to new should respond to #id and it's class should respond to .find (or be an instance of ActiveRecord::Base) so it can be reloaded."
    end 
    @resource = resource_or_result
    initialize_attributes(attributes)
  end
  
  setup(resource) if respond_to?(:setup)
end

#initialize_default_attributesObject



70
71
72
73
74
# File 'lib/shared_workforce/task.rb', line 70

def initialize_default_attributes
  self.class.default_attributes.each do |name, value|
    instance_variable_set("@#{name}", value)
  end
end

#process_result(result) ⇒ Object



92
93
94
95
96
97
# File 'lib/shared_workforce/task.rb', line 92

def process_result(result)
  SharedWorkforce.logger.info("Shared Workforce: processing responses for #{title}")
  initialize_attributes(result.callback_params)
  success!(result)
  complete!(result)
end

#request(options = {}) ⇒ Object



115
116
117
118
119
# File 'lib/shared_workforce/task.rb', line 115

def request(options = {})
  SharedWorkforce.logger.info("Shared Workforce: creating task #{title}")
  task_request = remote_request(self, options)
  task_request.create
end

#resourceObject



111
112
113
# File 'lib/shared_workforce/task.rb', line 111

def resource
  @resource ||= find_resource
end

#success!(result) ⇒ Object



99
100
101
# File 'lib/shared_workforce/task.rb', line 99

def success!(result)
  send(@on_success.to_sym, resource, result.responses) if @on_success
end

#to_hashObject



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/shared_workforce/task.rb', line 126

def to_hash
  {
    :title => title,
    :instruction => instruction,
    :guidelines => guidelines,
    :image_url => image_url,
    :image_crop_ratio => image_crop_ratio,
    :answer_options => answer_options,
    :responses_required => responses_required,
    :answer_type => answer_type.to_s,
    :callback_url => callback_url,
    :replace => replace,
    :text => text,
    :html => html,
    :callback_params => attributes
  }.reject {|k,v| v.nil? }
end