rturk 1.0.5 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitmodules +3 -0
- data/.yardoc +0 -0
- data/README.markdown +49 -29
- data/Rakefile +53 -0
- data/TODO.markdown +3 -0
- data/VERSION +1 -1
- data/examples/blank_slate.rb +21 -4
- data/examples/create_hit.rb +19 -0
- data/examples/newtweet.html +0 -1
- data/examples/review_answer.rb +18 -5
- data/lib/rturk.rb +33 -6
- data/lib/rturk/adapters/answers.rb +38 -0
- data/lib/rturk/adapters/assignment.rb +75 -0
- data/lib/rturk/adapters/hit.rb +97 -0
- data/lib/rturk/builders/qualification_builder.rb +68 -0
- data/lib/rturk/builders/qualifications_builder.rb +43 -0
- data/lib/rturk/builders/question_builder.rb +51 -0
- data/lib/rturk/errors.rb +5 -0
- data/lib/rturk/logger.rb +20 -0
- data/lib/rturk/macros.rb +35 -0
- data/lib/rturk/operation.rb +86 -0
- data/lib/rturk/operations/approve_assignment.rb +28 -0
- data/lib/rturk/operations/create_hit.rb +77 -0
- data/lib/rturk/operations/disable_hit.rb +19 -0
- data/lib/rturk/operations/dispose_hit.rb +19 -0
- data/lib/rturk/operations/force_expire_hit.rb +18 -0
- data/lib/rturk/operations/get_account_balance.rb +15 -0
- data/lib/rturk/operations/get_assignments_for_hit.rb +24 -0
- data/lib/rturk/operations/get_hit.rb +21 -0
- data/lib/rturk/operations/get_reviewable_hits.rb +23 -0
- data/lib/rturk/operations/grant_bonus.rb +26 -0
- data/lib/rturk/operations/reject_assignment.rb +23 -0
- data/lib/rturk/requester.rb +58 -76
- data/lib/rturk/response.rb +58 -0
- data/lib/rturk/responses/create_hit_response.rb +33 -0
- data/lib/rturk/responses/get_account_balance_response.rb +11 -0
- data/lib/rturk/responses/get_assignments_for_hit_response.rb +43 -0
- data/lib/rturk/responses/get_hit_response.rb +80 -0
- data/lib/rturk/responses/get_reviewable_hits_response.rb +33 -0
- data/lib/rturk/utilities.rb +19 -1
- data/lib/rturk/xml_utilities.rb +23 -0
- data/rturk.gemspec +143 -0
- data/spec/adapters/answers_spec.rb +27 -0
- data/spec/adapters/assignment_spec.rb +0 -0
- data/spec/adapters/hit_spec.rb +46 -0
- data/spec/builders/qualification_spec.rb +53 -0
- data/spec/builders/qualifications_spec.rb +30 -0
- data/spec/builders/question_spec.rb +46 -0
- data/spec/fake_responses/approve_assignment.xml +5 -0
- data/spec/fake_responses/create_hit.xml +12 -0
- data/spec/fake_responses/disable_hit.xml +5 -0
- data/spec/fake_responses/dispose_hit.xml +5 -0
- data/spec/fake_responses/force_expire_hit.xml +5 -0
- data/spec/fake_responses/get_account_balance.xml +10 -0
- data/spec/fake_responses/get_assignments.xml +35 -0
- data/spec/fake_responses/get_hit.xml +41 -0
- data/spec/fake_responses/get_reviewable_hits.xml +17 -0
- data/spec/fake_responses/grant_bonus.xml +5 -0
- data/spec/fake_responses/invalid_credentials.xml +12 -0
- data/spec/fake_responses/reject_assignment.xml +5 -0
- data/spec/operations/approve_assignment_spec.rb +27 -0
- data/spec/operations/create_hit_spec.rb +66 -0
- data/spec/operations/disable_hit_spec.rb +26 -0
- data/spec/operations/dispose_hit_spec.rb +26 -0
- data/spec/operations/force_expire_hit_spec.rb +26 -0
- data/spec/operations/get_account_balance_spec.rb +15 -0
- data/spec/operations/get_assignments_spec.rb +26 -0
- data/spec/operations/get_hit_spec.rb +18 -0
- data/spec/operations/get_reviewable_hits_spec.rb +0 -0
- data/spec/operations/grant_bonus_spec.rb +32 -0
- data/spec/operations/reject_assignment_spec.rb +26 -0
- data/spec/requester_spec.rb +7 -18
- data/spec/response_spec.rb +48 -0
- data/spec/rturk_spec.rb +27 -0
- data/spec/spec_helper.rb +28 -3
- data/spec/tmp +2 -0
- data/spec/xml_parse_spec.rb +32 -0
- metadata +94 -34
- data/examples/external_page.rb +0 -33
- data/lib/rturk/answer.rb +0 -20
- data/lib/rturk/custom_operations.rb +0 -80
- data/lib/rturk/external_question_builder.rb +0 -22
- data/spec/answer_spec.rb +0 -24
- data/spec/external_question_spec.rb +0 -27
data/examples/external_page.rb
DELETED
@@ -1,33 +0,0 @@
|
|
1
|
-
require '../lib/rturk'
|
2
|
-
|
3
|
-
props = {:Title=>"Write a twitter update",
|
4
|
-
:MaxAssignments=>1, :LifetimeInSeconds=>3600,
|
5
|
-
:Reward=>{:Amount=>0.10, :CurrencyCode=>"USD"},
|
6
|
-
:Keywords=>"twitter, blogging, writing, english",
|
7
|
-
:Description=>"Simply write a twitter update for me",
|
8
|
-
:RequesterAnnotation=>"OptionalNote",
|
9
|
-
:AssignmentDurationInSeconds=>3600, :AutoApprovalDelayInSeconds=>3600,
|
10
|
-
:QualificationRequirement=>[{
|
11
|
-
:QualificationTypeId=>"000000000000000000L0",
|
12
|
-
:IntegerValue=>90,
|
13
|
-
:Comparator=>"GreaterThan",
|
14
|
-
:RequiredToPreview=>"false"
|
15
|
-
}]
|
16
|
-
}
|
17
|
-
root = File.expand_path(File.dirname(__FILE__))
|
18
|
-
aws = YAML.load(File.open(File.join(root, 'mturk.yml')))
|
19
|
-
@turk = RTurk::Requester.new(aws['AWSAccessKeyId'], aws['AWSAccessKey'], :sandbox => true)
|
20
|
-
page = RTurk::ExternalQuestionBuilder.build("http://s3.amazonaws.com/mpercival.com/newtweet.html", :id => 'foo')
|
21
|
-
|
22
|
-
puts "*" * 80
|
23
|
-
puts "This is the XML created for the external page question \n #{page}"
|
24
|
-
|
25
|
-
puts "*" * 80
|
26
|
-
hit = @turk.create_hit(props, page)
|
27
|
-
puts "And the response from CreateHIT operation"
|
28
|
-
p hit
|
29
|
-
|
30
|
-
puts "*" * 80
|
31
|
-
puts "Created a new HIT which can be found at #{@turk.url_for_hit_type(hit['HIT']['HITTypeId'])}"
|
32
|
-
|
33
|
-
File.open(File.join(root ,"last_hit"), "w") {|f| f.write(hit['HIT']['HITId']) }
|
data/lib/rturk/answer.rb
DELETED
@@ -1,20 +0,0 @@
|
|
1
|
-
module RTurk
|
2
|
-
class Answer
|
3
|
-
|
4
|
-
def self.parse(xml)
|
5
|
-
answer = XmlSimple.xml_in(xml, {'ForceArray' => false})
|
6
|
-
response = {}
|
7
|
-
answers = answer['Answer']
|
8
|
-
answers = Array.new(1) { answers } unless answers.instance_of? Array
|
9
|
-
answers.each do |a|
|
10
|
-
question = a['QuestionIdentifier']
|
11
|
-
a.delete('QuestionIdentifier')
|
12
|
-
a.each_value do |v|
|
13
|
-
response[question] = v
|
14
|
-
end
|
15
|
-
end
|
16
|
-
response
|
17
|
-
end
|
18
|
-
|
19
|
-
end
|
20
|
-
end
|
@@ -1,80 +0,0 @@
|
|
1
|
-
module RTurk::CustomOperations
|
2
|
-
# Overides createHIT to allow for easier entry
|
3
|
-
def create_hit(props, page)
|
4
|
-
props = format_props(props)
|
5
|
-
props = props.merge(:Question => page, :Operation => 'CreateHIT')
|
6
|
-
request(props)
|
7
|
-
end
|
8
|
-
|
9
|
-
# Attempt to expire hit, then approve assignments, and finally dispose of
|
10
|
-
def kill_hit(hit_id)
|
11
|
-
forceExpireHIT(:HITId => hit_id)
|
12
|
-
get_assignments_for_hit(hit_id).each do |assignment|
|
13
|
-
approveAssignment(:AssignmentId => assignment[:AssignmentId])
|
14
|
-
end
|
15
|
-
disposeHIT(:HITId => hit_id)
|
16
|
-
end
|
17
|
-
|
18
|
-
# Wipe out all HIT's associated with this account
|
19
|
-
def blank_slate
|
20
|
-
search_response = searchHITs(:PageSize => 100)
|
21
|
-
if search_results = search_response['SearchHITsResult']['HIT']
|
22
|
-
search_results.each do |hit|
|
23
|
-
kill_hit(hit['HITId'])
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
|
29
|
-
def get_assignments_for_hit(hit)
|
30
|
-
response = request(:Operation => 'GetAssignmentsForHIT', :HITId => hit)
|
31
|
-
assignments = []
|
32
|
-
if response['GetAssignmentsForHITResult']['Assignment'].instance_of?(Array)
|
33
|
-
response['GetAssignmentsForHITResult']['Assignment'].each do |assignment|
|
34
|
-
answer = RTurk::Answer.parse(assignment['Answer'])
|
35
|
-
assignment.delete('Answer')
|
36
|
-
assignment['Answer'] = answer
|
37
|
-
assignments << assignment
|
38
|
-
end
|
39
|
-
else
|
40
|
-
if assignment = response['GetAssignmentsForHITResult']['Assignment']
|
41
|
-
answer = RTurk::Answer.parse(response['GetAssignmentsForHITResult']['Assignment']['Answer'])
|
42
|
-
assignment.delete('Answer')
|
43
|
-
assignment['Answer'] = answer
|
44
|
-
assignments << assignment
|
45
|
-
end
|
46
|
-
end
|
47
|
-
assignments
|
48
|
-
end
|
49
|
-
|
50
|
-
def url_for_hit(hit_id)
|
51
|
-
url_for_hit_type(getHIT(:HITId => hit_id)[:HITTypeId])
|
52
|
-
end
|
53
|
-
|
54
|
-
def url_for_hit_type(hit_type_id)
|
55
|
-
if @host =~ /sandbox/
|
56
|
-
"http://workersandbox.mturk.com/mturk/preview?groupId=#{hit_type_id}" # Sandbox Url
|
57
|
-
else
|
58
|
-
"http://mturk.com/mturk/preview?groupId=#{hit_type_id}" # Production Url
|
59
|
-
end
|
60
|
-
end
|
61
|
-
|
62
|
-
private
|
63
|
-
|
64
|
-
def format_props(params)
|
65
|
-
reward = params[:Reward]
|
66
|
-
qualifiers = params[:QualificationRequirement]
|
67
|
-
params.delete(:Reward)
|
68
|
-
params.delete(:QualificationRequirement)
|
69
|
-
params.merge!('Reward.1.Amount' => reward[:Amount], 'Reward.1.CurrencyCode' => reward[:CurrencyCode])
|
70
|
-
qualifiers.each_with_index do |qualifier, i|
|
71
|
-
params["QualificationRequirement.#{i+1}.QualificationTypeId"] = qualifier[:QualificationTypeId]
|
72
|
-
params["QualificationRequirement.#{i+1}.Comparator"] = qualifier[:Comparator]
|
73
|
-
params["QualificationRequirement.#{i+1}.IntegerValue"] = qualifier[:IntegerValue] if qualifier[:IntegerValue]
|
74
|
-
params["QualificationRequirement.#{i+1}.LocaleValue.Country"] = qualifier[:Country] if qualifier[:Country]
|
75
|
-
params["QualificationRequirement.#{i+1}.RequiredToPreview"] = qualifier[:RequiredToPreview]
|
76
|
-
end
|
77
|
-
params
|
78
|
-
end
|
79
|
-
|
80
|
-
end
|
@@ -1,22 +0,0 @@
|
|
1
|
-
module RTurk
|
2
|
-
class ExternalQuestionBuilder
|
3
|
-
|
4
|
-
|
5
|
-
def self.build(url, opts = {})
|
6
|
-
frame_height = opts[:frame_height] || 400
|
7
|
-
opts.delete(:frame_height)
|
8
|
-
querystring = opts.collect { |key, value| [CGI.escape(key.to_s), CGI.escape(value.to_s)].join('=') }.join('&')
|
9
|
-
url = opts.empty? ? url : "#{url}?#{querystring}"
|
10
|
-
xml = <<-XML
|
11
|
-
<ExternalQuestion xmlns="http://mechanicalturk.amazonaws.com/AWSMechanicalTurkDataSchemas/2006-07-14/ExternalQuestion.xsd">
|
12
|
-
<ExternalURL>#{url}</ExternalURL>
|
13
|
-
<FrameHeight>#{frame_height}</FrameHeight>
|
14
|
-
</ExternalQuestion>
|
15
|
-
XML
|
16
|
-
xml
|
17
|
-
end
|
18
|
-
|
19
|
-
end
|
20
|
-
|
21
|
-
|
22
|
-
end
|
data/spec/answer_spec.rb
DELETED
@@ -1,24 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
-
|
3
|
-
|
4
|
-
describe RTurk::Answer do
|
5
|
-
|
6
|
-
before(:all) do
|
7
|
-
@answer = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
|
8
|
-
<QuestionFormAnswers xmlns=\"http://mechanicalturk.amazonaws.com/AWSMechanicalTurkDataSchemas/2005-10-01/QuestionFormAnswers.xsd\">
|
9
|
-
<Answer>\n<QuestionIdentifier>tweet</QuestionIdentifier>\n<FreeText>Spec example</FreeText>\n</Answer>
|
10
|
-
<Answer>\n<QuestionIdentifier>Submit</QuestionIdentifier>\n<FreeText>Submit</FreeText>\n</Answer>
|
11
|
-
<Answer>\n<QuestionIdentifier>Foo</QuestionIdentifier>\n<RandomSelector>Bar</RandomSelector>\n</Answer>
|
12
|
-
</QuestionFormAnswers>\n"
|
13
|
-
@answer2 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
|
14
|
-
<QuestionFormAnswers xmlns=\"http://mechanicalturk.amazonaws.com/AWSMechanicalTurkDataSchemas/2005-10-01/QuestionFormAnswers.xsd\">
|
15
|
-
<Answer>\n<QuestionIdentifier>tweet</QuestionIdentifier>\n<FreeText>Spec example</FreeText>\n</Answer>
|
16
|
-
</QuestionFormAnswers>\n"
|
17
|
-
end
|
18
|
-
|
19
|
-
it "should parse an answer" do
|
20
|
-
RTurk::Answer.parse(@answer).should == {'Submit' => 'Submit', 'tweet' => 'Spec example', 'Foo' => 'Bar'}
|
21
|
-
RTurk::Answer.parse(@answer2).should == {'tweet' => 'Spec example'}
|
22
|
-
end
|
23
|
-
|
24
|
-
end
|
@@ -1,27 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
-
|
3
|
-
|
4
|
-
describe RTurk::ExternalQuestionBuilder do
|
5
|
-
|
6
|
-
|
7
|
-
it "should build a question with params" do
|
8
|
-
RTurk::ExternalQuestionBuilder.build('http://google.com/', :id => 'foo').should ==
|
9
|
-
<<-XML
|
10
|
-
<ExternalQuestion xmlns="http://mechanicalturk.amazonaws.com/AWSMechanicalTurkDataSchemas/2006-07-14/ExternalQuestion.xsd">
|
11
|
-
<ExternalURL>http://google.com/?id=foo</ExternalURL>
|
12
|
-
<FrameHeight>400</FrameHeight>
|
13
|
-
</ExternalQuestion>
|
14
|
-
XML
|
15
|
-
end
|
16
|
-
|
17
|
-
it "should build a question without params" do
|
18
|
-
RTurk::ExternalQuestionBuilder.build('http://google.com/').should ==
|
19
|
-
<<-XML
|
20
|
-
<ExternalQuestion xmlns="http://mechanicalturk.amazonaws.com/AWSMechanicalTurkDataSchemas/2006-07-14/ExternalQuestion.xsd">
|
21
|
-
<ExternalURL>http://google.com/</ExternalURL>
|
22
|
-
<FrameHeight>400</FrameHeight>
|
23
|
-
</ExternalQuestion>
|
24
|
-
XML
|
25
|
-
end
|
26
|
-
|
27
|
-
end
|