shared_workforce 0.2.9 → 0.2.10
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.
- data/lib/shared_workforce/frameworks/rails.rb +1 -0
- data/lib/shared_workforce/response_poller.rb +43 -20
- data/lib/shared_workforce/task_request/http_with_poller.rb +4 -1
- data/lib/shared_workforce/version.rb +1 -1
- data/spec/task_request/http_with_poller_spec.rb +33 -0
- data/spec/task_spec.rb +19 -1
- metadata +14 -12
@@ -1,30 +1,53 @@
|
|
1
1
|
module SharedWorkforce
|
2
2
|
class ResponsePoller
|
3
|
-
|
3
|
+
|
4
|
+
def self.start(interval=60)
|
5
|
+
new.start(interval)
|
6
|
+
end
|
7
|
+
|
8
|
+
def initialize
|
9
|
+
$stdout.sync = true
|
10
|
+
end
|
11
|
+
|
12
|
+
def start(interval)
|
4
13
|
Thread.abort_on_exception = true
|
5
14
|
Thread.new do
|
6
|
-
puts "SharedWorkforce: Checking every
|
15
|
+
puts "SharedWorkforce: Checking every #{interval} seconds for new responses."
|
16
|
+
|
7
17
|
while true
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
'X-API-Key'=>SharedWorkforce.configuration.api_key,
|
12
|
-
:content_type => :json,
|
13
|
-
:accept => :json
|
14
|
-
}))
|
15
|
-
if task['state'] == "completed"
|
16
|
-
puts "SharedWorkforce: Task complete. Getting responses."
|
17
|
-
responses = JSON.parse(RestClient.get("#{SharedWorkforce.configuration.http_end_point}/tasks/#{task_id}/responses", {
|
18
|
-
'X-API-Key'=>SharedWorkforce.configuration.api_key,
|
19
|
-
:content_type => :json,
|
20
|
-
:accept => :json
|
21
|
-
}))
|
22
|
-
puts "SharedWorkforce: Processing responses."
|
23
|
-
SharedWorkforce::TaskResult.new(responses).process!
|
24
|
-
break;
|
25
|
-
end
|
18
|
+
puts "SharedWorkforce: Checking for new task responses."
|
19
|
+
process_tasks completed_tasks
|
20
|
+
sleep interval
|
26
21
|
end
|
27
22
|
end
|
28
23
|
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def process_tasks(tasks)
|
28
|
+
tasks.each do |task|
|
29
|
+
if task['state'] == "completed"
|
30
|
+
puts "SharedWorkforce: Task complete. Getting responses."
|
31
|
+
responses = collect_responses(task['id'])
|
32
|
+
SharedWorkforce::TaskResult.new(responses).process!
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def completed_tasks
|
38
|
+
JSON.parse(RestClient.get("#{SharedWorkforce.configuration.http_end_point}/tasks/unreturned", {
|
39
|
+
'X-API-Key'=>SharedWorkforce.configuration.api_key,
|
40
|
+
:content_type => :json,
|
41
|
+
:accept => :json
|
42
|
+
}))
|
43
|
+
end
|
44
|
+
|
45
|
+
def collect_responses(id)
|
46
|
+
JSON.parse(RestClient.get("#{SharedWorkforce.configuration.http_end_point}/tasks/#{id}/responses/collect", {
|
47
|
+
'X-API-Key'=>SharedWorkforce.configuration.api_key,
|
48
|
+
:content_type => :json,
|
49
|
+
:accept => :json
|
50
|
+
}))
|
51
|
+
end
|
29
52
|
end
|
30
53
|
end
|
@@ -4,11 +4,14 @@ module SharedWorkforce
|
|
4
4
|
|
5
5
|
def create
|
6
6
|
task = JSON.parse(RestClient.post("#{http_end_point}/tasks", *request_params))
|
7
|
-
ResponsePoller.start(task['id'])
|
8
7
|
end
|
9
8
|
|
10
9
|
def cancel
|
11
10
|
RestClient.post("#{http_end_point}/tasks/cancel", *request_params)
|
12
11
|
end
|
12
|
+
|
13
|
+
def request_params
|
14
|
+
[{:task=>@task.to_hash.merge(@params).merge(:callback_url=>callback_url, :callback_enabled=>false), :api_key=>api_key}.to_json, {:content_type => :json, :accept => :json}]
|
15
|
+
end
|
13
16
|
end
|
14
17
|
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
2
|
+
|
3
|
+
describe "TaskRequest::HttpWithPoller" do
|
4
|
+
describe "#create" do
|
5
|
+
it "should disable callbacks" do
|
6
|
+
task = ApprovePhotoTask.new
|
7
|
+
task_request = SharedWorkforce::TaskRequest::HttpWithPoller.new(task,
|
8
|
+
:image_url=>"http://www.google.com/logo.png",
|
9
|
+
:image_crop_ratio=>1.7,
|
10
|
+
:callback_params=>{:resource_id=>'1234'}
|
11
|
+
)
|
12
|
+
|
13
|
+
stub_request(:post, "api.sharedworkforce.com/tasks").to_return(:body=>{:id=>123}.to_json)
|
14
|
+
|
15
|
+
task_request.create
|
16
|
+
|
17
|
+
a_request(:post, "http://api.sharedworkforce.com/tasks").with(:body=>{'task'=>
|
18
|
+
{
|
19
|
+
'title'=>"Approve Photo",
|
20
|
+
'instruction'=>"Please classify this photo by choosing the appropriate tickboxes.",
|
21
|
+
'image_url'=>"http://www.google.com/logo.png",
|
22
|
+
'image_crop_ratio'=>1.7,
|
23
|
+
'answer_options'=> ['Obscenity', 'Nudity', 'Blurry', 'Upside down or sideways', 'Contains more than one person in the foreground', 'Has people in the background', 'Contains children'],
|
24
|
+
'responses_required'=>3,
|
25
|
+
'answer_type'=>'tags',
|
26
|
+
'callback_url'=>"#{SharedWorkforce.configuration.callback_host}/shared_workforce/task_response",
|
27
|
+
'callback_params'=>{'resource_id'=>'1234'},
|
28
|
+
'replace'=>true,
|
29
|
+
'callback_enabled'=>false,
|
30
|
+
}, :api_key=>'test-api-key'}).should have_been_made.once
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
data/spec/task_spec.rb
CHANGED
@@ -255,7 +255,7 @@ describe "Task" do
|
|
255
255
|
a_request(:post, "http://api.sharedworkforce.com/tasks/cancel").should have_been_made.once
|
256
256
|
end
|
257
257
|
|
258
|
-
it "should raise a ConfigurationError if a callback host is not set" do
|
258
|
+
it "should not raise a ConfigurationError if a callback host is not set" do
|
259
259
|
task = Class.new { include SharedWorkforce::Task }
|
260
260
|
with_configuration do |config|
|
261
261
|
config.callback_host = nil
|
@@ -341,6 +341,24 @@ describe "Task" do
|
|
341
341
|
task.image_crop_ratio = 0.5
|
342
342
|
task.to_hash[:image_crop_ratio].should == 0.5
|
343
343
|
end
|
344
|
+
|
345
|
+
it "should include the callback url" do
|
346
|
+
with_configuration do |config|
|
347
|
+
config.callback_host = 'http://callback.example.com'
|
348
|
+
task = ApprovePhotoTask.new(@resource_class.new)
|
349
|
+
task.image_crop_ratio = 0.5
|
350
|
+
task.to_hash[:callback_url].should =~ %r{^http://callback.example.com/}
|
351
|
+
end
|
352
|
+
end
|
353
|
+
|
354
|
+
it "should set a nil callback url" do
|
355
|
+
with_configuration do |config|
|
356
|
+
config.callback_host = 'http://callback.example.com'
|
357
|
+
task = ApprovePhotoTask.new(@resource_class.new)
|
358
|
+
task.image_crop_ratio = 0.5
|
359
|
+
task.to_hash[:callback_url].should =~ %r{^http://callback.example.com/}
|
360
|
+
end
|
361
|
+
end
|
344
362
|
end
|
345
363
|
end
|
346
364
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: shared_workforce
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.10
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-07-20 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rest-client
|
16
|
-
requirement: &
|
16
|
+
requirement: &70320585942740 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70320585942740
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: json
|
27
|
-
requirement: &
|
27
|
+
requirement: &70320585942320 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70320585942320
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: activesupport
|
38
|
-
requirement: &
|
38
|
+
requirement: &70320585941900 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :runtime
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70320585941900
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: rspec
|
49
|
-
requirement: &
|
49
|
+
requirement: &70320585941400 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ! '>='
|
@@ -54,10 +54,10 @@ dependencies:
|
|
54
54
|
version: 1.2.9
|
55
55
|
type: :development
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *70320585941400
|
58
58
|
- !ruby/object:Gem::Dependency
|
59
59
|
name: webmock
|
60
|
-
requirement: &
|
60
|
+
requirement: &70320585940980 !ruby/object:Gem::Requirement
|
61
61
|
none: false
|
62
62
|
requirements:
|
63
63
|
- - ! '>='
|
@@ -65,7 +65,7 @@ dependencies:
|
|
65
65
|
version: '0'
|
66
66
|
type: :development
|
67
67
|
prerelease: false
|
68
|
-
version_requirements: *
|
68
|
+
version_requirements: *70320585940980
|
69
69
|
description: Shared Workforce is a service and simple API for human intelligence tasks
|
70
70
|
email:
|
71
71
|
- sam@samoliver.com
|
@@ -102,6 +102,7 @@ files:
|
|
102
102
|
- spec/support/configuration_helper.rb
|
103
103
|
- spec/task_request/black_hole_spec.rb
|
104
104
|
- spec/task_request/http_spec.rb
|
105
|
+
- spec/task_request/http_with_poller_spec.rb
|
105
106
|
- spec/task_response_spec.rb
|
106
107
|
- spec/task_result_spec.rb
|
107
108
|
- spec/task_spec.rb
|
@@ -138,6 +139,7 @@ test_files:
|
|
138
139
|
- spec/support/configuration_helper.rb
|
139
140
|
- spec/task_request/black_hole_spec.rb
|
140
141
|
- spec/task_request/http_spec.rb
|
142
|
+
- spec/task_request/http_with_poller_spec.rb
|
141
143
|
- spec/task_response_spec.rb
|
142
144
|
- spec/task_result_spec.rb
|
143
145
|
- spec/task_spec.rb
|