cloudfactory 0.6 → 0.6.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,3 @@
1
- ## 0.6 (2012-06-06)
2
- * Badge creation, update, deletion and listing added
3
-
4
1
  ## 0.5.6 (2012-05-17)
5
2
  * Priority field added in the line
6
3
 
@@ -13,132 +13,45 @@ module CF
13
13
  attr_accessor :description
14
14
 
15
15
  # number of badge provided to the worker
16
- attr_accessor :max_badge_assignments
17
-
18
- attr_accessor :allow_retake_after
16
+ attr_accessor :max_badges
19
17
 
20
18
  # Contains error message
21
19
  attr_accessor :errors
22
20
 
23
21
  def initialize(options={})
24
22
  options.symbolize_keys!
25
- #if the form is provided as a file then explicitly add title and raw_html for the form
26
- if options[:form] && options[:form].class != Hash && File.exist?(options[:form])
27
- file_type = IO.popen(["file", "--brief", "--mime-type", options[:form]], in: :close, err: :close).read.chomp
28
- if file_type == "text/html"
29
- options[:form] = {:title => "#{options[:name]}_form",:_type => "CustomTaskForm", :raw_html => File.read(options[:form])}
30
- else
31
- @errors = ["Wrong type of file for the form."]
32
- return @errors
33
- exit(1)
23
+ if options[:test_attributes] && options[:test_attributes][:form_attributes]
24
+ form = options[:test_attributes][:form_attributes]
25
+ if( form[:type] == "CustomTaskForm" && form[:file])
26
+ raw_html = ""
27
+ File.open("#{form[:file]}").each_line do |line|
28
+ raw_html += line
29
+ end
30
+ options[:test_attributes][:form_attributes].merge!({:raw_html => raw_html,:_type =>"CustomTaskForm"})
34
31
  end
32
+ options[:test_attributes][:form_attributes].merge!({:_type =>"TaskForm"}) if form[:type] == "TaskForm"
35
33
  end
36
-
37
- #known_answers of the badge get customize because to if unique identifier doesn't present then array of hashs returns the last hash for Httparty and get all key value merge into one in rest client/ rack
38
- options[:known_answers] = self.class.customize_known_answers(options[:known_answers], options[:name]) if options[:known_answers].present?
39
-
40
34
  @settings = options
41
- @name = options[:name]
42
- @description = options[:description]
43
- @allow_retake_after = options[:allow_retake_after]
44
-
45
- request =
46
- {
47
- :body =>
35
+ @station = options.delete(:station)
36
+ @line = options.delete(:line)
37
+ if @line.present? && @station.present?
38
+ request =
48
39
  {
49
- :api_key => CF.api_key,
50
- :badge => options
40
+ :body =>
41
+ {
42
+ :api_key => CF.api_key,
43
+ :badge => options
44
+ }
51
45
  }
52
- }
53
46
 
54
- resp = HTTParty.post("#{CF.api_url}#{CF.api_version}/accounts/#{CF.account_name}/badges.json",request)
55
- begin
56
- self.errors = resp["error"]["message"] if resp.code != 200
57
- return resp
58
- rescue
59
- self.errors = ["Unexpected error found. Confirm that you are in correct path and try again."]
47
+ resp = HTTParty.post("#{CF.api_url}#{CF.api_version}/lines/#{CF.account_name}/#{@line.title.downcase}/stations/#{@station.index}/badges.json",request)
48
+ @station.badges = self
49
+ self.errors = resp.parsed_response['error']['message'] if resp.code != 200
60
50
  end
61
51
  end
62
52
 
63
53
  def self.create(*args)
64
54
  Badge.new(args.first)
65
55
  end
66
-
67
- def self.update(badge_name, update_params)
68
- badge_name = badge_name.parameterize
69
- update_params.symbolize_keys!
70
- if update_params[:form] && update_params[:form].class != Hash && File.exist?(update_params[:form])
71
- file_type = IO.popen(["file", "--brief", "--mime-type", update_params[:form]], in: :close, err: :close).read.chomp
72
- if file_type == "text/html"
73
- update_params[:form] = {:title => "#{update_params[:name]}_form",:_type => "CustomTaskForm", :raw_html => File.read(update_params[:form])}
74
- else
75
- return
76
- end
77
- end
78
-
79
- update_params[:known_answers] = customize_known_answers(update_params[:known_answers], update_params[:name]) if update_params[:known_answers].present?
80
-
81
- request =
82
- {
83
- :body =>
84
- {
85
- :api_key => CF.api_key,
86
- :badge => update_params
87
- }
88
- }
89
- resp = HTTParty.put("#{CF.api_url}#{CF.api_version}/accounts/#{CF.account_name}/badges/#{badge_name}.json",request)
90
- return resp
91
- end
92
-
93
- def self.destroy(*args)
94
- name = args[0].parameterize
95
- options = args[1].nil? ? {} : args[1]
96
- decendents = check_for_decendents(name)
97
- if decendents
98
- response = delete("/accounts/#{CF.account_name}/badges/#{name}.json") if options.force
99
- else
100
- response = delete("/accounts/#{CF.account_name}/badges/#{name}.json")
101
- end
102
- @errors = response['error']['message'] if response['code'] != 200
103
- return response
104
- end
105
-
106
- #list all badges with in the account if name is not provided else it will return list all values of the given badge
107
- def self.list(name = nil)
108
- response = get("/accounts/#{CF.account_name}/badges.json", :name => name)
109
- @errors = response['error']['message'] if response['code'] != 200
110
- return response
111
- end
112
-
113
- protected
114
-
115
- #insert name to the goldstandard/known_answer if name is not provided
116
- def self.customize_known_answers(known_answers, badge_name)
117
- index = 1
118
- known_answers.each do |known_answer|
119
- unless known_answer["name"].present?
120
- known_answer.merge!("name" => "#{badge_name}_answer_#{index}")
121
- index +=1
122
- end
123
- end
124
- end
125
-
126
-
127
- #list the decendents for the badges like worker_associated, line_associated, number of tasks associated with the badge
128
- def self.check_for_decendents(name)
129
- response = get("/accounts/#{CF.account_name}/badges.json", :name => name)
130
- @errors = response["error"]["message"] unless response["code"] == 200
131
- unless @errors
132
- lines = response["badges"].first["line_associated"]
133
- workers = response["badges"].first["workers"]
134
- tasks = response["badges"].first["num_of_tasks"]
135
- #if the lines of worker count is greater then 0 then return true else return lines and worker count
136
- if lines > 0 || workers > 0
137
- {:lines_count => lines, :workers_count => workers, :tasks => tasks }
138
- else
139
- false
140
- end
141
- end
142
- end
143
56
  end
144
57
  end
@@ -17,15 +17,12 @@ require 'active_support/core_ext/object/blank'
17
17
 
18
18
  cli_directory = File.expand_path("../cf/cli", File.dirname(__FILE__))
19
19
  require "#{cli_directory}/config"
20
- require "#{cli_directory}/badge_yaml_validator"
21
- require "#{cli_directory}/badge"
22
20
  require "#{cli_directory}/line_yaml_validator"
23
21
  require "#{cli_directory}/line"
24
22
  require "#{cli_directory}/form"
25
23
  require "#{cli_directory}/production"
26
24
 
27
25
 
28
-
29
26
  if ENV['TEST']
30
27
  require 'pry'
31
28
  end
@@ -36,9 +33,9 @@ module Cf # :nodoc: all
36
33
  include Cf::Config
37
34
 
38
35
  def help(*args)
39
- File.open(File.expand_path(File.dirname(__FILE__)+"/help.txt")).each_line{ |s|
40
- say s
41
- }
36
+ File.open(File.expand_path(File.dirname(__FILE__)+"/help.txt")).each_line{ |s|
37
+ say s
38
+ }
42
39
  end
43
40
 
44
41
  map "-v" => :version
@@ -62,7 +59,7 @@ module Cf # :nodoc: all
62
59
 
63
60
  no_tasks do
64
61
  def ask_password(message)
65
- ::HighLine.new.ask(message) do |q|
62
+ ::HighLine.new.ask(message) do |q|
66
63
  q.echo = '*'
67
64
  end
68
65
  end
@@ -106,9 +103,6 @@ module Cf # :nodoc: all
106
103
  # later it can be replaced with hacked millisami-thor version of the thor library with run-time dependency via Bundler
107
104
  subcommand "production", Cf::Production
108
105
 
109
- desc "badge", "Commands to manage Badges. For more info cf badge help"
110
- subcommand "badge", Cf::Badge
111
-
112
106
  desc "output <run-title>", "Get the output of run. For more info, cf output help"
113
107
  method_option :run_title, :type => :string, :required => true, :aliases => "-t", :desc => "the index of the station"
114
108
  method_option :station_index, :type => :numeric, :aliases => "-s", :desc => "the index of the station"
@@ -134,7 +128,7 @@ module Cf # :nodoc: all
134
128
  output = CF::Run.final_output(run_title)
135
129
  end
136
130
  if !output.empty?
137
- output.each
131
+ output.each
138
132
  csv_str = CSVHash(output,output.first.keys)
139
133
  csv_str = csv_str.gsub("\"\"", '"')
140
134
  FileUtils.mkdir("#{line_source}/output") unless Dir.exist?("#{line_source}/output") if RUBY_VERSION > "1.9"
@@ -239,14 +239,20 @@ 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
-
245
242
  if worker_type == "human"
246
- badges = station_file['station']['badges']
243
+ skill_badges = worker['skill_badges']
247
244
  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
248
250
 
249
- human_worker = CF::HumanWorker.new({:station => s, :number => number, :reward => reward, :badge => badges, :stat_badge => stat_badge})
251
+ if worker['skill_badges'].present?
252
+ skill_badges.each do |badge|
253
+ human_worker.badge = badge
254
+ end
255
+ end
250
256
 
251
257
  say_status "worker", "#{number} Cloud #{pluralize(number, "Worker")} with reward of #{reward} #{pluralize(reward, "cent")}"
252
258
  display_error(line_title, "#{human_worker.errors}") if human_worker.errors.present?
@@ -260,7 +266,6 @@ module Cf # :nodoc: all
260
266
  display_error(line_title, "Invalid worker type: #{worker_type}")
261
267
  end
262
268
 
263
-
264
269
  # Creation of Form
265
270
  # Creation of TaskForm
266
271
  if station_file['station']['task_form'].present?
@@ -293,15 +298,6 @@ module Cf # :nodoc: all
293
298
  display_error(line_title, "#{form.errors}") if form.errors.present?
294
299
  end
295
300
 
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
-
305
301
  #check the presence of gold standard and create goldstandards for the station
306
302
  gold_standards = station_file['station']['gold_standards']
307
303
  if gold_standards
@@ -327,6 +323,29 @@ module Cf # :nodoc: all
327
323
  end
328
324
  end
329
325
  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
330
349
  end
331
350
  end
332
351
 
@@ -351,17 +370,17 @@ module Cf # :nodoc: all
351
370
  end
352
371
  end
353
372
  if gold_standards.class == Hash#gold_standards["file"]
354
- gold_standard = CF::GoldStandard.new(gold_standards.merge!({:line => line}))
355
- say_status "GoldStandard from file", "'#{gold_standards[:file]}'"
356
- display_error(line_title, "#{gold_standard.errors}") if gold_standard.errors.present?
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?
357
376
  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
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"]}'"
380
+ display_error(line_title, "#{gold_standard.errors}") if gold_standard.errors.present?
363
381
  end
364
382
  end
383
+ end
365
384
 
366
385
  say " ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁ ☁", :white
367
386
  say "Line was successfully created.", :green
@@ -30,8 +30,6 @@ 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
35
33
  custom_task_form:
36
34
  form_title: Clink a link and paste in url
37
35
  instruction: Look through search results and find the best date
@@ -65,16 +65,3 @@ 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 :badges
48
+ attr_accessor :badge
49
49
 
50
50
  # ==Initializes a new "worker" object
51
51
  # ==Usage of HumanWorker.new(hash):
@@ -72,26 +72,34 @@ 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]
76
- @badges = options[:badge]
75
+ @stat_badge = options[:stat_badge].nil? ? nil : options[:stat_badge]
77
76
  if @station
78
- request =
79
- {
80
- :body =>
77
+ if options[:stat_badge].nil?
78
+ request =
81
79
  {
82
- :api_key => CF.api_key,
83
- :worker => {:number => @number, :reward => @reward, :type => "HumanWorker"},
84
- :badge => options[:badge],
85
- :stat_badge => options[:stat_badge]
80
+ :body =>
81
+ {
82
+ :api_key => CF.api_key,
83
+ :worker => {:number => @number, :reward => @reward, :type => "HumanWorker"}
84
+ }
86
85
  }
87
- }
86
+ else
87
+ request =
88
+ {
89
+ :body =>
90
+ {
91
+ :api_key => CF.api_key,
92
+ :worker => {:number => @number, :reward => @reward, :type => "HumanWorker"},
93
+ :stat_badge => options[:stat_badge]
94
+ }
95
+ }
96
+ end
88
97
  resp = HTTParty.post("#{CF.api_url}#{CF.api_version}/lines/#{CF.account_name}/#{@station.line['title'].downcase}/stations/#{@station.index}/workers.json",request)
89
98
 
90
99
  self.id = resp.parsed_response['id']
91
100
  self.number = resp.parsed_response['number']
92
101
  self.reward = resp.parsed_response['reward']
93
102
  self.stat_badge = resp.parsed_response['stat_badge']
94
- self.badges = resp.parsed_response['badges']
95
103
 
96
104
  if resp.code != 200
97
105
  self.errors = resp.parsed_response['error']['message']
@@ -101,8 +109,52 @@ module CF
101
109
  end
102
110
  end
103
111
 
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
+
104
156
  def to_s # :nodoc:
105
- "{:id => => #{self.id}, :number => #{self.number}, :reward => #{self.reward}, :stat_badge => #{self.stat_badge}, :errors => #{self.errors},:badges =>#{self.badges}}"
157
+ "{:id => => #{self.id}, :number => #{self.number}, :reward => #{self.reward}, :stat_badge => #{self.stat_badge}, :errors => #{self.errors}}"
106
158
  end
107
159
  end
108
160
  end