cloudfactory 0.5.6 → 0.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,25 @@
1
+ require 'pry'
2
+ module Cf
3
+ module BadgeYamlValidator
4
+
5
+ def validate(yaml_path)
6
+ errors = []
7
+ badge_dump = YAML::load(File.read(yaml_path).strip)
8
+ if badge_dump.class == Hash
9
+ if badge_dump["badge"].class == Array
10
+ badge_dump["badge"].each do |badge|
11
+ errors << "form for the badge is missing" unless badge["form"]
12
+ errors << "name of the badge is missing" unless badge["name"]
13
+ errors << "description for the badge is missing" unless badge["description"]
14
+ errors << "known answers for the badge must be array of hashes" unless badge["known_answers"].class == Array
15
+ end
16
+ else
17
+ errors << "badge mut be array of hash"
18
+ end
19
+ else
20
+ errors << "badges must be hash"
21
+ end
22
+ errors
23
+ end
24
+ end
25
+ end
data/lib/cf/cli/line.rb CHANGED
@@ -239,20 +239,14 @@ module Cf # :nodoc: all
239
239
  number = worker['num_workers']
240
240
  reward = worker['reward']
241
241
  worker_type = worker['worker_type']
242
+
243
+ ##### Add badge to the station ########
244
+
242
245
  if worker_type == "human"
243
- skill_badges = worker['skill_badges']
246
+ badges = station_file['station']['badges']
244
247
  stat_badge = worker['stat_badge']
245
- if stat_badge.nil?
246
- human_worker = CF::HumanWorker.new({:station => s, :number => number, :reward => reward})
247
- else
248
- human_worker = CF::HumanWorker.new({:station => s, :number => number, :reward => reward, :stat_badge => stat_badge})
249
- end
250
248
 
251
- if worker['skill_badges'].present?
252
- skill_badges.each do |badge|
253
- human_worker.badge = badge
254
- end
255
- end
249
+ human_worker = CF::HumanWorker.new({:station => s, :number => number, :reward => reward, :badge => badges, :stat_badge => stat_badge})
256
250
 
257
251
  say_status "worker", "#{number} Cloud #{pluralize(number, "Worker")} with reward of #{reward} #{pluralize(reward, "cent")}"
258
252
  display_error(line_title, "#{human_worker.errors}") if human_worker.errors.present?
@@ -266,6 +260,7 @@ module Cf # :nodoc: all
266
260
  display_error(line_title, "Invalid worker type: #{worker_type}")
267
261
  end
268
262
 
263
+
269
264
  # Creation of Form
270
265
  # Creation of TaskForm
271
266
  if station_file['station']['task_form'].present?
@@ -298,6 +293,15 @@ module Cf # :nodoc: all
298
293
  display_error(line_title, "#{form.errors}") if form.errors.present?
299
294
  end
300
295
 
296
+ # print if the badge for the station exist NOTE: addition of the badge is done above.
297
+ # it is printed here for better UI
298
+ if badges
299
+ say "Adding Badges For Station#{index}", :green
300
+ badges.each do |name|
301
+ say_status "-", "#{name}"
302
+ end
303
+ end
304
+
301
305
  #check the presence of gold standard and create goldstandards for the station
302
306
  gold_standards = station_file['station']['gold_standards']
303
307
  if gold_standards
@@ -323,29 +327,6 @@ module Cf # :nodoc: all
323
327
  end
324
328
  end
325
329
  end
326
-
327
- #######create badge #########
328
- badges = station_file['station']['badges']
329
- if badges
330
- say "Adding Badges to station", :green
331
- badges.each do |s_badge|
332
- if s_badge.length == 1 && s_badge.keys.include?("name")
333
- else
334
- form = s_badge["test_attributes"]["form_attributes"]
335
- unless form.blank?
336
- if form["type"] == "CustomTaskForm"
337
- file = File.read("#{line_source}/#{form["file"]}") || File.read("#{line_source}/badges/station#{station_file['station']['station_index']}.html")
338
- s_badge["test_attributes"]["form_attributes"].merge!({:raw_html => file, :_type =>"CustomTaskForm" })
339
- else
340
- s_badge["test_attributes"]["form_attributes"].merge!({:_type =>"TaskForm" })
341
- end
342
- end
343
- end
344
- badge = CF::Badge.new(s_badge.merge({:line => line,:station => s }))
345
- say_status "Badge","'#{s_badge["name"] }'"
346
- display_error(line_title, "#{badge.errors}") if badge.errors.present?
347
- end
348
- end
349
330
  end
350
331
  end
351
332
 
@@ -370,17 +351,17 @@ module Cf # :nodoc: all
370
351
  end
371
352
  end
372
353
  if gold_standards.class == Hash#gold_standards["file"]
373
- gold_standard = CF::GoldStandard.new(gold_standards.merge!({:line => line}))
374
- say_status "GoldStandard from file", "'#{gold_standards[:file]}'"
375
- display_error(line_title, "#{gold_standard.errors}") if gold_standard.errors.present?
376
- else
377
- gold_standards.each do |gold_options|
378
- gold_standard = CF::GoldStandard.new(gold_options.merge(:line => line))
379
- say_status "GoldStandard", "'#{gold_options["name"]}'"
354
+ gold_standard = CF::GoldStandard.new(gold_standards.merge!({:line => line}))
355
+ say_status "GoldStandard from file", "'#{gold_standards[:file]}'"
380
356
  display_error(line_title, "#{gold_standard.errors}") if gold_standard.errors.present?
357
+ else
358
+ gold_standards.each do |gold_options|
359
+ gold_standard = CF::GoldStandard.new(gold_options.merge(:line => line))
360
+ say_status "GoldStandard", "'#{gold_options["name"]}'"
361
+ display_error(line_title, "#{gold_standard.errors}") if gold_standard.errors.present?
362
+ end
381
363
  end
382
364
  end
383
- end
384
365
 
385
366
  say " ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁", :white
386
367
  say "Line was successfully created.", :green
@@ -0,0 +1,21 @@
1
+ badge:
2
+ - name: Designer #name of the badge that you want to create
3
+ description: This badge allow to do the designing tasks #description of the badge
4
+ max_badge_assignments: 5 #numbers of worker who can take the badge
5
+ retries_allowed: 5 #number of retries that worker get at the time of taking the badge
6
+ pass_percentage: 80 #the value in percentage that the worker must score to pass the test
7
+ assignment_duration: 3600 #time assigned for badge test
8
+ check_manually: false #checking badge answers that worker has ssubmitted
9
+ allow_retake_after: 30 #number of day after which the worker can again take the badge exam
10
+ form: badge_form.html #form for the badge. It should be html format if it is a custom form
11
+ known_answers: #the hash of question and answers for the badge
12
+ - input:
13
+ image_url: http://www.crystalinks.com/newton.jpg
14
+ expected_output:
15
+ gender: male
16
+ age: 60
17
+ - input:
18
+ image_url: http://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Salmanrampwalk.png/220px-Salmanrampwalk.png
19
+ expected_output:
20
+ gender: male
21
+ age: 50
@@ -30,6 +30,8 @@ stations:
30
30
  worker_type: human # "human" or name of robot (google_translate_robot, etc)
31
31
  num_workers: 2
32
32
  reward: 2
33
+ badge:
34
+ - bizcard-digitizer
33
35
  custom_task_form:
34
36
  form_title: Clink a link and paste in url
35
37
  instruction: Look through search results and find the best date
data/lib/cf/help.txt CHANGED
@@ -65,3 +65,16 @@ Output:
65
65
  Get the output of run. For more info, cf output help
66
66
  More:
67
67
  cf output help [sub-command] #list all help related to output subcommands
68
+
69
+
70
+ Badge:
71
+ Usage:
72
+ cf badge create # Create badges that are in badge.yml
73
+ cf badge delete <badge name> # Delete the given badge within your account
74
+ cf badge generate # Generate a new badge template at badges/badge.yml
75
+ cf badge list # List all badges asscociated with your account. Use 'cf badge list <badge_name>' for single badge
76
+ cf badge update <badge_name> # Update the given badge within with your account. Use 'cf badge update' to update all badges in badge.yml
77
+ Description:
78
+ Commands to manage the badges.
79
+ More:
80
+ cf help [COMMAND] # Describe subcommands or one specific subcommand
@@ -45,7 +45,7 @@ module CF
45
45
  attr_accessor :errors
46
46
 
47
47
  # Badge setting for "worker" object
48
- attr_accessor :badge
48
+ attr_accessor :badges
49
49
 
50
50
  # ==Initializes a new "worker" object
51
51
  # ==Usage of HumanWorker.new(hash):
@@ -72,34 +72,26 @@ module CF
72
72
  @station = options[:station]
73
73
  @number = options[:number].nil? ? 1 : options[:number]
74
74
  @reward = options[:reward]
75
- @stat_badge = options[:stat_badge].nil? ? nil : options[:stat_badge]
75
+ @stat_badge = options[:stat_badge]
76
+ @badges = options[:badge]
76
77
  if @station
77
- if options[:stat_badge].nil?
78
- request =
79
- {
80
- :body =>
81
- {
82
- :api_key => CF.api_key,
83
- :worker => {:number => @number, :reward => @reward, :type => "HumanWorker"}
84
- }
85
- }
86
- else
87
- request =
78
+ request =
79
+ {
80
+ :body =>
88
81
  {
89
- :body =>
90
- {
91
- :api_key => CF.api_key,
92
- :worker => {:number => @number, :reward => @reward, :type => "HumanWorker"},
93
- :stat_badge => options[:stat_badge]
94
- }
82
+ :api_key => CF.api_key,
83
+ :worker => {:number => @number, :reward => @reward, :type => "HumanWorker"},
84
+ :badge => options[:badge],
85
+ :stat_badge => options[:stat_badge]
95
86
  }
96
- end
87
+ }
97
88
  resp = HTTParty.post("#{CF.api_url}#{CF.api_version}/lines/#{CF.account_name}/#{@station.line['title'].downcase}/stations/#{@station.index}/workers.json",request)
98
89
 
99
90
  self.id = resp.parsed_response['id']
100
91
  self.number = resp.parsed_response['number']
101
92
  self.reward = resp.parsed_response['reward']
102
93
  self.stat_badge = resp.parsed_response['stat_badge']
94
+ self.badges = resp.parsed_response['badges']
103
95
 
104
96
  if resp.code != 200
105
97
  self.errors = resp.parsed_response['error']['message']
@@ -109,52 +101,8 @@ module CF
109
101
  end
110
102
  end
111
103
 
112
- # ==Creation a new "worker" object with Badge
113
- # ==Usage Example:
114
- # ==In Plain Ruby way
115
- # badge_settings =
116
- # {
117
- # :title => 'Football Fanatic',
118
- # :description => "This qualification allows you to perform work at stations which have this badge.",
119
- # :max_badges => 3,
120
- # :test =>
121
- # {
122
- # :input => {:name => "Lionel Andres Messi", :country => "Argentina"},
123
- # :expected_output =>
124
- # [
125
- # {:birthplace => "Rosario, Santa Fe, Argentina",:match_options => {:tolerance => 10, :ignore_case => true }},
126
- # {:position => "CF",:match_options => {:tolerance => 1 }},
127
- # {:"current-club" => "Barcelona",:match_options => {:tolerance => 1, :ignore_case => false }}
128
- # ]
129
- # }
130
- # }
131
- # line = CF::Line.new("human_worker", "Digitization")
132
- # input_format = CF::InputFormat.new({:name => "image_url", :required => true, :valid_type => "url"})
133
- # line.input_formats input_format
134
- #
135
- # station = CF::Station.new({:type => "work"})
136
- # line.stations station
137
- #
138
- # worker = CF::HumanWorker.new({:number => 2, :reward => 20, :skill_badge => skill_badge})
139
- # line.stations.first.worker = worker
140
- #
141
- # line.stations.first.worker.badge = badge_settings
142
- def badge=(badge)
143
- request =
144
- {
145
- :body =>
146
- {
147
- :api_key => CF.api_key,
148
- :skill_badge => badge
149
- }
150
- }
151
- resp = HTTParty.post("#{CF.api_url}#{CF.api_version}/lines/#{CF.account_name}/#{@station.line['title'].downcase}/stations/#{@station.index}/workers/#{self.id}/badge.json",request)
152
- self.errors = resp['error']['message'] if resp.code != 200
153
- self.skill_badges << resp.parsed_response['skill_badges']
154
- end
155
-
156
104
  def to_s # :nodoc:
157
- "{:id => => #{self.id}, :number => #{self.number}, :reward => #{self.reward}, :stat_badge => #{self.stat_badge}, :errors => #{self.errors}}"
105
+ "{:id => => #{self.id}, :number => #{self.number}, :reward => #{self.reward}, :stat_badge => #{self.stat_badge}, :errors => #{self.errors},:badges =>#{self.badges}}"
158
106
  end
159
107
  end
160
108
  end
data/lib/cf/station.rb CHANGED
@@ -179,32 +179,26 @@ module CF
179
179
  number = worker_instance.number
180
180
  reward = worker_instance.reward
181
181
  stat_badge = worker_instance.stat_badge
182
- if stat_badge.nil?
183
- request =
184
- {
185
- :body =>
186
- {
187
- :api_key => CF.api_key,
188
- :worker => {:number => number, :reward => reward, :type => "HumanWorker"}
189
- }
190
- }
191
- else
192
- request =
182
+ badges = worker_instance.badges
183
+
184
+ request =
185
+ {
186
+ :body =>
193
187
  {
194
- :body =>
195
- {
196
- :api_key => CF.api_key,
197
- :worker => {:number => number, :reward => reward, :type => "HumanWorker"},
198
- :stat_badge => stat_badge
199
- }
188
+ :api_key => CF.api_key,
189
+ :worker => {:number => number, :reward => reward, :type => "HumanWorker"},
190
+ :stat_badge => stat_badge,
191
+ :badge => badges
200
192
  }
201
- end
193
+ }
194
+
202
195
  resp = HTTParty.post("#{CF.api_url}#{CF.api_version}/lines/#{CF.account_name}/#{self.line_title.downcase}/stations/#{self.index}/workers.json",request)
203
196
  worker = CF::HumanWorker.new({})
204
197
  worker.id = resp.parsed_response['id']
205
198
  worker.number = resp.parsed_response['number']
206
199
  worker.reward = resp.parsed_response['reward']
207
200
  worker.stat_badge = resp.parsed_response['stat_badge']
201
+ worker.badges = resp.parsed_response['badges']
208
202
  if resp.code != 200
209
203
  worker.errors = resp.parsed_response['error']['message']
210
204
  end
@@ -373,45 +367,5 @@ module CF
373
367
  def gold_standards=(gold_standard) # :nodoc:
374
368
  @gold_standards << gold_standard
375
369
  end
376
-
377
- #specify the badges for the station
378
- # ===Usage Example:
379
- # CF::Badge.new({:line => line, :station => s,:name => "Tomb Digitizer", :description => "This badge qualifies you to work on tomb digitization tasks.",:max_badges => 100,:gold_standards => ["#{@gold_standard.settings[:name]}"],:test_attributes =>{:type => "default",:retries => 10,:pass_percentage => 100,:check_manually => true}})
380
- def badges badge = nil
381
- line_title = line["title"] || line.title
382
- if badge
383
- if badge.settings[:test_attributes] && badge.settings[:test_attributes][:form_attributes].present?
384
- form = badge.settings[:test_attributes][:form_attributes]
385
- if( form[:type] == "CustomTaskForm" && form[:file])
386
- raw_html = ""
387
- File.open("#{form[:file]}").each_line do |line|
388
- raw_html += line
389
- end
390
- badge.settings[:test_attributes][:form_attributes].merge!({:raw_html => raw_html,:_type =>"CustomTaskForm"})
391
- end
392
- badge.settings[:test_attributes][:form_attributes].merge!({:_type =>"TaskForm"}) if form[:type] == "TaskForm"
393
- end
394
- request =
395
- {
396
- :body =>
397
- {
398
- :api_key => CF.api_key,
399
- :badge => badge.settings
400
- }
401
- }
402
- resp = HTTParty.post("#{CF.api_url}#{CF.api_version}/lines/#{CF.account_name}/#{line_title.downcase}/stations/#{self.index}/badges.json",request)
403
- badge = CF::Badge.new()
404
- badge.settings.merge!(resp.to_hash)
405
- self.errors = resp.parsed_response['error']['message'] if resp.code != 200
406
- @badges << badge
407
- else
408
- @badges
409
- end
410
- end
411
-
412
- def badges=(badge)
413
- @badges << badge
414
- end
415
-
416
370
  end
417
371
  end
data/lib/cf/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module CF # :nodoc: all
2
- VERSION = "0.5.6"
2
+ VERSION = "0.6"
3
3
  end
data/spec/badges_spec.rb CHANGED
@@ -1,342 +1,242 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe CF::Badge do
4
- context "add a badge" do
5
- it "should add a badge to the station in DSL way" do
4
+ context "#create" do
5
+ it " in block DSL way" do
6
6
  WebMock.allow_net_connect!
7
- title = "line_title#{Time.new.strftime('%Y%b%d-%H%M%S')}".downcase
8
- sleep 1
9
- line = CF::Line.create(title,"Digitization") do |l|
10
- CF::InputFormat.new({:line => l, :name => "Company", :required => true, :valid_type => "general"})
11
- CF::InputFormat.new({:line => l, :name => "Website", :required => true, :valid_type => "url"})
12
- end
13
- station = CF::Station.create({:line => line, :type => "work"}) do |s|
14
- CF::HumanWorker.new({:station => s, :number => 1, :reward => 10})
15
- CF::TaskForm.create({:station => s, :title => "Enter text from a business card image", :instruction => "Describe"}) do |i|
16
- CF::FormField.new({:form => i, :label => "First Name", :field_type => "short_answer", :required => "true"})
17
- CF::FormField.new({:form => i, :label => "Middle Name", :field_type => "short_answer"})
18
- CF::FormField.new({:form => i, :label => "Last Name", :field_type => "short_answer", :required => "true"})
19
- end
20
- @gold_standard = CF::GoldStandard.new({ :line => line,
21
- :station => s,
22
- :name => "easy_#{Time.now}",
23
- :input => {'image_url' => "http://onwired.com/images/portfolio/linda-stanley-business-card.jpg"},
24
- :expected_output => {"first_name" => {"value" => "John"}, "last_name" => {"value" => "Lennon"}, "company" => {"value" => "Sprout"}}
25
- })
26
-
27
- @badge = CF::Badge.create({
28
- :line => line,
29
- :station => s,
30
- :name => "Tomb Digitizer#{Time.now.day}",
31
- :description => "This badge qualifies you to work on tomb digitization tasks.",
32
- :max_badges => 100,
33
- :gold_standards => ["#{@gold_standard.settings[:name]}"],
34
- :test_attributes =>
35
- {
36
- :type => "default",
37
- :retries => 10,
38
- :pass_percentage => 100,
39
- :check_manually => true
40
- }
41
- })
42
- end
43
- end
44
-
45
- it "should reuse the badge with in the account and badge to the station in DSL way" do
46
- WebMock.allow_net_connect!
47
- title = "line_title#{Time.new.strftime('%Y%b%d-%H%M%S')}".downcase
48
- sleep 1
49
- line = CF::Line.create(title,"Digitization") do |l|
50
- CF::InputFormat.new({:line => l, :name => "Company", :required => true, :valid_type => "general"})
51
- CF::InputFormat.new({:line => l, :name => "Website", :required => true, :valid_type => "url"})
52
- end
53
- station = CF::Station.create({:line => line, :type => "work"}) do |s|
54
- CF::HumanWorker.new({:station => s, :number => 1, :reward => 10})
55
- CF::TaskForm.create({:station => s, :title => "Enter text from a business card image", :instruction => "Describe"}) do |i|
56
- CF::FormField.new({:form => i, :label => "First Name", :field_type => "short_answer", :required => "true"})
57
- CF::FormField.new({:form => i, :label => "Middle Name", :field_type => "short_answer"})
58
- CF::FormField.new({:form => i, :label => "Last Name", :field_type => "short_answer", :required => "true"})
59
- end
60
- @gold_standard = CF::GoldStandard.new({ :line => line,
61
- :station => s,
62
- :name => "easy_#{Time.now}",
63
- :input => {'image_url' => "http://onwired.com/images/portfolio/linda-stanley-business-card.jpg"},
64
- :expected_output => {"first_name" => {"value" => "John"}, "last_name" => {"value" => "Lennon"}, "company" => {"value" => "Sprout"}}
65
- })
66
-
67
- @badge = CF::Badge.create({
68
- :line => line,
69
- :station => s,
70
- :name => "timromero#{Time.now.day}",
71
- :description => "This badge qualifies you to work on tomb digitization tasks.",
72
- :max_badges => 100,
73
- :gold_standards => ["#{@gold_standard.settings[:name]}"],
74
- :test_attributes =>
75
- {
76
- :type => "default",
77
- :retries => 10,
78
- :pass_percentage => 100,
79
- :check_manually => true
80
- }
81
- })
82
-
83
- @badge = CF::Badge.create({
84
- :line => line,
85
- :station => s,
86
- :name => "Tomb Digitizer#{Time.now.day}"
87
- })
88
- end
89
- end
90
-
91
-
92
- it "should add a badge to the station in plain ruby way" do
93
- WebMock.allow_net_connect!
94
- title = "line_title#{Time.new.strftime('%Y%b%d-%H%M%S')}".downcase
95
- sleep 1
96
- @line = CF::Line.create(title,"Digitization") do |l|
97
- CF::InputFormat.new({:line => l, :name => "Company", :required => true, :valid_type => "general"})
98
- CF::InputFormat.new({:line => l, :name => "Website", :required => true, :valid_type => "url"})
99
- end
100
- station = CF::Station.create({:line => @line, :type => "work"}) do |s|
101
- CF::HumanWorker.new({:station => s, :number => 1, :reward => 10})
102
- CF::TaskForm.create({:station => s, :title => "Enter text from a business card image", :instruction => "Describe"}) do |i|
103
- CF::FormField.new({:form => i, :label => "First Name", :field_type => "short_answer", :required => "true"})
104
- CF::FormField.new({:form => i, :label => "Middle Name", :field_type => "short_answer"})
105
- CF::FormField.new({:form => i, :label => "Last Name", :field_type => "short_answer", :required => "true"})
106
- end
107
- @gold_standard = CF::GoldStandard.new({ :line => @line,
108
- :station => s,
109
- :name => "easy",
110
- :input => {'image_url' => "http://onwired.com/images/portfolio/linda-stanley-business-card.jpg"},
111
- :expected_output => {"first_name" => {"value" => "John"}, "last_name" => {"value" => "Lennon"}, "company" => {"value" => "Sprout"}}
112
- })
113
- end
114
- badge = CF::Badge.new({
115
- :name => "Tomb Digitizereee#{Time.now}",
116
- :description => "This badge qualifies you to work on tomb digitization tasks.",
117
- :max_badges => 100,
118
- :gold_standards => ["#{@gold_standard.settings[:name]}"],
119
- :test_attributes =>
120
- {
121
- :type => "default",
122
- :retries => 10,
123
- :pass_percentage => 100,
124
- :check_manually => false
125
- }
126
- })
127
-
128
- @line.stations.first.badges badge
129
- badge1 = CF::Badge.new({
130
- :name => "timromero#{Time.now.day}"
131
- })
132
- @line.stations.first.badges badge1
133
- @line.stations.first.badges.count.should eql(2)
134
- @line.stations.first.badges.last.settings["name"].should eql("timromero#{Time.now.day}")
135
- @line.stations.first.badges.first.settings["name"].should eq(badge.settings[:name])
136
- @line.stations.first.badges.first.settings['test']["form"].should eql({"_type"=>"TaskForm", "form_fields"=>[{"label"=>"First Name", "field_type"=>"short_answer", "required"=>true, "position"=>1}, {"label"=>"Middle Name", "field_type"=>"short_answer", "position"=>2}, {"label"=>"Last Name", "field_type"=>"short_answer", "required"=>true, "position"=>3}], "instruction"=>"Describe", "title"=>"Enter text from a business card image"})
137
-
138
- end
139
-
140
-
141
- it "should add a badge to the station with test form defined in DSL way" do
142
- WebMock.allow_net_connect!
143
- title = "line_title#{Time.new.strftime('%Y%b%d-%H%M%S')}".downcase
144
- sleep 1
145
- line = CF::Line.create(title,"Digitization") do |l|
146
- CF::InputFormat.new({:line => l, :name => "Company", :required => true, :valid_type => "general"})
147
- CF::InputFormat.new({:line => l, :name => "Website", :required => true, :valid_type => "url"})
148
- end
149
- station = CF::Station.create({:line => line, :type => "work"}) do |s|
150
- CF::HumanWorker.new({:station => s, :number => 1, :reward => 10})
151
- CF::TaskForm.create({:station => s, :title => "Enter text from a business card image", :instruction => "Describe"}) do |i|
152
- CF::FormField.new({:form => i, :label => "First Name", :field_type => "short_answer", :required => "true"})
153
- CF::FormField.new({:form => i, :label => "Middle Name", :field_type => "short_answer"})
154
- CF::FormField.new({:form => i, :label => "Last Name", :field_type => "short_answer", :required => "true"})
155
- end
156
- @gold_standard = CF::GoldStandard.new({ :line => line,
157
- :station => s,
158
- :name => "easy_#{Time.now}",
159
- :input => {'image_url' => "http://onwired.com/images/portfolio/linda-stanley-business-card.jpg"},
160
- :expected_output => {"first_name" => {"value" => "John"}, "last_name" => {"value" => "Lennon"}, "company" => {"value" => "Sprout"}}
161
- })
162
-
163
- @badge = CF::Badge.new({
164
- :line => line,
165
- :station => s,
166
- :name => "Tomb Digitizer#{Time.now}",
167
- :description => "This badge qualifies you to work on tomb digitization tasks.",
168
- :max_badges => 100,
169
- :gold_standards => ["#{@gold_standard.settings[:name]}"],
170
- :test_attributes =>
171
- {
172
- :type => "default",
173
- :retries => 10,
174
- :pass_percentage => 100,
175
- :check_manually => true,
176
- :form_attributes => {
177
- :title => "Tombfinder Test",
178
- :instruction => "Look at the image of the tomb in the url given and answer the asked questions.",
179
- :type => "TaskForm",
180
- :form_fields_attributes =>
181
- [
182
- { "label" => "Last Name", "field_type" => "radio_button", 'option_values' => ["LEIGH", "IRVING", "GERTRUDE"] },
183
- { "label" => "Lizzie Year of Birth", "field_type" => "radio_button", 'option_values' => ["1873", "1933", "1896"] }
184
- ]
185
- }
186
- }
187
- })
188
- end
189
- line.stations.first.badges.first.settings[:test_attributes][:form_attributes][:type].should eql("TaskForm")
190
- line.stations.first.badges.first.settings[:test_attributes][:form_attributes][:form_fields_attributes].should eql([{"label"=>"Last Name", "field_type"=>"radio_button", "option_values"=>["LEIGH", "IRVING", "GERTRUDE"]}, {"label"=>"Lizzie Year of Birth", "field_type"=>"radio_button", "option_values"=>["1873", "1933", "1896"]}])
191
- end
192
-
193
-
194
-
195
- it "should add a badge to the station with customtaskform as a badge testform form defined in DSL way" do
196
- WebMock.allow_net_connect!
197
- title = "line_title#{Time.new.strftime('%Y%b%d-%H%M%S')}".downcase
198
- sleep 1
199
- line = CF::Line.create(title,"Digitization") do |l|
200
- CF::InputFormat.new({:line => l, :name => "Company", :required => true, :valid_type => "general"})
201
- CF::InputFormat.new({:line => l, :name => "Website", :required => true, :valid_type => "url"})
202
- end
203
- station = CF::Station.create({:line => line, :type => "work"}) do |s|
204
- CF::HumanWorker.new({:station => s, :number => 1, :reward => 10})
205
- CF::TaskForm.create({:station => s, :title => "Enter text from a business card image", :instruction => "Describe"}) do |i|
206
- CF::FormField.new({:form => i, :label => "First Name", :field_type => "short_answer", :required => "true"})
207
- CF::FormField.new({:form => i, :label => "Middle Name", :field_type => "short_answer"})
208
- CF::FormField.new({:form => i, :label => "Last Name", :field_type => "short_answer", :required => "true"})
209
- end
210
- @gold_standard = CF::GoldStandard.new({ :line => line,
211
- :station => s,
212
- :name => "easy_#{Time.now}",
213
- :input => {'image_url' => "http://onwired.com/images/portfolio/linda-stanley-business-card.jpg"},
214
- :expected_output => {"first_name" => {"value" => "John"}, "last_name" => {"value" => "Lennon"}, "company" => {"value" => "Sprout"}}
215
- })
216
-
217
- @badge = CF::Badge.new({
218
- :line => line,
219
- :station => s,
220
- :name => "Tomb Digitizer#{Time.now}",
221
- :description => "This badge qualifies you to work on tomb digitization tasks.",
222
- :max_badges => 100,
223
- :gold_standards => ["#{@gold_standard.settings[:name]}"],
224
- :test_attributes =>
225
- {
226
- :type => "default",
227
- :retries => 10,
228
- :pass_percentage => 100,
229
- :check_manually => true,
230
- :form_attributes => {
231
- :title => "Custom TestForm",
232
- :instruction => "Look at the image of the tomb in the url given and answer the asked questions.",
233
- :type => "CustomTaskForm",
234
- :file =>"./spec/badges/station_1.html"
235
- }
236
- }
237
- })
238
- end
239
- line.stations.first.badges.first.settings[:test_attributes][:form_attributes][:type].should eql("CustomTaskForm")
240
- end
241
-
242
-
243
- it "should add a badge with defined custom_task_form as a badge test form in plain ruby way " do
244
- WebMock.allow_net_connect!
245
- title = "line_title#{Time.new.strftime('%Y%b%d-%H%M%S')}".downcase
246
- sleep 1
247
- @line = CF::Line.create(title,"Digitization") do |l|
248
- CF::InputFormat.new({:line => l, :name => "Company", :required => true, :valid_type => "general"})
249
- CF::InputFormat.new({:line => l, :name => "Website", :required => true, :valid_type => "url"})
250
- end
251
- station = CF::Station.create({:line => @line, :type => "work"}) do |s|
252
- CF::HumanWorker.new({:station => s, :number => 1, :reward => 10})
253
- CF::TaskForm.create({:station => s, :title => "Enter text from a business card image", :instruction => "Describe"}) do |i|
254
- CF::FormField.new({:form => i, :label => "First Name", :field_type => "short_answer", :required => "true"})
255
- CF::FormField.new({:form => i, :label => "Middle Name", :field_type => "short_answer"})
256
- CF::FormField.new({:form => i, :label => "Last Name", :field_type => "short_answer", :required => "true"})
257
- end
258
- @gold_standard = CF::GoldStandard.new({ :line => @line,
259
- :station => s,
260
- :name => "easy",
261
- :input => {'image_url' => "http://onwired.com/images/portfolio/linda-stanley-business-card.jpg"},
262
- :expected_output => {"first_name" => {"value" => "John"}, "last_name" => {"value" => "Lennon"}, "company" => {"value" => "Sprout"}}
263
- })
264
- end
265
- badge = CF::Badge.new({
266
- :name => "Tomb Digitizer#{Time.now}",
267
- :description => "This badge qualifies you to work on tomb digitization tasks.",
268
- :max_badges => 100,
269
- :gold_standards => ["#{@gold_standard.settings[:name]}"],
270
- :test_attributes =>
271
- {
272
- :type => "default",
273
- :retries => 10,
274
- :pass_percentage => 100,
275
- :check_manually => false,
276
- :form_attributes => {
277
- :title => "Custom TestForm in plain ruby way",
278
- :instruction => "Look at the image of the tomb in the url given and answer the asked questions.",
279
- :type => "CustomTaskForm",
280
- :file =>"./spec/badges/station_1.html"
281
- }
282
- }
283
- })
284
- @line.stations.first.badges badge
285
- @line.stations.first.badges.first.settings["test"]["form"]["_type"].should eql("CustomTaskForm")
286
- @line.stations.first.badges.first.settings["test"]["form"]["raw_html"].present?.should eql(true)
287
- end
288
-
289
-
290
- it "should add a badge with defined task_form as a badge test form in plain ruby way " do
291
- WebMock.allow_net_connect!
292
- title = "line_title#{Time.new.strftime('%Y%b%d-%H%M%S')}".downcase
293
- sleep 1
294
- @line = CF::Line.create(title,"Digitization") do |l|
295
- CF::InputFormat.new({:line => l, :name => "Company", :required => true, :valid_type => "general"})
296
- CF::InputFormat.new({:line => l, :name => "Website", :required => true, :valid_type => "url"})
297
- end
298
- station = CF::Station.create({:line => @line, :type => "work"}) do |s|
299
- CF::HumanWorker.new({:station => s, :number => 1, :reward => 10})
300
- CF::TaskForm.create({:station => s, :title => "Enter text from a business card image", :instruction => "Describe"}) do |i|
301
- CF::FormField.new({:form => i, :label => "First Name", :field_type => "short_answer", :required => "true"})
302
- CF::FormField.new({:form => i, :label => "Middle Name", :field_type => "short_answer"})
303
- CF::FormField.new({:form => i, :label => "Last Name", :field_type => "short_answer", :required => "true"})
304
- end
305
- @gold_standard = CF::GoldStandard.new({ :line => @line,
306
- :station => s,
307
- :name => "easy",
308
- :input => {'image_url' => "http://onwired.com/images/portfolio/linda-stanley-business-card.jpg"},
309
- :expected_output => {"first_name" => {"value" => "John"}, "last_name" => {"value" => "Lennon"}, "company" => {"value" => "Sprout"}}
310
- })
311
- end
312
- badge = CF::Badge.new({
313
- :name => "Tomb Digitizer#{Time.now}",
314
- :description => "This badge qualifies you to work on tomb digitization tasks.",
315
- :max_badges => 100,
316
- :gold_standards => ["#{@gold_standard.settings[:name]}"],
317
- :test_attributes =>
318
- {
319
- :type => "default",
320
- :retries => 10,
321
- :pass_percentage => 100,
322
- :check_manually => false,
323
- :form_attributes => {
324
- :title => "Tombfinder Test plain ruby way",
325
- :instruction => "Look at the image of the tomb in the url given and answer the asked questions.",
326
- :type => "TaskForm",
327
- :form_fields_attributes =>
328
- [
329
- { "label" => "Last Name", "field_type" => "radio_button", 'option_values' => ["LEIGH", "IRVING", "GERTRUDE"] },
330
- { "label" => "Lizzie Year of Birth", "field_type" => "radio_button", 'option_values' => ["1873", "1933", "1896"] }
331
- ]
332
- }
333
- }
334
- })
335
- @line.stations.first.badges badge
336
- @line.stations.first.badges.first.settings["test"]["form"]["_type"].should eql("TaskForm")
337
- @line.stations.first.badges.first.settings["test"]["form"]["form_fields"].should eql([{"label"=>"Last Name", "field_type"=>"radio_button", "option_values"=>["LEIGH", "IRVING", "GERTRUDE"]}, {"label"=>"Lizzie Year of Birth", "field_type"=>"radio_button", "option_values"=>["1873", "1933", "1896"]}])
338
-
339
- end
7
+ @badge = CF::Badge.create(
8
+ :name => "Bizcard Digitizer_dsl",
9
+ :description => "this badge qualifies the worker for bizcard digitization",
10
+ :max_badge_assignments => 100,
11
+ :retries_allowed => 5,
12
+ :pass_percentage => 80,
13
+ :assignment_duration => 3600,
14
+ :allow_retake_after => 5,
15
+ :check_manually =>false,
16
+ :form => "./lib/cf/cli/templates/sample-line/form.html",
17
+ :known_answers=>
18
+ [
19
+ { "name" => "chandra",
20
+ "input"=> {"image_url"=> "http://onwired.com/images/portfolio/linda-stanley-business-card.jpg"},
21
+ "expected_output"=>{"gender"=>{"value"=>"Male"}, "rich"=>{"value"=>"1"}}
22
+ },
23
+ {
24
+ "name" => "mohan",
25
+ "input"=>{"image_url"=> "http://onwired.com/images/portfolio/linda-stanley-business-card.jpg2"},
26
+ "expected_output"=>{"gender"=>{"value"=>"Male"}, "rich"=>{"value"=>"2"}}
27
+ }
28
+ ]
29
+ )
30
+ end
31
+
32
+ it "in plain ruby way" do
33
+ WebMock.allow_net_connect!
34
+ @badge = CF::Badge.new(
35
+ :name => "Bizcard Digitizer_plain",
36
+ :description => "this badge qualifies the worker for bizcard digitization",
37
+ :max_badge_assignments => 100,
38
+ :retries_allowed => 5,
39
+ :pass_percentage => 80,
40
+ :assignment_duration => 3600,
41
+ :allow_retake_after => 5,
42
+ :check_manually =>false,
43
+ :form => "./lib/cf/cli/templates/sample-line/form.html",
44
+ :known_answers=>
45
+ [
46
+ { "name" => "chandra",
47
+ "input"=> {"image_url"=> "http://onwired.com/images/portfolio/linda-stanley-business-card.jpg"},
48
+ "expected_output"=>{"gender"=>{"value"=>"Male"}, "rich"=>{"value"=>"1"}}
49
+ },
50
+ {
51
+ "name" => "mohan",
52
+ "input"=>{"image_url"=> "http://onwired.com/images/portfolio/linda-stanley-business-card.jpg2"},
53
+ "expected_output"=>{"gender"=>{"value"=>"Male"}, "rich"=>{"value"=>"2"}}
54
+ }
55
+ ]
56
+ )
57
+ end
58
+
59
+ it " with standard task form" do
60
+ WebMock.allow_net_connect!
61
+ @badge = CF::Badge.create(
62
+ :name => "Bizcard Digitizer_standard_form",
63
+ :description => "this badge qualifies the worker for bizcard digitization",
64
+ :max_badge_assignments => 100,
65
+ :retries_allowed => 5,
66
+ :pass_percentage => 80,
67
+ :assignment_duration => 3600,
68
+ :allow_retake_after => 5,
69
+ :check_manually =>false,
70
+ :form => {:title => "standard task form", :instruction => "Describe me"},
71
+ :known_answers=>
72
+ [
73
+ { "name" => "chandra",
74
+ "input"=> {"image_url"=> "http://onwired.com/images/portfolio/linda-stanley-business-card.jpg"},
75
+ "expected_output"=>{"gender"=>{"value"=>"Male"}, "rich"=>{"value"=>"1"}}
76
+ },
77
+ {
78
+ "name" => "mohan",
79
+ "input"=>{"image_url"=> "http://onwired.com/images/portfolio/linda-stanley-business-card.jpg2"},
80
+ "expected_output"=>{"gender"=>{"value"=>"Male"}, "rich"=>{"value"=>"2"}}
81
+ }
82
+ ]
83
+ )
84
+ end
85
+
86
+ end
87
+
88
+ context "create a badge without" do
89
+ before(:each) do
90
+ WebMock.allow_net_connect!
91
+ @badge_hash = {
92
+ :name => "Bizcard Digitizer#{Time.new.strftime('%Y%b%d-%H%M%S')}",
93
+ :description => "this badge qualifies the worker for bizcard digitization",
94
+ :max_badge_assignments => 100,
95
+ :retries_allowed => 5,
96
+ :pass_percentage => 80,
97
+ :assignment_duration => 3600,
98
+ :allow_retake_after => 5,
99
+ :check_manually =>false,
100
+ :form => "./lib/cf/cli/templates/sample-line/form.html",
101
+ :known_answers=>
102
+ [
103
+ { "name" => "chandra",
104
+ "input"=> {"image_url"=> "http://onwired.com/images/portfolio/linda-stanley-business-card.jpg"},
105
+ "expected_output"=>{"gender"=>{"value"=>"Male"}, "rich"=>{"value"=>"1"}}
106
+ },
107
+ {
108
+ "name" => "mohan",
109
+ "input"=>{"image_url"=> "http://onwired.com/images/portfolio/linda-stanley-business-card.jpg2"},
110
+ "expected_output"=>{"gender"=>{"value"=>"Male"}, "rich"=>{"value"=>"2"}}
111
+ }
112
+ ]
113
+ }
114
+ end
115
+
116
+
117
+ it "name should give error 'Name can't be blank'" do
118
+ @badge_hash.delete(:name)
119
+ badge = CF::Badge.new(@badge_hash)
120
+ badge.errors.should eql(["Name can't be blank"])
121
+ end
122
+
123
+ it "form should raise error 'Test is invalid'" do
124
+ @badge_hash.delete(:form)
125
+ badge = CF::Badge.new(@badge_hash)
126
+ badge.errors.should eql(["Test is invalid"])
127
+ end
128
+
129
+ it "known_answers should raise error 'You have not provided known answers for the badge'" do
130
+ @badge_hash.delete(:known_answers)
131
+ badge = CF::Badge.new(@badge_hash)
132
+ badge.errors.should eql("You have not provided known answers for the badge.")
133
+ end
134
+
135
+ it "form with different file type" do
136
+ @badge_hash[:form] = "./lib/cf/cli/templates/sample-line/badge.yml"
137
+ badge = CF::Badge.new(@badge_hash)
138
+ badge.errors.should eql(["Wrong type of file for the form."])
139
+ end
140
+ end
141
+
142
+
143
+ context "#update" do
144
+ before(:each) do
145
+ WebMock.allow_net_connect!
146
+ @badge = CF::Badge.create(
147
+ :name => "Bizcard Digitizer",
148
+ :description => "this badge qualifies the worker for bizcard digitization",
149
+ :max_badge_assignments => 100,
150
+ :retries_allowed => 5,
151
+ :pass_percentage => 80,
152
+ :assignment_duration => 3600,
153
+ :allow_retake_after => 5,
154
+ :check_manually =>false,
155
+ :form => "./lib/cf/cli/templates/sample-line/form.html",
156
+ :known_answers=>
157
+ [
158
+ { "name" => "chandra",
159
+ "input"=> {"image_url"=> "http://onwired.com/images/portfolio/linda-stanley-business-card.jpg"},
160
+ "expected_output"=>{"gender"=>{"value"=>"Male"}, "rich"=>{"value"=>"1"}}
161
+ },
162
+ {
163
+ "name" => "mohan",
164
+ "input"=>{"image_url"=> "http://onwired.com/images/portfolio/linda-stanley-business-card.jpg2"},
165
+ "expected_output"=>{"gender"=>{"value"=>"Male"}, "rich"=>{"value"=>"2"}}
166
+ }
167
+ ]
168
+ )
169
+
170
+ @badge_hash = {
171
+ :name => "Bizcard Digitizer_updated",
172
+ :description => "this badge qualifies the worker for bizcard digitization for next level",
173
+ :max_badge_assignments => 80,
174
+ :retries_allowed => 0,
175
+ :pass_percentage => 10,
176
+ :assignment_duration => 300,
177
+ :allow_retake_after => 2,
178
+ :check_manually => true,
179
+ :form => "./lib/cf/cli/templates/sample-line/form.html",
180
+ :known_answers=>
181
+ [
182
+ {
183
+ "name" => "updated",
184
+ "input"=>{"image_url"=> "http://onwired.com/images/portfolio/linda-stanley-business-card.jpg2"},
185
+ "expected_output"=>{"gender"=>{"value"=>"Male"}, "rich"=>{"value"=>"2"}}
186
+ }
187
+ ]
188
+ }
189
+ end
190
+
191
+ it "in block DSl Way" do
192
+ badge = CF::Badge.update("Bizcard Digitizer", @badge_hash)
193
+ badge["name"].should eql("bizcard-digitizer_updated")
194
+ badge["description"].should eql("this badge qualifies the worker for bizcard digitization for next level")
195
+ badge["max_badge_assignments"].should eql(80)
196
+ badge["test"]["retries"].should eql(0)
197
+ badge["test"]["check_manually"].should eql(true)
198
+ badge["test"]["pass_percentage"].should eql(10)
199
+ badge["test"]["known_answers"].should eql(1)
200
+ end
201
+
202
+ it "in absence of form should raise error" do
203
+ @badge_hash.delete(:form)
204
+ badge = CF::Badge.update(@badge.name, @badge_hash)
205
+ badge["error"]["message"].should eql("Badge #{@badge.name.parameterize} can not be updated beacuse {:test=>[\"is invalid\"]}")
206
+ end
207
+
208
+ end
209
+
210
+ context "#list" do
211
+ it "should list all badges" do
212
+ WebMock.allow_net_connect!
213
+ badge = CF::Badge.list
214
+ badge["code"].should eql(200)
215
+ end
216
+
217
+ it "should list specific badge" do
218
+ WebMock.allow_net_connect!
219
+ badge_list = CF::Badge.list("bizcard-digitizer_updated")
220
+ badge_list["badges"].first["name"].should eql("bizcard-digitizer_updated")
221
+ end
222
+ end
223
+
224
+ context "#delete" do
225
+ it "a specific badge" do
226
+ WebMock.allow_net_connect!
227
+ badge = CF::Badge.destroy("bizcard-digitizer_updated")
228
+ badge["code"].should eql(200)
229
+ CF::Badge.list("bizcard-digitizer_updated")["error"]["message"].should eql("No badge to list")
230
+ end
340
231
 
341
- end
342
- end
232
+ it "delete all badges" do
233
+ WebMock.allow_net_connect!
234
+ badge2 = CF::Badge.destroy("bizcard-digitizer")
235
+ badge2 = CF::Badge.destroy("bizcard-digitizer_updated")
236
+ badge2 = CF::Badge.destroy("bizcard-digitizer_dsl")
237
+ badge3 = CF::Badge.destroy("bizcard-digitizer_plain")
238
+ badge4 = CF::Badge.destroy("Bizcard Digitizer_standard_form")
239
+ end
240
+
241
+ end
242
+ end