cloudfactory 0.0.13

Sign up to get free protection for your applications and to get access to all the features.
Files changed (116) hide show
  1. data/.gitignore +11 -0
  2. data/.rspec +1 -0
  3. data/Gemfile +10 -0
  4. data/README.md +44 -0
  5. data/Rakefile +40 -0
  6. data/bin/cf +8 -0
  7. data/cf.gemspec +76 -0
  8. data/example/google_translate_app/Gemfile +12 -0
  9. data/example/google_translate_app/config.ru +7 -0
  10. data/example/google_translate_app/google_translate_input.csv +4 -0
  11. data/example/google_translate_app/google_translator_app.rb +53 -0
  12. data/example/google_translate_app/views/index.haml +2 -0
  13. data/example/google_translate_app/views/layout.haml +7 -0
  14. data/example/google_translate_app/views/run.haml +4 -0
  15. data/example/google_translate_app/views/style.sass +2 -0
  16. data/example/human_worker_app/Gemfile +12 -0
  17. data/example/human_worker_app/config.ru +7 -0
  18. data/example/human_worker_app/human_worker_app.rb +55 -0
  19. data/example/human_worker_app/human_worker_input.csv +5 -0
  20. data/example/human_worker_app/public/app.js +12 -0
  21. data/example/human_worker_app/temp.csv +3 -0
  22. data/example/human_worker_app/views/index.haml +15 -0
  23. data/example/human_worker_app/views/layout.haml +10 -0
  24. data/example/human_worker_app/views/result.haml +18 -0
  25. data/example/human_worker_app/views/run.haml +12 -0
  26. data/example/human_worker_app/views/style.sass +25 -0
  27. data/example/sample_yaml_files/concept_tagging_robot.yaml +18 -0
  28. data/example/sample_yaml_files/content_scraping_robot.yaml +19 -0
  29. data/example/sample_yaml_files/entity_extraction_robot.yaml +18 -0
  30. data/example/sample_yaml_files/google_translate_robot.yaml +20 -0
  31. data/example/sample_yaml_files/image_processing_robot.yaml +20 -0
  32. data/example/sample_yaml_files/keyword_matching_and_text_extraction_robot.yaml +26 -0
  33. data/example/sample_yaml_files/mailer_robot.yaml +21 -0
  34. data/example/sample_yaml_files/media_converter_robot.yaml +21 -0
  35. data/example/sample_yaml_files/media_splitting_robot.yaml +20 -0
  36. data/example/sample_yaml_files/multiple_skill_badge.yaml +75 -0
  37. data/example/sample_yaml_files/sentiment_robot.yaml +19 -0
  38. data/example/sample_yaml_files/skill_badge.yaml +56 -0
  39. data/example/sample_yaml_files/stat_badge.yaml +40 -0
  40. data/example/sample_yaml_files/term_extraction_robot.yaml +20 -0
  41. data/example/sample_yaml_files/tournament_station_and_form_fields.yaml +40 -0
  42. data/features/form_generation.feature +46 -0
  43. data/features/form_preview.feature +98 -0
  44. data/features/line_creation.feature +99 -0
  45. data/features/line_deletion.feature +50 -0
  46. data/features/line_generation.feature +57 -0
  47. data/features/run.feature +141 -0
  48. data/features/support/cli_steps.rb +16 -0
  49. data/features/support/env.rb +23 -0
  50. data/features/target_url.feature +82 -0
  51. data/fixtures/api_credentials_example.yml +4 -0
  52. data/fixtures/input_data/media_converter_robot.csv +2 -0
  53. data/fixtures/input_data/test.csv +2 -0
  54. data/lib/cf.rb +94 -0
  55. data/lib/cf/account.rb +32 -0
  56. data/lib/cf/cli.rb +52 -0
  57. data/lib/cf/cli/config.rb +87 -0
  58. data/lib/cf/cli/form.rb +82 -0
  59. data/lib/cf/cli/line.rb +237 -0
  60. data/lib/cf/cli/production.rb +62 -0
  61. data/lib/cf/cli/templates/css_file.css.erb +22 -0
  62. data/lib/cf/cli/templates/form_preview.html.erb +17 -0
  63. data/lib/cf/cli/templates/html_file.html.erb +21 -0
  64. data/lib/cf/cli/templates/js_file.js.erb +18 -0
  65. data/lib/cf/cli/templates/line.tt +55 -0
  66. data/lib/cf/cli/templates/sample-line/form.css +27 -0
  67. data/lib/cf/cli/templates/sample-line/form.html +26 -0
  68. data/lib/cf/cli/templates/sample-line/form.js +7 -0
  69. data/lib/cf/cli/templates/sample-line/line.yml.erb +67 -0
  70. data/lib/cf/cli/templates/sample-line/sample-line.csv +3 -0
  71. data/lib/cf/client.rb +56 -0
  72. data/lib/cf/custom_task_form.rb +136 -0
  73. data/lib/cf/department.rb +24 -0
  74. data/lib/cf/final_output.rb +20 -0
  75. data/lib/cf/form_field.rb +62 -0
  76. data/lib/cf/human_worker.rb +67 -0
  77. data/lib/cf/input_format.rb +134 -0
  78. data/lib/cf/line.rb +231 -0
  79. data/lib/cf/robot_worker.rb +31 -0
  80. data/lib/cf/run.rb +158 -0
  81. data/lib/cf/station.rb +340 -0
  82. data/lib/cf/task_form.rb +147 -0
  83. data/lib/cf/version.rb +3 -0
  84. data/lib/generators/cf/form/form_generator.rb +55 -0
  85. data/lib/generators/cf/form/templates/cf_form.html.erb +11 -0
  86. data/lib/generators/cf/install/install_generator.rb +22 -0
  87. data/lib/generators/cf/install/templates/README +13 -0
  88. data/lib/generators/cf/install/templates/cloud_factory.rb +7 -0
  89. data/spec/account_spec.rb +11 -0
  90. data/spec/badge_spec.rb +88 -0
  91. data/spec/concept_tagging_robot_spec.rb +54 -0
  92. data/spec/config_spec.rb +20 -0
  93. data/spec/content_scraping_robot_spec.rb +58 -0
  94. data/spec/custom_task_form_spec.rb +190 -0
  95. data/spec/department_spec.rb +14 -0
  96. data/spec/entity_extraction_robot_spec.rb +56 -0
  97. data/spec/form_field_spec.rb +126 -0
  98. data/spec/generators/form_generator_spec.rb +60 -0
  99. data/spec/generators/install_generator_spec.rb +35 -0
  100. data/spec/google_translate_robot_spec.rb +64 -0
  101. data/spec/human_worker_spec.rb +118 -0
  102. data/spec/image_processing_robot_spec.rb +56 -0
  103. data/spec/input_format_spec.rb +113 -0
  104. data/spec/keyword_matching_and_text_extraction_robot_spec.rb +73 -0
  105. data/spec/line_spec.rb +338 -0
  106. data/spec/mailer_robot_spec.rb +62 -0
  107. data/spec/media_converter_robot_spec.rb +72 -0
  108. data/spec/media_splitting_robot_spec.rb +62 -0
  109. data/spec/run_spec.rb +298 -0
  110. data/spec/sentiment_robot_spec.rb +56 -0
  111. data/spec/spec_helper.rb +39 -0
  112. data/spec/station_spec.rb +256 -0
  113. data/spec/task_form_spec.rb +96 -0
  114. data/spec/term_extraction_robot_spec.rb +58 -0
  115. data/spec/text_appending_robot_spec.rb +86 -0
  116. metadata +472 -0
data/lib/cf/station.rb ADDED
@@ -0,0 +1,340 @@
1
+ module CF
2
+ class Station
3
+ require 'httparty'
4
+ include Client
5
+
6
+ # type of the station, e.g. station = Station.new(line, {:type => "Work"})
7
+ attr_accessor :type
8
+
9
+ # line attribute is parent attribute for station & is required for making Api call
10
+ attr_accessor :line_title
11
+
12
+ # ID of the station
13
+ attr_accessor :id, :extra, :except, :index, :line, :station_input_formats, :jury_worker, :auto_judge, :errors
14
+
15
+ # ==Initializes a new station
16
+ # ===Usage Example
17
+ # line = CF::Line.new("Digitize", "Survey")
18
+ # station = CF::Station.new({:line => line, :type => "Work"})
19
+ def initialize(options={})
20
+ @input_formats =[]
21
+ @line_title = options[:line].nil? ? nil : options[:line].title
22
+ @type = options[:type].nil? ? nil : options[:type].camelize
23
+ @jury_worker = options[:jury_worker]
24
+ @auto_judge = options[:auto_judge]
25
+ @station_input_formats = options[:input_formats]
26
+ @line_instance = options[:line]
27
+ request_general =
28
+ {
29
+ :body =>
30
+ {
31
+ :api_key => CF.api_key,
32
+ :station => {:type => @type, :input_formats => @station_input_formats}
33
+ }
34
+ }
35
+ request_tournament =
36
+ {
37
+ :body =>
38
+ {
39
+ :api_key => CF.api_key,
40
+ :station => {:type => @type, :jury_worker => @jury_worker, :auto_judge => @auto_judge, :input_formats => @station_input_formats}
41
+ }
42
+ }
43
+ if @line_title
44
+ if @type == "Improve"
45
+ line = options[:line]
46
+ if line.stations.size < 1
47
+ raise ImproveStationNotAllowed.new("You cannot add Improve Station as a first station of a line")
48
+ else
49
+ resp = HTTParty.post("#{CF.api_url}#{CF.api_version}/lines/#{CF.account_name}/#{@line_instance.title.downcase}/stations.json",request_general)
50
+ end
51
+ elsif @type == "Tournament"
52
+ resp = HTTParty.post("#{CF.api_url}#{CF.api_version}/lines/#{CF.account_name}/#{@line_instance.title.downcase}/stations.json",request_tournament)
53
+ else
54
+ resp = HTTParty.post("#{CF.api_url}#{CF.api_version}/lines/#{CF.account_name}/#{@line_instance.title.downcase}/stations.json",request_general)
55
+ end
56
+ resp.to_hash.each_pair do |k,v|
57
+ self.send("#{k}=",v) if self.respond_to?(k)
58
+ end
59
+ @line_instance.stations = self
60
+ if resp.response.code != "200"
61
+ self.errors = resp.parsed_response['error']['message']
62
+ end
63
+ end
64
+ end
65
+
66
+ # ==Initializes a new station within block
67
+ # ===Usage Example
68
+ # ===Creating station using block variable
69
+ # line = CF::Line.create("Digitize Card","Digitization") do |l|
70
+ # CF::InputFormat.new({:line => l, :label => "Company", :field_type => "text_data", :value => "Google", :required => true, :validation_format => "general"})
71
+ # CF::InputFormat.new({:line => l, :label => "Website", :field_type => "text_data", :value => "www.google.com", :required => true, :validation_format => "url"})
72
+ # CF::Station.create({:line => l, :type => "work") do |s|
73
+ # CF::HumanWorker.new({:station => s, :number => 1, :reward => 20)
74
+ # CF::Form.create({:station => s, :title => "Enter text from a business card image", :description => "Describe"}) do |i|
75
+ # CF::FormField.new({:form => i, :label => "First Name", :field_type => "SA", :required => "true"})
76
+ # CF::FormField.new({:form => i, :label => "Middle Name", :field_type => "SA"})
77
+ # CF::FormField.new({:form => i, :label => "Last Name", :field_type => "SA", :required => "true"})
78
+ # end
79
+ # end
80
+ # end
81
+ #
82
+ # ===OR creating without variable within block
83
+ # line = CF::Line.create("Digitize Card","Digitization") do
84
+ # CF::InputFormat.new({:line => self, :label => "Company", :field_type => "text_data", :value => "Google", :required => true, :validation_format => "general"})
85
+ # CF::InputFormat.new({:line => self, :label => "Website", :field_type => "text_data", :value => "www.google.com", :required => true, :validation_format => "url"})
86
+ # CF::Station.create({:line => self, :type => "work") do
87
+ # CF::HumanWorker.new({:station => self, :number => 1, :reward => 20)
88
+ # CF::Form.create({:station => self, :title => "Enter text from a business card image", :description => "Describe"}) do
89
+ # CF::FormField.new({:form => self, :label => "First Name", :field_type => "SA", :required => "true"})
90
+ # CF::FormField.new({:form => self, :label => "Middle Name", :field_type => "SA"})
91
+ # CF::FormField.new({:form => self, :label => "Last Name", :field_type => "SA", :required => "true"})
92
+ # end
93
+ # end
94
+ # end
95
+ def self.create(options, &block)
96
+ station = Station.new(options)
97
+ if block.arity >= 1
98
+ block.call(station)
99
+ else
100
+ station.instance_eval &block
101
+ end
102
+ station
103
+ end
104
+
105
+ # ==Creates new form for station object
106
+ # ===Usage of worker method for "station" object
107
+ # line = CF::Line.new("line name", "Survey")
108
+ # station = CF::Station.new({:type => "work"})
109
+ # line.stations station
110
+ # human_worker = CF::HumanWorker.new({:number => 1, :reawrd => 20})
111
+ # line.stations.first.worker human_worker
112
+ def worker worker_instance = nil
113
+ if worker_instance
114
+ @worker_instance = worker_instance
115
+ else
116
+ @worker_instance
117
+ end
118
+ end
119
+
120
+ def worker=(worker_instance) # :nodoc:
121
+ worker_type = worker_instance.class.to_s.split("::").last
122
+ if worker_type == "HumanWorker"
123
+ if worker_instance.station
124
+ @worker_instance = worker_instance
125
+ else
126
+ number = worker_instance.number
127
+ reward = worker_instance.reward
128
+ badge = worker_instance.badge
129
+ stat_badge = worker_instance.stat_badge
130
+ if badge.nil? && stat_badge.nil?
131
+ request =
132
+ {
133
+ :body =>
134
+ {
135
+ :api_key => CF.api_key,
136
+ :worker => {:number => number, :reward => reward, :type => "HumanWorker"}
137
+ }
138
+ }
139
+ else
140
+ request =
141
+ {
142
+ :body =>
143
+ {
144
+ :api_key => CF.api_key,
145
+ :worker => {:number => number, :reward => reward, :type => "HumanWorker"},
146
+ :skill_badge => badge,
147
+ :stat_badge => stat_badge
148
+ }
149
+ }
150
+ end
151
+ resp = HTTParty.post("#{CF.api_url}#{CF.api_version}/lines/#{CF.account_name}/#{self.line_title.downcase}/stations/#{self.index}/workers.json",request)
152
+ worker = CF::HumanWorker.new({})
153
+ worker.id = resp.parsed_response['id']
154
+ worker.number = resp.parsed_response['number']
155
+ worker.reward = resp.parsed_response['reward']
156
+ worker.stat_badge = resp.parsed_response['stat_badge']
157
+ worker.skill_badges << resp.parsed_response['skill_badges']
158
+ if resp.code != 200
159
+ worker.errors = resp.parsed_response['error']['message']
160
+ end
161
+ @worker_instance = worker
162
+ end
163
+
164
+ elsif worker_type == "RobotWorker"
165
+ if worker_instance.station
166
+ @worker_instance = worker_instance
167
+ else
168
+ @settings = worker_instance.settings
169
+ @type = worker_instance.type
170
+ request = @settings.merge(:type => @type)
171
+ resp = CF::RobotWorker.post("/lines/#{CF.account_name}/#{self.line_title.downcase}/stations/#{self.index}/workers.json", :worker => request)
172
+ worker = CF::RobotWorker.new({})
173
+ worker.settings = @settings
174
+ worker.type = @type
175
+ worker.number = resp.number
176
+ worker.reward = resp.reward
177
+ if resp.code != 200
178
+ worker.errors = resp.error.message
179
+ end
180
+ @worker_instance = worker
181
+ end
182
+ end
183
+ end
184
+
185
+ # ==Creates new form for station object
186
+ # ===Usage of Instruction method for "station" object
187
+ # line = CF::Line.new("line name", "Survey")
188
+ # station = CF::Station.new({:type => "work"})
189
+ # line.stations station
190
+ # standard_form = CF::Form.new({:title => "title", :description => "description"})
191
+ # line.stations.first.form form
192
+ def form form_instance = nil
193
+ if form_instance
194
+ @form_instance = form_instance
195
+ else
196
+ @form_instance
197
+ end
198
+ end
199
+
200
+ def form=(form_instance) # :nodoc:
201
+ if form_instance.class == Hash
202
+ form_type = form_instance['_type']
203
+ @form = eval(form_type.camelize).new({})
204
+ form_instance.to_hash.each_pair do |k,v|
205
+ @form.send("#{k}=",v) if @form.respond_to?(k)
206
+ end
207
+ else
208
+ @form = form_instance
209
+ end
210
+
211
+ if @form.station
212
+ @form_instance = @form
213
+ else
214
+ @title = @form.title
215
+ @instruction = @form.instruction
216
+ type = @form.class.to_s.split("::").last
217
+ form = @form.class.new({})
218
+ if type == "CustomTaskForm"
219
+ @html = @form.raw_html
220
+ @css = @form.raw_css
221
+ @javascript = @form.raw_javascript
222
+ @resp = CF::CustomTaskForm.post("/lines/#{CF.account_name}/#{self.line_title.downcase}/stations/#{self.index}/form.json", :form => {:title => @title, :instruction => @instruction, :_type => "CustomTaskForm", :raw_html => @html, :raw_css => @css, :raw_javascript => @javascript})
223
+ else
224
+ @resp = CF::TaskForm.post("/lines/#{CF.account_name}/#{self.line_title.downcase}/stations/#{self.index}/form.json", :form => {:title => @title, :instruction => @instruction, :_type => type})
225
+ end
226
+ @resp.to_hash.each_pair do |k,v|
227
+ form.send("#{k}=",v) if form.respond_to?(k)
228
+ end
229
+ form.station = self
230
+ @form_instance = form
231
+ end
232
+ end
233
+
234
+ # ==Usage of line.input_formats(input_format)
235
+ # line = CF::Line.new("line name", "Survey")
236
+ # station = CF::Station.new({:type => "work"})
237
+ # line.stations station
238
+ # input_format = CF::InputFormat.new({:line => l, :label => "Website", :field_type => "text_data", :value => "www.google.com", :required => true, :validation_format => "url"})
239
+ # line.stations.first.input_formats input_format
240
+ # * returns
241
+ # line.stationss.first.input_formats as an array of input_formats
242
+ def input_formats input_formats_value = nil
243
+ if input_formats_value
244
+ name = input_formats_value.name
245
+ required = input_formats_value.required
246
+ valid_type = input_formats_value.valid_type
247
+ resp = CF::InputFormat.post("/lines/#{CF.account_name}/#{self.line_title.downcase}/input_formats.json", :input_format => {:name => name, :required => required, :valid_type => valid_type})
248
+ input_format = CF::InputFormat.new()
249
+ resp.input_format.to_hash.each_pair do |k,v|
250
+ input_format.send("#{k}=",v) if input_format.respond_to?(k)
251
+ end
252
+ @input_formats << input_format
253
+ else
254
+ @input_formats.first
255
+ end
256
+ end
257
+ def input_formats=(input_formats_value) # :nodoc:
258
+ @input_formats << input_formats_value
259
+ end
260
+ # ==Updates Standard Instruction of a station
261
+ # ===Usage example
262
+ # line = CF::Line.create("Digitize Card","Digitization") do |l|
263
+ # CF::Station.create({:line => l, :type => "work") do |s|
264
+ # CF::HumanWorker.new({:station => s, :number => 1, :reward => 20)
265
+ # end
266
+ # end
267
+ # line.stations[0].update_form({:title => "Enter phone number from a business card image", :description => "Call"})
268
+ def update_form(options={})
269
+ @title = options[:title]
270
+ @description = options[:description]
271
+ self.class.put("/stations/#{self.id}/form.json", :form => {:title => @title, :description => @description, :type => "Form"})
272
+ end
273
+
274
+ # ==Returns a particular station of a line
275
+ # ===Usage example for get_station() method
276
+ # line = CF::Line.create("Digitize Card", "4dc8ad6572f8be0600000001")
277
+ # station = CF::Station.new({:line => line, :type => "Work"})
278
+ # line.stations station
279
+ #
280
+ # got_station = line.stations[0].get
281
+ # returns the station object
282
+ def get
283
+ resp = self.class.get("/lines/#{CF.account_name}/#{self.line_title.downcase}/stations/#{self.index}.json")
284
+ return resp
285
+ end
286
+
287
+ # ==Returns information of form
288
+ # ===Usage example
289
+ # line = CF::Line.create("Digitize Card","Digitization") do |l|
290
+ # CF::InputFormat.new({:line => l, :label => "Company", :field_type => "text_data", :value => "Google", :required => true, :validation_format => "general"})
291
+ # CF::InputFormat.new({:line => l, :label => "Website", :field_type => "text_data", :value => "www.google.com", :required => true, :validation_format => "url"})
292
+ # CF::Station.create({:line => l, :type => "work") do |s|
293
+ # CF::HumanWorker.new({:station => s, :number => 1, :reward => 20)
294
+ # CF::Form.create({:station => s, :title => "Enter text from a business card image", :description => "Describe"}) do |i|
295
+ # CF::FormField.new({:form => i, :label => "First Name", :field_type => "SA", :required => "true"})
296
+ # CF::FormField.new({:form => i, :label => "Middle Name", :field_type => "SA"})
297
+ # CF::FormField.new({:form => i, :label => "Last Name", :field_type => "SA", :required => "true"})
298
+ # end
299
+ # end
300
+ # end
301
+ #
302
+ # @got_form = line.stations[0].get_form
303
+ def get_form
304
+ self.class.get("/lines/#{CF.account_name}/#{self.line_title.downcase}/stations/#{self.index}/form.json")
305
+ end
306
+
307
+ # ==Returns all the stations associated with a particular line
308
+ # ===Usage example for station.all method is
309
+ # line = CF::Line.create("Digitize Card","Digitization") do |l|
310
+ # CF::InputFormat.new({:line => l, :label => "Company", :field_type => "text_data", :value => "Google", :required => true, :validation_format => "general"})
311
+ # CF::InputFormat.new({:line => l, :label => "Website", :field_type => "text_data", :value => "www.google.com", :required => true, :validation_format => "url"})
312
+ # CF::Station.create({:line => l, :type => "work") do |s|
313
+ # CF::HumanWorker.new({:station => s, :number => 1, :reward => 20)
314
+ # CF::Form.create({:station => s, :title => "Enter text from a business card image", :description => "Describe"}) do |i|
315
+ # CF::FormField.new({:form => i, :label => "First Name", :field_type => "SA", :required => "true"})
316
+ # CF::FormField.new({:form => i, :label => "Middle Name", :field_type => "SA"})
317
+ # CF::FormField.new({:form => i, :label => "Last Name", :field_type => "SA", :required => "true"})
318
+ # end
319
+ # end
320
+ # end
321
+ #
322
+ # CF::Station.all(line)
323
+ # returns all stations
324
+ def self.all(line)
325
+ get("/lines/#{CF.account_name}/#{line.title.downcase}/stations.json")
326
+ end
327
+
328
+ # ==Deletes a station
329
+ # * We need to pass line object with which desired station associated with as an argument to delete a station
330
+ # ===Usage example for delete method
331
+ # line = CF::Line.new("Digitize Card", "4dc8ad6572f8be0600000001")
332
+ # station = CF::Station.new({:line =. line, :type => "Work"})
333
+ # line.stations station
334
+ #
335
+ # station.delete
336
+ def delete
337
+ self.class.delete("/lines/#{CF.account_name}/#{self.line_title.downcase}/stations/#{self.index}.json")
338
+ end
339
+ end
340
+ end
@@ -0,0 +1,147 @@
1
+ module CF
2
+ class TaskForm
3
+ require 'httparty'
4
+ include Client
5
+
6
+ # title of the standard_instruction
7
+ attr_accessor :title
8
+
9
+ # instruction about the instruction
10
+ attr_accessor :instruction
11
+
12
+ # form_fields for the Form
13
+ attr_accessor :form_fields
14
+
15
+ # station attributes required to store station information
16
+ attr_accessor :station
17
+
18
+ # ID of Standard Instruction
19
+ attr_accessor :id
20
+
21
+ # ==Initializes a new Form
22
+ # ==Usage of standard_instruction.new
23
+ # attrs = {:title => "Enter text from a business card image",
24
+ # :instruction => "Describe"}
25
+ #
26
+ # instruction = CF::Form.new(attrs)
27
+ def initialize(options={})
28
+ @form_fields =[]
29
+ @station = options[:station]
30
+ @title = options[:title]
31
+ @instruction = options[:instruction]
32
+ if !@station.nil?
33
+ resp = self.class.post("/lines/#{CF.account_name}/#{@station.line['title'].downcase}/stations/#{@station.index}/form.json", :form => {:title => @title, :instruction => @instruction, :_type => "TaskForm"})
34
+ resp.to_hash.each_pair do |k,v|
35
+ self.send("#{k}=",v) if self.respond_to?(k)
36
+ end
37
+ @station.form = self
38
+ end
39
+ end
40
+
41
+ # ==Initializes a new Form within block using Variable
42
+ # ==Usage of "standard_instruction.create(hash) do |block|"
43
+ # ===Creating Form using block variable
44
+ # line = CF::Line.create("Digitize Card","Digitization") do |l|
45
+ # CF::InputHeader.new({:line => l, :label => "Company", :field_type => "text_data", :value => "Google", :required => true, :validation_format => "general"})
46
+ # CF::InputHeader.new({:line => l, :label => "Website", :field_type => "text_data", :value => "www.google.com", :required => true, :validation_format => "url"})
47
+ # CF::Station.create({:line => l, :type => "work") do |s|
48
+ # CF::HumanWorker.new({:station => s, :number => 1, :reward => 20)
49
+ # CF::Form.create(:station => s, :title => "Enter text from a business card image", :instruction => "Describe"}) do |i|
50
+ # CF::FormField.new({:instruction => i, :label => "First Name", :field_type => "SA", :required => "true"})
51
+ # CF::FormField.new({:instruction => i, :label => "Middle Name", :field_type => "SA"})
52
+ # CF::FormField.new({:instruction => i, :label => "Last Name", :field_type => "SA", :required => "true"})
53
+ # end
54
+ # end
55
+ # end
56
+ #
57
+ # ===OR without block variable
58
+ # line = CF::Line.create("Digitize Card","Digitization") do
59
+ # CF::InputHeader.new({:line => self, :label => "Company", :field_type => "text_data", :value => "Google", :required => true, :validation_format => "general"})
60
+ # CF::InputHeader.new({:line => self, :label => "Website", :field_type => "text_data", :value => "www.google.com", :required => true, :validation_format => "url"})
61
+ # CF::Station.create({:line => self, :type => "work") do
62
+ # CF::HumanWorker.new({:station => self, :number => 1, :reward => 20)
63
+ # CF::Form.create(:station => self, :title => "Enter text from a business card image", :instruction => "Describe"}) do
64
+ # CF::FormField.new({:instruction => self, :label => "First Name", :field_type => "SA", :required => "true"})
65
+ # CF::FormField.new({:instruction => self, :label => "Middle Name", :field_type => "SA"})
66
+ # CF::FormField.new({:instruction => self, :label => "Last Name", :field_type => "SA", :required => "true"})
67
+ # end
68
+ # end
69
+ # end
70
+ #
71
+ def self.create(options, &block)
72
+ form = TaskForm.new(options)
73
+ if block.arity >= 1
74
+ block.call(form)
75
+ else
76
+ form.instance_eval &block
77
+ end
78
+ form
79
+ end
80
+
81
+ # ==appending different form_fields
82
+ # ===Syntax for FormField method is form_fields << form_field_values
83
+ # ==Usage of form_fields method
84
+ # line = CF::Line.create("Digitize Card","Digitization") do |l|
85
+ # CF::InputHeader.new({:line => l, :label => "Company", :field_type => "text_data", :value => "Google", :required => true, :validation_format => "general"})
86
+ # CF::InputHeader.new({:line => l, :label => "Website", :field_type => "text_data", :value => "www.google.com", :required => true, :validation_format => "url"})
87
+ # CF::Station.create({:line => l, :type => "work") do |s|
88
+ # CF::HumanWorker.new({:station => s, :number => 1, :reward => 20)
89
+ # CF::Form.create(:station => s, :title => "Enter text from a business card image", :instruction => "Describe"}) do |i|
90
+ # CF::FormField.new({:instruction => i, :label => "First Name", :field_type => "SA", :required => "true"})
91
+ # CF::FormField.new({:instruction => i, :label => "Middle Name", :field_type => "SA"})
92
+ # CF::FormField.new({:instruction => i, :label => "Last Name", :field_type => "SA", :required => "true"})
93
+ # end
94
+ # end
95
+ # end
96
+ #
97
+ def form_fields form_fields = nil
98
+ if form_fields
99
+ form_field_params = form_fields.form_field_params
100
+ party_param =
101
+ {
102
+ :body =>
103
+ {
104
+ :api_key => CF.api_key,
105
+ :form_field => form_field_params
106
+ }
107
+ }
108
+ resp = HTTParty.post("#{CF.api_url}#{CF.api_version}/lines/#{CF.account_name}/#{self.station.line_title.downcase}/stations/#{self.station.index}/form_fields.json",party_param)
109
+ form_field = CF::FormField.new({})
110
+ resp.parsed_response.to_hash.each_pair do |k,v|
111
+ form_field.send("#{k}=",v) if form_field.respond_to?(k)
112
+ end
113
+ if resp.code != 200
114
+ form_field.errors = resp.parsed_response['error']['message']
115
+ end
116
+ form_field.form_field_params = form_field_params
117
+ @form_fields << form_field
118
+ else
119
+ @form_fields
120
+ end
121
+ end
122
+ def form_fields=(form_fields) # :nodoc:
123
+ @form_fields << form_fields
124
+ end
125
+ # ==Deletes Standard Instruction of a station
126
+ # ==Usage example
127
+ # line = CF::Line.create("Digitize Card","Digitization") do |l|
128
+ # CF::InputHeader.new({:line => l, :label => "Company", :field_type => "text_data", :value => "Google", :required => true, :validation_format => "general"})
129
+ # CF::InputHeader.new({:line => l, :label => "Website", :field_type => "text_data", :value => "www.google.com", :required => true, :validation_format => "url"})
130
+ # CF::Station.create({:line => l, :type => "work") do |s|
131
+ # CF::HumanWorker.new({:station => s, :number => 1, :reward => 20)
132
+ # CF::Form.create(:station => s, :title => "Enter text from a business card image", :instruction => "Describe"}) do |i|
133
+ # CF::FormField.new({:instruction => i, :label => "First Name", :field_type => "SA", :required => "true"})
134
+ # CF::FormField.new({:instruction => i, :label => "Middle Name", :field_type => "SA"})
135
+ # CF::FormField.new({:instruction => i, :label => "Last Name", :field_type => "SA", :required => "true"})
136
+ # end
137
+ # end
138
+ # end
139
+ # station = line.stations[0]
140
+ #
141
+ # CF::Form.delete_instruction(station)
142
+ # deletes standard_instruction of a station
143
+ def self.delete_instruction(station)
144
+ delete("/lines/#{CF.account_name}/#{station.line_title.downcase}/stations/#{station.index}/form.json")
145
+ end
146
+ end
147
+ end