Class: SISFC::RequestGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/sisfc/generator.rb

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ RequestGenerator

Returns a new instance of RequestGenerator.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/sisfc/generator.rb', line 7

def initialize(opts={})
  if opts.has_key? :filename
    filename = opts[:filename]
    raise ArgumentError, "File #{filename} does not exist!" unless File.exists?(filename)
    @file = File.open(filename, mode='r')
  elsif opts.has_key? :command
    command = opts[:command]
    @file = IO.popen(command)
  else
    raise ArgumentError, "Need to provide either a filename or a command!"
  end

  # throw away the first line (containing the CSV headers)
  @file.gets

  # NOTE: so far we support only sequential integer rids
  @next_rid = 0

  setup_finalizer
end

Instance Method Details

#generateObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/sisfc/generator.rb', line 29

def generate
  # read next line from file
  line = @file.gets
  raise "End of input reached while reading request #{@next_rid}!" unless line

  # parse data
  tokens = line.split(",") # should be faster than CSV parsing
  generation_time  = tokens[0].to_f
  workflow_type_id = tokens[1].to_i
  customer_id      = tokens[2].to_i

  # increase @next_rid
  @next_rid += 1

  # return request
  {
    rid: @next_rid,
    generation_time: generation_time,
    workflow_type_id: workflow_type_id,
    customer_id: customer_id,
  }
end