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


38
39
40
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
# File 'lib/cf/station.rb', line 38

def initialize(options={})
  @input_formats =[]
  @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]
  if @batch_size.nil?
    request_general = 
    {
      :body => 
      {
        :api_key => CF.api_key,
        :station => {:type => @type, :input_formats => @station_input_formats}
      }
    }
    request_tournament = 
    {
      :body => 
      {
        :api_key => CF.api_key,
        :station => {:type => @type, :jury_worker => @jury_worker, :auto_judge => @auto_judge, :input_formats => @station_input_formats}
      }
    }
  else
    request_general = 
    {
      :body => 
      {
        :api_key => CF.api_key,
        :station => {:type => @type, :input_formats => @station_input_formats, :batch_size => @batch_size}
      }
    }
    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}
      }
    }
  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



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

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

#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



288
289
290
# File 'lib/cf/station.rb', line 288

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


119
120
121
122
123
124
125
126
127
# File 'lib/cf/station.rb', line 119

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


300
301
302
303
304
# File 'lib/cf/station.rb', line 300

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


216
217
218
219
220
221
222
# File 'lib/cf/station.rb', line 216

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

#form=(form_instance) ⇒ Object

:nodoc:



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/cf/station.rb', line 224

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
      @css = @form.raw_css
      @javascript = @form.raw_javascript
      @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, :raw_css => @css, :raw_javascript => @javascript})
    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
    if @resp.code != 200
      form.errors = @resp.error.message
    end
    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



269
270
271
272
273
# File 'lib/cf/station.rb', line 269

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


278
279
280
281
282
# File 'lib/cf/station.rb', line 278

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

#to_sObject

:nodoc:



306
307
308
309
310
311
312
# File 'lib/cf/station.rb', line 306

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


136
137
138
139
140
141
142
# File 'lib/cf/station.rb', line 136

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

#worker=(worker_instance) ⇒ Object

:nodoc:



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
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
# File 'lib/cf/station.rb', line 144

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
      badge = worker_instance.badge
      stat_badge = worker_instance.stat_badge
      if badge.nil? && stat_badge.nil?
        request = 
        {
          :body => 
          {
            :api_key => CF.api_key,
            :worker => {:number => number, :reward => reward, :type => "HumanWorker"}
          }
        }
      else
        request = 
        {
          :body => 
          {
            :api_key => CF.api_key,
            :worker => {:number => number, :reward => reward, :type => "HumanWorker"},
            :skill_badge => badge,
            :stat_badge => stat_badge
          }
        }
      end
      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.skill_badges << resp.parsed_response['skill_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
      if resp.code != 200
        worker.errors = resp.error.message
      end
      @worker_instance = worker
    end
  end
end