shared_workforce 0.1.2 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,121 +3,78 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
3
3
  describe "TaskRequest::Http" do
4
4
  describe "#create" do
5
5
  it "should invoke a task request" do
6
-
7
- task = SharedWorkforce::Task.define "Approve photo" do |h|
8
-
9
- h.directions = "Please classify this photo by choosing the appropriate tickboxes."
10
- h.image_url = "http://www.google.com/logo.png"
11
- h.answer_type = "tags"
12
- h.answer_options = ['Obscenity', 'Nudity', 'Blurry', 'Upside down or sideways', 'Contains more than one person in the foreground', 'Has people in the background', 'Contains children']
13
- h.responses_required = 3
14
- h.replace = false
15
6
 
16
- h.on_completion do |result|
17
- puts "Complete"
18
- end
19
-
20
- h.on_failure do |result|
21
- puts "Failed"
22
- end
23
-
24
- end
25
-
26
- task_request = SharedWorkforce::TaskRequest::Http.new(task, :callback_params=>{:resource_id=>'1234'})
7
+ task = ApprovePhotoTask.new
8
+ task_request = SharedWorkforce::TaskRequest::Http.new(task, :image_url=>"http://www.google.com/logo.png", :callback_params=>{:resource_id=>'1234'})
27
9
 
28
10
  stub_request(:post, "api.sharedworkforce.com/tasks")
29
11
  task_request.create
30
12
  a_request(:post, "http://api.sharedworkforce.com/tasks").with(:body=>{'task'=>
31
13
  {
32
- 'name'=>"Approve photo",
33
- 'directions'=>"Please classify this photo by choosing the appropriate tickboxes.",
14
+ 'title'=>"Approve Photo",
15
+ 'instruction'=>"Please classify this photo by choosing the appropriate tickboxes.",
34
16
  'image_url'=>"http://www.google.com/logo.png",
35
17
  'answer_options'=> ['Obscenity', 'Nudity', 'Blurry', 'Upside down or sideways', 'Contains more than one person in the foreground', 'Has people in the background', 'Contains children'],
36
18
  'responses_required'=>3,
37
19
  'answer_type'=>'tags',
38
20
  'callback_url'=>"#{SharedWorkforce.configuration.callback_host}/hci_task_result",
39
21
  'callback_params'=>{'resource_id'=>'1234'},
40
- 'replace'=>false
22
+ 'replace'=>true
41
23
  }, :api_key=>'test-api-key'}).should have_been_made.once
42
24
 
43
25
  end
44
26
 
45
- it "should allow options to be overridden when making the request" do
46
-
47
- task = SharedWorkforce::Task.define "Approve photo" do |h|
48
-
49
- h.directions = "Please classify this photo by choosing the appropriate tickboxes."
50
- h.image_url = "http://www.google.com/logo.png"
51
- h.answer_type = "tags"
52
- h.answer_options = ['Obscenity', 'Nudity', 'Blurry', 'Upside down or sideways', 'Contains more than one person in the foreground', 'Has people in the background', 'Contains children']
53
- h.responses_required = 3
54
-
55
- h.on_completion do |result|
56
- puts "Complete"
57
- end
58
-
59
- h.on_failure do |result|
60
- puts "Failed"
61
- end
62
-
63
- end
27
+ it "should allow options to be overridden when making the request" do
28
+ task = Class.new do
29
+ include SharedWorkforce::Task
30
+
31
+ title 'Approve Photo'
32
+ instruction 'Please classify this photo by choosing the appropriate tickboxes.'
33
+ responses_required 3
34
+ image_url "http://www.google.com/logo.png"
35
+ answer_type :tags
36
+ replace false
37
+
38
+ answer_options ['Obscenity', 'Nudity', 'Blurry', 'Upside down or sideways', 'Contains more than one person in the foreground', 'Has people in the background', 'Contains children']
39
+ end
64
40
 
65
- task_request = SharedWorkforce::TaskRequest::Http.new(task, {:callback_params=>{:resource_id=>'1234'}, :image_url=>"http://www.example.com/image.jpg"})
41
+ task_request = SharedWorkforce::TaskRequest::Http.new(task.new, {:callback_params=>{:resource_id=>'1234'}, :image_url=>"http://www.example.com/image.jpg"})
66
42
 
67
- stub_request(:post, "api.sharedworkforce.com/tasks")
68
- task_request.create
69
- a_request(:post, "http://api.sharedworkforce.com/tasks").with(:body=>{'task'=>
70
- {
71
- 'name'=>"Approve photo",
72
- 'directions'=>"Please classify this photo by choosing the appropriate tickboxes.",
73
- 'image_url'=>"http://www.example.com/image.jpg",
74
- 'answer_options'=> ['Obscenity', 'Nudity', 'Blurry', 'Upside down or sideways', 'Contains more than one person in the foreground', 'Has people in the background', 'Contains children'],
75
- 'responses_required'=>3,
76
- 'answer_type'=>'tags',
77
- 'callback_url'=>"#{SharedWorkforce.configuration.callback_host}/hci_task_result",
78
- 'callback_params'=>{'resource_id'=>'1234'},
79
- 'replace'=>false,
80
- }, :api_key=>'test-api-key'}).should have_been_made.once
43
+ stub_request(:post, "api.sharedworkforce.com/tasks")
44
+ task_request.create
45
+ a_request(:post, "http://api.sharedworkforce.com/tasks").with(:body=>{'task'=>
46
+ {
47
+ 'title'=>"Approve Photo",
48
+ 'instruction'=>"Please classify this photo by choosing the appropriate tickboxes.",
49
+ 'image_url'=>"http://www.example.com/image.jpg",
50
+ 'answer_options'=> ['Obscenity', 'Nudity', 'Blurry', 'Upside down or sideways', 'Contains more than one person in the foreground', 'Has people in the background', 'Contains children'],
51
+ 'responses_required'=>3,
52
+ 'answer_type'=>'tags',
53
+ 'callback_url'=>"#{SharedWorkforce.configuration.callback_host}/hci_task_result",
54
+ 'callback_params'=>{'resource_id'=>'1234'},
55
+ 'replace'=>false,
56
+ }, :api_key=>'test-api-key'}).should have_been_made.once
81
57
  end
82
58
  end
83
59
 
84
60
  describe "#cancel" do
85
61
  it "should invoke a task cancellation" do
86
62
 
87
- task = SharedWorkforce::Task.define "Approve photo" do |h|
88
-
89
- h.directions = "Please classify this photo by choosing the appropriate tickboxes."
90
- h.image_url = "http://www.google.com/logo.png"
91
- h.answer_type = "tags"
92
- h.answer_options = ['Obscenity', 'Nudity', 'Blurry', 'Upside down or sideways', 'Contains more than one person in the foreground', 'Has people in the background', 'Contains children']
93
- h.responses_required = 3
94
- h.replace = false
95
-
96
- h.on_completion do |result|
97
- puts "Complete"
98
- end
99
-
100
- h.on_failure do |result|
101
- puts "Failed"
102
- end
103
-
104
- end
105
-
106
- task_request = SharedWorkforce::TaskRequest::Http.new(task, :callback_params=>{:resource_id=>'1234'})
63
+ task_request = SharedWorkforce::TaskRequest::Http.new(ApprovePhotoTask.new, :callback_params=>{:resource_id=>'1234'})
107
64
 
108
65
  stub_request(:post, "api.sharedworkforce.com/tasks/cancel")
109
66
  task_request.cancel
110
67
  a_request(:post, "http://api.sharedworkforce.com/tasks/cancel").with(:body=>{'task'=>
111
68
  {
112
- 'name'=>"Approve photo",
113
- 'directions'=>"Please classify this photo by choosing the appropriate tickboxes.",
69
+ 'title'=>"Approve Photo",
70
+ 'instruction'=>"Please classify this photo by choosing the appropriate tickboxes.",
114
71
  'image_url'=>"http://www.google.com/logo.png",
115
72
  'answer_options'=> ['Obscenity', 'Nudity', 'Blurry', 'Upside down or sideways', 'Contains more than one person in the foreground', 'Has people in the background', 'Contains children'],
116
73
  'responses_required'=>3,
117
74
  'answer_type'=>'tags',
118
75
  'callback_url'=>"#{SharedWorkforce.configuration.callback_host}/hci_task_result",
119
76
  'callback_params'=>{'resource_id'=>'1234'},
120
- 'replace'=>false
77
+ 'replace'=>true
121
78
  }, :api_key=>'test-api-key'}).should have_been_made.once
122
79
 
123
80
  end
@@ -2,36 +2,30 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
 
3
3
  describe "TaskResult" do
4
4
  describe "#process!" do
5
- it "should invoke a tasks complete callback" do
6
-
7
- resources = {}
8
- resources['1'] = OpenStruct.new(:approved=>false)
9
- resources['2'] = OpenStruct.new(:approved=>false)
10
- resources['3'] = OpenStruct.new(:approved=>false)
11
-
12
- task = SharedWorkforce::Task.define("Approve photo") do |h|
13
-
14
- h.on_completion do |result|
15
- if result.responses.first.answer == 'yes'
16
- resources[result.callback_params['resource_id']].approved = true
17
- end
18
- end
19
-
20
- end
21
-
22
- SharedWorkforce::TaskResult.new({'callback_params'=>{'resource_id' => '2'}, 'responses'=>[{'answer'=>'yes'}], 'name'=>"Approve photo"}).process!
23
-
24
- resources['1'].approved.should == false
25
- resources['2'].approved.should == true
26
- resources['3'].approved.should == false
27
-
5
+ it "should invoke Task#process_result" do
6
+ ApprovePhotoTask.any_instance.should_receive(:process_result).once
7
+ SharedWorkforce::TaskResult.new({'callback_params'=>{'_task'=>{'class_name'=>'ApprovePhotoTask'}, 'resource_id' => '2', 'responses'=>[{'answer'=>'yes'}], 'name'=>"Approve photo please"}}).process!
28
8
  end
29
9
  end
30
10
 
31
- describe "#answers" do
32
- it "should collect the answers" do
33
- r = SharedWorkforce::TaskResult.new({'callback_params'=>{'resource_id' => '2'}, 'responses'=>[{'answer'=>'yes'}, {'answer'=>'no'}, {'answer'=>'yes'}], 'name'=>"Approve photo"})
34
- r.answers.should == ['yes', 'no', 'yes']
11
+ describe "#responses" do
12
+ it "should convert the result to a hash" do
13
+ r = SharedWorkforce::TaskResult.new(
14
+ {'callback_params'=>{'resource_id' => '2'},
15
+ 'responses'=>[
16
+ {'answer'=>'yes', 'username'=>'bilbo'},
17
+ {'answer'=>'no', 'username'=>'frodo'},
18
+ {'answer'=>'yes', 'username'=>'sam'}
19
+ ],
20
+ 'name'=>"Approve photo"
21
+ }
22
+ )
23
+
24
+ r.responses.should == [
25
+ {:answer=>'yes', :answered_by=>'bilbo'},
26
+ {:answer=>'no', :answered_by=>'frodo'},
27
+ {:answer=>'yes', :answered_by=>'sam'}
28
+ ]
35
29
  end
36
30
  end
37
31
 
@@ -41,4 +35,5 @@ describe "TaskResult" do
41
35
  r.usernames.should == ['bilbo', 'frodo', 'sam']
42
36
  end
43
37
  end
38
+
44
39
  end
data/spec/task_spec.rb CHANGED
@@ -1,112 +1,327 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+ require 'ostruct'
2
3
 
3
4
  describe "Task" do
4
- it "should define a task" do
5
-
6
- SharedWorkforce::Task.define "Approve photo" do |h|
7
-
8
- h.directions = "Please classify this photo by choosing the appropriate tickboxes."
9
- h.image_url = "http://www.google.com/logo.png"
10
- h.answer_options = ['Obscenity', 'Nudity', 'Blurry', 'Upside down or sideways', 'Contains more than one person in the foreground', 'Has people in the background', 'Contains children']
11
- h.responses_required = 3
12
-
13
- h.on_completion do |result|
5
+
6
+ before(:each) do
7
+ @resource_class = Class.new { def self.name; "Resource"; end; def self.find; end; def id; 333; end }
8
+ end
9
+
10
+ it "should define a task with default attributes" do
11
+ task_class = Class.new do
12
+ include SharedWorkforce::Task
13
+
14
+ title 'Approve Photo'
15
+ instruction 'Please classify this photo by choosing the appropriate tickboxes.'
16
+ responses_required 3
17
+ image_url "http://www.google.com/logo.png"
18
+
19
+ answer_options ['Obscenity', 'Nudity', 'Blurry', 'Upside down or sideways']
20
+
21
+ on_complete :puts_complete
22
+ on_failure :puts_failure
23
+
24
+ def puts_complete(results)
14
25
  puts "Complete"
15
26
  end
16
27
 
17
- h.on_failure do |result|
18
- puts "Failed"
28
+ def puts_failure(results)
29
+ puts "Failure"
19
30
  end
20
-
21
31
  end
22
32
 
23
- SharedWorkforce::Task.tasks.first[1].directions.should == "Please classify this photo by choosing the appropriate tickboxes."
24
-
33
+ task = task_class.new
34
+ task.instruction.should == "Please classify this photo by choosing the appropriate tickboxes."
35
+ task.title.should == "Approve Photo"
36
+ task.responses_required.should == 3
37
+ task.image_url.should == "http://www.google.com/logo.png"
38
+ task.answer_options.should == ['Obscenity', 'Nudity', 'Blurry', 'Upside down or sideways']
25
39
  end
26
-
27
- it "should run a completion callback" do
28
-
29
- object = OpenStruct.new(:test=>nil)
30
-
31
- task = SharedWorkforce::Task.define "Approve photo" do |h|
32
-
33
- h.on_completion do |result|
34
- object.test = "Complete"
40
+
41
+ it "should allow certain default attributes to be overwritten" do
42
+ task_class = Class.new do
43
+ include SharedWorkforce::Task
44
+
45
+ title 'Approve Photo'
46
+ instruction 'Please classify this photo by choosing the appropriate tickboxes.'
47
+ responses_required 3
48
+ image_url "http://www.google.com/logo.png"
49
+ replace false
50
+
51
+ answer_options ['Obscenity', 'Nudity', 'Blurry', 'Upside down or sideways']
52
+
53
+ text "A photo"
54
+
55
+ on_complete :puts_complete
56
+ on_failure :puts_failure
57
+
58
+ def puts_complete(results)
59
+ puts "Complete"
60
+ end
61
+
62
+ def puts_failure(results)
63
+ puts "Failure"
35
64
  end
36
-
37
65
  end
38
66
 
39
- object.test.should == nil
40
- task.complete!({})
41
- object.test.should == "Complete"
67
+ task = task_class.new
68
+
69
+ task.image_url = "http://www.bing.com"
70
+ task.answer_options = ['Poor quality']
71
+ task.responses_required = 10
72
+ task.instruction = nil
73
+ task.replace = true
74
+
75
+ task.image_url.should == "http://www.bing.com"
76
+ task.answer_options.should == ['Poor quality']
77
+ task.responses_required.should == 10
78
+ task.instruction.should == nil
79
+ task.replace.should == true
80
+ task.text.should == "A photo"
81
+ end
82
+
83
+ describe ".new" do
84
+ it "should set the attributes from the task result callback params" do
85
+ task_class = Class.new { include SharedWorkforce::Task; on_success :do_work; def do_work(*args); end; def setup(*args); end }
86
+ task_class.any_instance.should_receive(:success!)
87
+ task_class.any_instance.should_receive(:complete!)
88
+ task = task_class.new(SharedWorkforce::TaskResult.new({'callback_params'=>{'key'=>'value'}}))
89
+ task.attributes[:key].should == 'value'
90
+ end
91
+ end
92
+
93
+ describe ".create" do
94
+ it "should create a new task instance" do
95
+ resource = @resource_class.new
96
+ task_class = Class.new { include SharedWorkforce::Task }
97
+ task_class.any_instance.should_receive(:request)
98
+ task = task_class.create(resource, :field=>'name')
99
+ task.resource.should == resource
100
+ task.attributes[:field].should == 'name'
101
+ end
102
+
103
+ it "should pass the arguments through to Task#request" do
104
+ resource = @resource_class.new
105
+ task_class = Class.new { include SharedWorkforce::Task }
106
+ task_class.any_instance.should_receive(:request)
107
+ task_class.create(resource, :field=>'name')
108
+ end
42
109
  end
43
110
 
44
- it "should run a failure callback" do
45
-
46
- object = OpenStruct.new(:test=>nil)
111
+ describe "#process_result" do
112
+ it "should call success! and complete! with the results" do # Will become more important once we have task failure event handling
113
+ task_class = Class.new { include SharedWorkforce::Task; on_success :do_work; def do_work(*args); end; def setup(*args); end }
114
+ task = task_class.new
115
+ task.should_receive(:success!)
116
+ task.should_receive(:complete!)
117
+ task.process_result(SharedWorkforce::TaskResult.new({}))
118
+ end
119
+ end
120
+
121
+ describe "the callback" do
122
+
123
+ before(:each) do
124
+ @task_class = Class.new {
125
+ include SharedWorkforce::Task
126
+ on_success :do_work
127
+ on_complete :do_log
128
+ on_failure :try_again
129
+ def do_work(*args); end;
130
+ def setup(*args); end
131
+ }
132
+
133
+ @resource = @resource_class.new
134
+ @task = @task_class.new(@resource)
135
+ @result = SharedWorkforce::TaskResult.new(
136
+ {
137
+ 'responses'=>[
138
+ {'answer'=>'yes', 'username'=>'bilbo'},
139
+ {'answer'=>'no', 'username'=>'frodo'},
140
+ {'answer'=>'yes', 'username'=>'sam'}
141
+ ],
142
+ 'name'=>"Approve photo",
143
+ 'callback_params'=>@task.attributes
144
+ }
145
+ )
146
+ end
47
147
 
48
- task = SharedWorkforce::Task.define "Approve photo" do |h|
49
-
50
- h.on_failure do |result|
51
- object.test = "Failed"
148
+ describe "#success!" do
149
+ it "should run a success callback" do
150
+ @task.should_receive(:do_work)
151
+ @task.success!(@result)
152
+ end
153
+
154
+ it "should pass the resource to the callback method as the first argument and the result as the second argument" do
155
+ result = SharedWorkforce::TaskResult.new(:callback_params=>@task.attributes)
156
+ @task.should_receive(:do_work).with(@resource, @result.responses)
157
+ @task.success!(@result)
158
+ end
159
+ end
160
+
161
+ describe "#complete!" do
162
+ it "should run a complete callback" do
163
+ @task.should_receive(:do_log)
164
+ @task.complete!(@result)
165
+ end
166
+
167
+ it "should pass the resource to the callback method as the first argument and the result as the second argument" do
168
+ result = SharedWorkforce::TaskResult.new({})
169
+ @task.should_receive(:do_log).with(@resource, @result.responses)
170
+ @task.complete!(@result)
171
+ end
172
+ end
173
+
174
+ describe "#fail!" do
175
+ it "should run a failure callback" do
176
+ @task.should_receive(:try_again)
177
+ @task.fail!(@result)
178
+ end
179
+
180
+ it "should pass the resource to the callback method as the first argument and the result as the second argument" do
181
+ result = SharedWorkforce::TaskResult.new({})
182
+ @task.should_receive(:try_again).with(@resource, @result.responses)
183
+ @task.fail!(@result)
184
+ end
185
+
186
+ it "should not raise an error if there is no callback defined" do
187
+ lambda {
188
+ task = Class.new { include SharedWorkforce::Task }
189
+ task.new.fail!({})
190
+ }.should_not raise_error
52
191
  end
53
-
54
192
  end
55
-
56
- object.test.should == nil
57
- task.fail!({})
58
- object.test.should == "Failed"
59
193
  end
60
-
61
- it "should not raise an error if there is no callback defined" do
62
- lambda {
63
- task = SharedWorkforce::Task.define("Approve photo") { }
64
- task.fail!({})
65
- }.should_not raise_error
194
+
195
+ describe ".new" do
196
+ before do
197
+ @task_class = Class.new { include SharedWorkforce::Task; def setup(*args); end }
198
+ end
199
+
200
+ it "should call setup after initialization" do
201
+ @task_class.any_instance.should_receive(:setup).once
202
+ @task_class.new
203
+ end
204
+
205
+ it "should pass the resource as an argument to setup" do
206
+ @resource = @resource_class.new
207
+ @task_class.any_instance.should_receive(:setup).with(@resource).once
208
+ @task_class.new(@resource)
209
+ end
210
+
211
+ it "should set the callback params when passed as an argument" do
212
+ @resource = @resource_class.new
213
+ attributes = {:profile_field=>'introduction'}
214
+ task = @task_class.new(@resource, attributes)
215
+ task.attributes[:profile_field].should == 'introduction'
216
+ end
217
+
218
+ it "should symbolize the keys of the callback params when passed as an argument" do
219
+ @resource = @resource_class.new
220
+ attributes = {'profile_field'=>'introduction'}
221
+ task = @task_class.new(@resource, attributes)
222
+ task.attributes[:profile_field].should == 'introduction'
223
+ end
66
224
  end
67
-
68
- it "should request a task" do
69
- task = SharedWorkforce::Task.define("Approve photo") { }
70
- task.should_receive(:request).with(:request_id=>'123')
71
- SharedWorkforce::Task.request("Approve photo", :request_id=>'123')
225
+
226
+ describe ".attributes[]" do
227
+ it "should allow callback params to be set" do
228
+ task = Class.new { include SharedWorkforce::Task }.new(@resource_class.new, {})
229
+ task.attributes[:profile_field] = 'introduction'
230
+ task.attributes[:profile_field].should == 'introduction'
231
+ end
72
232
  end
73
-
74
- it "should cancel a task" do
75
- task = SharedWorkforce::Task.define("Approve photo") { }
76
- task.should_receive(:request).with(:request_id=>'123')
77
- SharedWorkforce::Task.request("Approve photo", :request_id=>'123')
78
-
79
- task.should_receive(:cancel).with(:request_id=>'123')
80
- SharedWorkforce::Task.cancel("Approve photo", :request_id=>'123')
233
+
234
+ describe "#request" do
235
+ it "should make a new task http request" do
236
+ task = Class.new { include SharedWorkforce::Task }
237
+
238
+ stub_request(:post, "http://api.sharedworkforce.com/tasks")
239
+ task.new.request(:request_id=>'123')
240
+ a_request(:post, "http://api.sharedworkforce.com/tasks").should have_been_made.once
241
+ end
81
242
  end
82
243
 
83
- it "should find a task" do
84
- SharedWorkforce::Task.define("Approve photo") { }
85
- SharedWorkforce::Task.define("Check profile text") { }
86
- SharedWorkforce::Task.define("Check content") { }
87
-
88
-
89
- SharedWorkforce::Task.find("Check profile text").name.should == "Check profile text"
244
+ describe "#cancel" do
245
+ it "should send a cancel task http request" do
246
+ task_class = Class.new { include SharedWorkforce::Task }
247
+
248
+ stub_request(:post, "http://api.sharedworkforce.com/tasks/cancel")
249
+ task_class.cancel(@resource_class.new)
250
+ a_request(:post, "http://api.sharedworkforce.com/tasks/cancel").should have_been_made.once
251
+ end
252
+
253
+ it "should raise a ConfigurationError if a callback host is not set" do
254
+ task = Class.new { include SharedWorkforce::Task }
255
+ with_configuration do |config|
256
+ config.callback_host = nil
257
+ lambda {
258
+ task.new.cancel(:request_id=>'123')
259
+ }.should raise_error SharedWorkforce::ConfigurationError
260
+ end
261
+ end
90
262
 
263
+ it "should raise a ConfigurationError if an API key is not set" do
264
+ task = Class.new { include SharedWorkforce::Task }
265
+ with_configuration do |config|
266
+ config.api_key = nil
267
+ lambda {
268
+ task.new.cancel(:request_id=>'123')
269
+ }.should raise_error SharedWorkforce::ConfigurationError
270
+ end
271
+ end
91
272
  end
92
-
93
- it "should raise a ConfigurationError if a callback host is not set" do
94
- SharedWorkforce::Task.define("Approve photo") {}
95
- with_configuration do |config|
96
- config.callback_host = nil
273
+
274
+ describe "#resource" do
275
+ it "should return the resource that passed to as an argument to new" do
276
+ task_class = Class.new { include SharedWorkforce::Task }
277
+ resource = @resource_class.new
278
+ task = task_class.new(resource)
279
+ task.resource.should == resource
280
+ end
281
+
282
+ it "should raise an ArgumentError if the resource does not respond to #find" do
283
+ task_class = Class.new { include SharedWorkforce::Task }
284
+ resource = double
97
285
  lambda {
98
- SharedWorkforce::Task.request("Approve photo", {})
99
- }.should raise_error SharedWorkforce::ConfigurationError
286
+ task = task_class.new(resource)
287
+ }.should raise_error ArgumentError
288
+ end
289
+
290
+ it "should return the resource from the callback params" do
291
+ class ResourceFinder; def self.find(id); return "#{id}ABCD"; end; end
292
+ task_class = Class.new { include SharedWorkforce::Task }
293
+ task = task_class.new(SharedWorkforce::TaskResult.new({'callback_params'=>{'_task'=>{'resource'=>{'class_name'=>'ResourceFinder', 'id' => '2'}}}}))
294
+ task.resource.should == "2ABCD"
295
+ end
296
+
297
+ it "should return nil if the callback params do not specify a resource" do
298
+ task_class = Class.new { include SharedWorkforce::Task }
299
+ task = task_class.new(SharedWorkforce::TaskResult.new({'callback_params'=>{}}))
300
+ task.resource.should == nil
100
301
  end
101
302
  end
102
-
103
- it "should raise a ConfigurationError if an API key is not set" do
104
- SharedWorkforce::Task.define("Approve photo") {}
105
- with_configuration do |config|
106
- config.api_key = nil
107
- lambda {
108
- SharedWorkforce::Task.request("Approve photo", {})
109
- }.should raise_error SharedWorkforce::ConfigurationError
303
+
304
+ describe "#to_hash" do
305
+ it "should include the class name and id of the resource in the callback params" do
306
+ task_class = Class.new { include SharedWorkforce::Task }
307
+ task = task_class.new(@resource_class.new)
308
+ task.to_hash[:callback_params][:_task][:resource][:id].should == 333
309
+ task.to_hash[:callback_params][:_task][:resource][:class_name].should == "Resource"
310
+ end
311
+
312
+ it "should include custom callback params in the callback params" do
313
+ task_class = Class.new { include SharedWorkforce::Task }
314
+ task = task_class.new(@resource_class.new, :profile_field=>'introduction')
315
+ task.to_hash[:callback_params][:_task][:resource][:id].should == 333
316
+ task.to_hash[:callback_params][:_task][:resource][:class_name].should == "Resource"
317
+ task.to_hash[:callback_params][:profile_field].should == 'introduction'
318
+ end
319
+
320
+ it "should include the class name of the task" do
321
+ task = ApprovePhotoTask.new(@resource_class.new, :profile_field=>'introduction')
322
+ task.to_hash[:callback_params][:_task][:resource][:id].should == 333
323
+ task.to_hash[:callback_params][:_task][:resource][:class_name].should == "Resource"
324
+ task.to_hash[:callback_params][:_task][:class_name].should == 'ApprovePhotoTask'
110
325
  end
111
326
  end
112
327
  end