Class: FileQueue

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

Overview

FileQueue - simple queue to bind to a topic and pop messages

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFileQueue

Returns a new instance of FileQueue.



9
10
11
# File 'lib/file_queue.rb', line 9

def initialize
  @queue_name = SecureRandom.hex(20).to_s
end

Instance Attribute Details

#queue_nameObject (readonly)

Returns the value of attribute queue_name.



8
9
10
# File 'lib/file_queue.rb', line 8

def queue_name
  @queue_name
end

Instance Method Details

#bind(file_topic) ⇒ Object



13
14
15
16
# File 'lib/file_queue.rb', line 13

def bind(file_topic)
  @queue_location = file_topic.topic_location + '/' + @queue_name
  FileUtils.mkdir_p @queue_location
end

#latest_message_fileObject



25
26
27
28
29
30
# File 'lib/file_queue.rb', line 25

def latest_message_file
  files_sorted_by_time = Dir[@queue_location + '/*'].sort_by do |file|
    File.mtime(file)
  end
  files_sorted_by_time[0]
end

#popObject



18
19
20
21
22
23
# File 'lib/file_queue.rb', line 18

def pop
  latest_message = latest_message_file
  message_content = File.read(latest_message).chomp
  File.delete(latest_message)
  message_content
end