Class: CF::Station

Inherits:
Object
  • Object
show all
Includes:
Client
Defined in:
lib/cf/station.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Station

Initializes a new station

Usage Example:

line = CF::Line.new("Digitize", "Survey")
station = CF::Station.new({:type => "Work"})
line.stations station


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/cf/station.rb', line 41

def initialize(options={})
  @input_formats =[]
  @badges = []
  @line_title = options[:line].nil? ? nil : options[:line].title
  @type = options[:type].nil? ? nil : options[:type].camelize
  @jury_worker = options[:jury_worker]
  @auto_judge = options[:auto_judge]
  @station_input_formats = options[:input_formats]
  @line_instance = options[:line]
  @batch_size = options[:batch_size]
  @gold_standards = []
  @gold_fields = options[:gold_fields]
  @acceptance_ratio = options[:acceptance_ratio]
  if @batch_size.nil?
    request_general =
    {
      :body =>
      {
        :api_key => CF.api_key,
        :station => {:type => @type, :input_formats => @station_input_formats, :gold_fields => @gold_fields}
      }
    }
    if @acceptance_ratio.nil?
      request_tournament =
      {
        :body =>
        {
          :api_key => CF.api_key,
          :station => {:type => @type, :jury_worker => @jury_worker, :auto_judge => @auto_judge, :input_formats => @station_input_formats, :gold_fields => @gold_fields}
        }
      }
    else
      request_tournament =
      {
        :body =>
        {
          :api_key => CF.api_key,
          :station => {:type => @type, :jury_worker => @jury_worker, :auto_judge => @auto_judge, :input_formats => @station_input_formats, :acceptance_ratio => @acceptance_ratio, :gold_fields => @gold_fields}
        }
      }
    end
  else
    request_general =
    {
      :body =>
      {
        :api_key => CF.api_key,
        :station => {:type => @type, :input_formats => @station_input_formats, :batch_size => @batch_size, :gold_fields => @gold_fields}
      }
    }
    if @acceptance_ratio.nil?
      request_tournament =
      {
        :body =>
        {
          :api_key => CF.api_key,
          :station => {:type => @type, :jury_worker => @jury_worker, :auto_judge => @auto_judge, :input_formats => @station_input_formats, :batch_size => @batch_size, :gold_fields => @gold_fields}
        }
      }
    else
      request_tournament =
      {
        :body =>
        {
          :api_key => CF.api_key,
          :station => {:type => @type, :jury_worker => @jury_worker, :auto_judge => @auto_judge, :input_formats => @station_input_formats, :batch_size => @batch_size, :acceptance_ratio => @acceptance_ratio, :gold_fields => @gold_fields}
        }
      }
    end
  end
  if @line_title
    if @type == "Improve"
      line = options[:line]
      if line.stations.size < 1
        raise ImproveStationNotAllowed.new("You cannot add Improve Station as a first station of a line")
      else
        resp = HTTParty.post("#{CF.api_url}#{CF.api_version}/lines/#{CF.}/#{@line_instance.title.downcase}/stations.json",request_general)
      end
    elsif @type == "Tournament"
      resp = HTTParty.post("#{CF.api_url}#{CF.api_version}/lines/#{CF.}/#{@line_instance.title.downcase}/stations.json",request_tournament)
    else
      resp = HTTParty.post("#{CF.api_url}#{CF.api_version}/lines/#{CF.}/#{@line_instance.title.downcase}/stations.json",request_general)
    end
    resp.to_hash.each_pair do |k,v|
      self.send("#{k}=",v) if self.respond_to?(k)
    end
    @line_instance.stations = self
    if resp.response.code != "200"
      self.errors = resp.parsed_response['error']['message']
    end
  end
end

Instance Attribute Details

#auto_judgeObject

Auto Judge settings for the Tournament Station



25
26
27
# File 'lib/cf/station.rb', line 25

def auto_judge
  @auto_judge
end

#batch_sizeObject

batch_size attribute, required for specifing size of batch



34
35
36
# File 'lib/cf/station.rb', line 34

def batch_size
  @batch_size
end

#errorsObject

Contains Error Message if any



28
29
30
# File 'lib/cf/station.rb', line 28

def errors
  @errors
end

#gold_fieldsObject

Contains gold_feild



31
32
33
# File 'lib/cf/station.rb', line 31

def gold_fields
  @gold_fields
end

#indexObject

Index number of station



13
14
15
# File 'lib/cf/station.rb', line 13

def index
  @index
end

#jury_workerObject

Jury worker settings for the Tournament Station



22
23
24
# File 'lib/cf/station.rb', line 22

def jury_worker
  @jury_worker
end

#lineObject

Line attribute of the station with which station is associated



16
17
18
# File 'lib/cf/station.rb', line 16

def line
  @line
end

#line_titleObject

Title of line with which station is associated with



10
11
12
# File 'lib/cf/station.rb', line 10

def line_title
  @line_title
end

#station_input_formatsObject

Manual input format settings for the station



19
20
21
# File 'lib/cf/station.rb', line 19

def station_input_formats
  @station_input_formats
end

#typeObject

type of the station, e.g. station = Station.new(=> “Work”)



7
8
9
# File 'lib/cf/station.rb', line 7

def type
  @type
end

Class Method Details

.all(line) ⇒ Object

Returns all the stations associated with a particular line

Usage Example:

CF::Station.all(line)

returns all stations



303
304
305
# File 'lib/cf/station.rb', line 303

def self.all(line)
  get("/lines/#{CF.}/#{line.title.downcase}/stations.json")
end

.create(options, &block) ⇒ Object

Creation of a new station

Usage Example

line = CF::Line.create("Digitize Card","Digitization") do |l|
  CF::InputFormat.new({:line => l, :label => "Company", :required => "true", :valid_type => "general"})
  CF::InputFormat.new({:line => l, :label => "Website", :required => "true", :valid_type => "url"})
  CF::Station.create({:line => l, :type => "work") do |s|
    CF::HumanWorker.new({:station => s, :number => 1, :reward => 20)
    CF::Form.create({:station => s, :title => "Enter text from a business card image", :instruction => "Describe"}) do |i|
      CF::FormField.new({:form => i, :label => "First Name", :field_type => "SA", :required => "true"})
      CF::FormField.new({:form => i, :label => "Middle Name", :field_type => "SA"})
      CF::FormField.new({:form => i, :label => "Last Name", :field_type => "SA", :required => "true"})
    end
  end
end


148
149
150
151
152
153
154
155
156
# File 'lib/cf/station.rb', line 148

def self.create(options, &block)
  station = Station.new(options)
  if block.arity >= 1
    block.call(station)
  else
    station.instance_eval &block
  end
  station
end

Instance Method Details

#deleteObject

Deletes a station

  • We need to pass line object with which desired station associated with as an argument to delete a station

Usage example for delete method

line = CF::Line.new("Digitize", "Department_name")
station = CF::Station.new({:type => "Work"})
line.stations station

line.stations.first.delete


315
316
317
318
319
# File 'lib/cf/station.rb', line 315

def delete
  resp = self.class.delete("/lines/#{CF.}/#{self.line_title.downcase}/stations/#{self.index}.json")
  self.errors = resp.error.message if resp.code != 200
  return resp
end

#form(form_instance = nil) ⇒ Object

Creates new form for station object

Usage Example:

line = CF::Line.new("line name", "Survey")
station = CF::Station.new({:type => "work"})
line.stations station
form = CF::Form.new({:title => "title", :instruction => "description"})
line.stations.first.form form


234
235
236
237
238
239
240
# File 'lib/cf/station.rb', line 234

def form form_instance = nil
  if form_instance
    @form_instance = form_instance
  else
    @form_instance
  end
end

#form=(form_instance) ⇒ Object

:nodoc:



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/cf/station.rb', line 242

def form=(form_instance) # :nodoc:
  if form_instance.class == Hash
    form_type = form_instance['_type']
    @form = eval(form_type.camelize).new({})
    form_instance.to_hash.each_pair do |k,v|
      @form.send("#{k}=",v) if @form.respond_to?(k)
    end
  else
    @form = form_instance
  end

  if @form.station
    @form_instance = @form
  else
    @title = @form.title
    @instruction = @form.instruction
    type = @form.class.to_s.split("::").last
    form = @form.class.new({})
    if type == "CustomTaskForm"
      @html = @form.raw_html
      @resp = CF::CustomTaskForm.post("/lines/#{CF.}/#{self.line_title.downcase}/stations/#{self.index}/form.json", :form => {:title => @title, :instruction => @instruction, :_type => "CustomTaskForm", :raw_html => @html})
    else
      @resp = CF::TaskForm.post("/lines/#{CF.}/#{self.line_title.downcase}/stations/#{self.index}/form.json", :form => {:title => @title, :instruction => @instruction, :_type => type})
    end
    @resp.to_hash.each_pair do |k,v|
      form.send("#{k}=",v) if form.respond_to?(k)
    end
    form.errors = @resp['error']['message'] if @resp['code'] != 200
    form.station = self
    @form_instance = form
  end
end

#getObject

Returns a particular station of a line

Usage Example:

line = CF::Line.create("Digitize", "Department_name")
station = CF::Station.new({:type => "Work"})
line.stations station

got_station = line.stations[0].get

returns the station object



283
284
285
286
287
# File 'lib/cf/station.rb', line 283

def get
  resp = self.class.get("/lines/#{CF.}/#{self.line_title.downcase}/stations/#{self.index}.json")
  self.errors = resp['error']['message'] if resp['code'] != 200
  return resp
end

#get_formObject

Returns information of form

Usage example:

@got_form = line.stations[0].get_form


292
293
294
295
296
# File 'lib/cf/station.rb', line 292

def get_form
  resp = self.class.get("/lines/#{CF.}/#{self.line_title.downcase}/stations/#{self.index}/form.json")
  self.errors = resp['error']['message'] if resp['code'] != 200
  return resp
end

#gold_standards(gold_standard = nil) ⇒ Object

specify the goldstandards for the station

Usage Example:

CF::GoldStandard.new({ :station => station,:name => “easy”,:input => [=>“”],:expected_output => [=> {“value” => “John”, “last_name” => => “Lennon”, “company” => => “Sprout”}]})



332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
# File 'lib/cf/station.rb', line 332

def gold_standards gold_standard = nil
  if gold_standard
    station = self
    gold_standard_options = gold_standard.settings
    if !gold_standard_options.nil?
      if gold_standard_options[:file]
        if File.exist?(gold_standard_options[:file].to_s)
          file_upload = File.new(gold_standard_options[:file], 'rb')
          resp = self.class.post("/lines/#{CF.}/#{line.title.downcase}/gold_standards.json", {:file => file_upload,:template => gold_standard_options[:template]})
          gold_standard = CF::GoldStandard.new()
          resp.each do |gs|
            gold_standard.settings.merge!(gs.to_hash)
          end
        end
      else
        request =
        {
          :body =>
          {
            :api_key => CF.api_key,
            :gold_standard => gold_standard_options
          }
        }
        resp = HTTParty.post("#{CF.api_url}#{CF.api_version}/lines/#{CF.}/#{line.title.downcase}/stations/#{station.index.to_i}/gold_standards.json",request)
        gold_standard = CF::GoldStandard.new()
        resp.parsed_response.first.delete("id")
        gold_standard.settings.merge!(resp.parsed_response.first.symbolize_keys)
        self.errors = resp.parsed_response['error']['message'] if resp.code != 200
      end
      @gold_standards << gold_standard
    end
  else
    @gold_standards
  end
end

#gold_standards=(gold_standard) ⇒ Object

:nodoc:



368
369
370
# File 'lib/cf/station.rb', line 368

def gold_standards=(gold_standard) # :nodoc:
  @gold_standards << gold_standard
end

#to_sObject

:nodoc:



321
322
323
324
325
326
327
# File 'lib/cf/station.rb', line 321

def to_s # :nodoc:
  if self.type == "TournamentStation"
    "{:type => #{self.type}, :index => #{self.index}, :line_title => #{self.line_title}, :station_input_formats => #{self.station_input_formats}, :jury_worker => #{self.jury_worker}, auto_judge => #{self.auto_judge}, :errors => #{self.errors}}"
  else
    "{:type => #{self.type}, :index => #{self.index}, :line_title => #{self.line_title}, :station_input_formats => #{self.station_input_formats}, :errors => #{self.errors}}"
  end
end

#worker(worker_instance = nil) ⇒ Object

Creates new Worker for station object

Usage Example:

line = CF::Line.new("line name", "Survey")
station = CF::Station.new({:type => "work"})
line.stations station
human_worker = CF::HumanWorker.new({:number => 1, :reward => 20})
line.stations.first.worker human_worker


165
166
167
168
169
170
171
# File 'lib/cf/station.rb', line 165

def worker worker_instance = nil
  if worker_instance
    @worker_instance = worker_instance
  else
    @worker_instance
  end
end

#worker=(worker_instance) ⇒ Object

:nodoc:



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/cf/station.rb', line 173

def worker=(worker_instance) # :nodoc:
  worker_type = worker_instance.class.to_s.split("::").last
  if worker_type == "HumanWorker"
    if worker_instance.station
      @worker_instance = worker_instance
    else
      number = worker_instance.number
      reward = worker_instance.reward
      stat_badge = worker_instance.stat_badge
      badges = worker_instance.badges

      request =
      {
        :body =>
        {
          :api_key => CF.api_key,
          :worker => {:number => number, :reward => reward, :type => "HumanWorker"},
          :stat_badge => stat_badge,
          :badge => badges
        }
      }

      resp = HTTParty.post("#{CF.api_url}#{CF.api_version}/lines/#{CF.}/#{self.line_title.downcase}/stations/#{self.index}/workers.json",request)
      worker = CF::HumanWorker.new({})
      worker.id =  resp.parsed_response['id']
      worker.number =  resp.parsed_response['number']
      worker.reward =  resp.parsed_response['reward']
      worker.stat_badge =  resp.parsed_response['stat_badge']
      worker.badges =  resp.parsed_response['badges']
      if resp.code != 200
        worker.errors = resp.parsed_response['error']['message']
      end
      @worker_instance = worker
    end

  elsif worker_type == "RobotWorker"
    if worker_instance.station
      @worker_instance = worker_instance
    else
      @settings = worker_instance.settings
      @type = worker_instance.type
      request = @settings.merge(:type => @type)
      resp = CF::RobotWorker.post("/lines/#{CF.}/#{self.line_title.downcase}/stations/#{self.index}/workers.json", :worker => request)
      worker = CF::RobotWorker.new({})
      worker.settings = @settings
      worker.type = @type
      worker.number = resp['number']
      worker.reward = resp['reward']
      worker.errors = resp['error']['message'] if resp['code'] != 200
      @worker_instance = worker
    end
  end
end