Class: CF::Line

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title, department_name, options = {}) ⇒ Line

Initializes a new line

Usage of line.new(“line_name”)

line = Line.new("line_name", "Survey")


38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/cf/line.rb', line 38

def initialize(title, department_name, options={})
  @input_formats =[]
  @stations =[]
  @title = title
  @department_name = department_name
  @public = options[:public].present? ? options[:public] : true
  @description = options[:description]
  resp = self.class.post("/lines/#{CF.}.json", {:line => {:title => title, :department_name => department_name, :public => @public, :description => @description}})
  if resp.code != 200
    self.errors = resp.error.message
  end
end

Instance Attribute Details

#department_nameObject

Department Name for Line



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

def department_name
  @department_name
end

#descriptionObject

Description attribute describes about the line

Description attribute is optional



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

def description
  @description
end

#errorsObject

Contains Error Messages



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

def errors
  @errors
end

#input_formats(input_formats_value = nil) ⇒ Object

Adds input format in a line

Usage Example:

line = Line.new("line name", "Survey")

input_format = CF::InputFormat.new({:label => "image_url", :required => true, :valid_type => "url"})
line.input_formats input_format
  • returns

line.input_formats as an array of input_formats



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

def input_formats
  @input_formats
end

#output_formats(output_format = nil) ⇒ Object

Specifies output format for a line

Usage Example:

output_format = CF::OutputFormat.new({:station_1 => [{:name => "First Name"}],:station_2 => [{:name => "Mobile", :except => true}]})
line.output_formats output_format


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

def output_formats
  @output_formats
end

#publicObject

Public is a boolean attribute which when set to true becomes public & vice-versa

Public attribute is optional, by default it’s true



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

def public
  @public
end

#stations(stations = nil) ⇒ Object

Adds station in a line

Usage Example:

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

line.stations as an array of stations



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

def stations
  @stations
end

#titleObject

Title of the Line



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

def title
  @title
end

Class Method Details

.all(options = {}) ⇒ Object

Returns all the lines of an account

Syntax for all method is

CF::Line.all

OR

CF:Line.all(:page => 1)


259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/cf/line.rb', line 259

def self.all(options={})
  page = options[:page].presence
  if page
    resp = get("/lines/#{CF.}.json", :page => page)
  else
    resp = get("/lines/#{CF.}.json")
  end
  @errors = resp.error.message if resp.code != 200
  new_resp = []
  if resp.lines
    if resp.lines.count > 0
      resp.lines.each do |l|
        new_resp << l.to_hash
      end
    end
  end
  send_resp = {"lines" => new_resp, "total_pages" => resp.total_pages, "total_lines" => resp.total_lines}
  return send_resp
end

.create(title, department_name, options = {}, &block) ⇒ Object

Initializes a new line

Usage Example:

creating Line within block using variable

Line.create("line_name", "Department_name") do |line|
  CF::InputFormat.new({:line => line, :label => "image_url", :required => true, :valid_type => "url"})
  CF::Station.new({:line => line, :type => "Work"})
end

OR

creating without variable

CF::Line.create("line_name", "Department_name") do
  CF::InputFormat.new({:line => self, :label => "image_url", :required => true, :valid_type => "url"})
  CF::Station.new({:line => self, :type => "Work"})
end


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

def self.create(title, department_name, options={}, &block)
  line = Line.new(title,department_name,options={})
  @public = options[:public]
  @description = options[:description]
  if block.arity >= 1
    block.call(line)
  else
    line.instance_eval &block
  end
  line
end

.destroy(title, options = {}) ⇒ Object

Deletes a line by passing it’s title

Usage Example:

line = CF::Line.new("line_title", "Survey")
CF::Line.destroy("line_title")


327
328
329
330
331
332
333
334
335
336
# File 'lib/cf/line.rb', line 327

def self.destroy(title, options={})
  forced = options[:forced]
  if forced
    resp = delete("/lines/#{CF.}/#{title.downcase}.json", {:forced => forced})
  else
    resp = delete("/lines/#{CF.}/#{title.downcase}.json")
  end
  @errors = resp.error.message if resp.code != 200
  return resp
end

.find(line) ⇒ Object

Finds a line

Usage Example:

CF::Line.find(line)

OR

CF::Line.find("line_title")


238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/cf/line.rb', line 238

def self.find(line)
  if line.class == CF::Line
    resp = get("/lines/#{CF.}/#{line.title.downcase}.json")
  elsif line.class == String
    if line.split("/").count == 2
       = line.split("/").first
      title = line.split("/").last
      resp = get("/lines/#{}/#{title.downcase}.json")
    elsif line.split("/").count == 1
      resp = get("/lines/#{CF.}/#{line.downcase}.json")
    end
  end
  @errors = resp.error.message if resp.code != 200
  return resp.to_hash
end

.info(line) ⇒ Object

Returns the content of a line by making an Api call

Usage Example:

CF::Line.info(line)

OR

CF::Line.info("line_title")


223
224
225
226
227
228
229
230
231
# File 'lib/cf/line.rb', line 223

def self.info(line)
  if line.class == CF::Line
    resp = get("/lines/#{CF.}/#{line.title.downcase}.json")
  else
    resp = get("/lines/#{CF.}/#{line.downcase}.json")
  end
  @errors = resp.error.message if resp.code != 200
  return resp
end

.inspect(line_title) ⇒ Object

Return all the associated elements of a line

Usage Example:

line = CF::Line.inspect("line_title")


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
367
368
369
370
371
372
373
374
375
376
377
378
379
# File 'lib/cf/line.rb', line 341

def self.inspect(line_title)
  resp = get("/lines/#{CF.}/#{line_title.downcase}/inspect.json")
  @errors = resp.error.message if resp.code != 200
  if resp.code == 200
    send_resp = resp.to_hash
    @line_input_formats = []
    resp.input_formats.each do |l_i|
      @line_input_formats << l_i.to_hash
    end
    send_resp.delete("input_formats")
    send_resp.merge!("input_formats" => @line_input_formats)
    @stations = []
  
    resp.stations.each do |s|
      @station_input_formats = []
      s.input_formats.each do |i|
        @station_input_formats << i.to_hash
      end
      @station_form_fields = []
      @temp_station = s.to_hash
      if !s.form_fields.nil?
        s.form_fields.each do |f|
          @station_form_fields << f.to_hash
        end
        @temp_station.delete("form_fields")
        @temp_station.merge!("form_fields" => @station_form_fields)
      end
      @temp_station.delete("input_formats")
      @temp_station.merge!("input_formats" => @station_input_formats)
      @stations << @temp_station
    end
  
    send_resp.delete("stations")
    send_resp.merge!("stations" => @stations)
    send_resp
  else
    resp
  end
end

.public_linesObject

Return all the public lines

Usage Example:

CF::Line.public_lines


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

def self.public_lines
  get("/public_lines.json")
end

Instance Method Details

#destroy(options = {}) ⇒ Object

Deletes a line

Usage Example:

line = CF::Line.new("Digitize Card", "Survey")
line.destroy


310
311
312
313
314
315
316
317
318
319
320
321
# File 'lib/cf/line.rb', line 310

def destroy(options={})
  force = options[:force]
  if !force.nil?
    resp = self.class.delete("/lines/#{CF.}/#{self.title.downcase}.json", :forced => force)
  else
    resp = self.class.delete("/lines/#{CF.}/#{self.title.downcase}.json")
  end
  if resp.code != 200
    self.errors = resp.errors.message
  end
  return resp
end

#get_stationsObject

Returns all the stations of a line

Usage Example:

CF::Line.get_stations


282
283
284
# File 'lib/cf/line.rb', line 282

def get_stations
  CF::Station.get("/lines/#{ACCOUNT_NAME}/#{self.title.downcase}/stations.json")
end

#update(options = {}) ⇒ Object

Updates a line

Syntax for update method is

line = CF::Line.new("Digitize Card", "Survey")
line.update({:title => "New Title"})
  • This changes the title of the “line” object from “Digitize Card” to “New Title”



297
298
299
300
301
302
303
304
# File 'lib/cf/line.rb', line 297

def update(options={}) # :nodoc:
  old_title = self.title
  @title = options[:title]
  @department_name = options[:department_name]
  @public = options[:public]
  @description = options[:description]
  self.class.put("/lines/#{CF.}/#{old_title.downcase}.json", :line => {:title => @title, :department_name => @department_name, :public => @public, :description => @description})
end