Class: DataFile

Inherits:
Object
  • Object
show all
Defined in:
lib/sqs_cli/data_file.rb

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ DataFile

Returns a new instance of DataFile.



5
6
7
# File 'lib/sqs_cli/data_file.rb', line 5

def initialize(filename)
  @filename = filename
end

Instance Method Details

#read_batchesObject



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/sqs_cli/data_file.rb', line 9

def read_batches
  count = 0
  File.readlines(@filename).each_slice(10).each do |batch|
    yield (batch.map { |message|
      count += 1
      OpenStruct.new(
        message_id: count.to_s,
        body: Base64.strict_decode64(message.strip)
      )
    })
  end
end

#write_batch(batch) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/sqs_cli/data_file.rb', line 22

def write_batch(batch)
  File.open(@filename, "a") do |f|
    batch.each do |message|
      f.puts Base64.strict_encode64(message.body.strip)
    end
  end
end