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


35
36
37
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
# File 'lib/cf/station.rb', line 35

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]
  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}
    }
  }
  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

#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



262
263
264
# File 'lib/cf/station.rb', line 262

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


96
97
98
99
100
101
102
103
104
# File 'lib/cf/station.rb', line 96

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


274
275
276
# File 'lib/cf/station.rb', line 274

def delete
  self.class.delete("/lines/#{CF.}/#{self.line_title.downcase}/stations/#{self.index}.json")
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


193
194
195
196
197
198
199
# File 'lib/cf/station.rb', line 193

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

#form=(form_instance) ⇒ Object

:nodoc:



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
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/cf/station.rb', line 201

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



246
247
248
249
# File 'lib/cf/station.rb', line 246

def get
  resp = self.class.get("/lines/#{CF.}/#{self.line_title.downcase}/stations/#{self.index}.json")
  return resp
end

#get_formObject

Returns information of form

Usage example:

@got_form = line.stations[0].get_form


254
255
256
# File 'lib/cf/station.rb', line 254

def get_form
  self.class.get("/lines/#{CF.}/#{self.line_title.downcase}/stations/#{self.index}/form.json")
end

#to_sObject



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

def to_s
  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


113
114
115
116
117
118
119
# File 'lib/cf/station.rb', line 113

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

#worker=(worker_instance) ⇒ Object

:nodoc:



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
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
# File 'lib/cf/station.rb', line 121

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