class Fog::Rackspace::Queues::Mock::MockQueue

An in-memory Queue implementation.

Attributes

claims[RW]
messages[RW]
metadata[RW]
name[RW]

Public Class Methods

new(name) click to toggle source
# File lib/fog/rackspace/queues.rb, line 105
def initialize(name)
  @name, @metadata = name, {}
  @messages, @claims = [], {}
  @id_counter = Fog::Mock.random_hex(24).to_i(16)
end

Public Instance Methods

add_claim(ttl, grace) click to toggle source

Create a new MockClaim.

@param ttl [Integer] Time-to-live for the claim. @param grace [Integer] Grace period that's added to messages included in this claim.

# File lib/fog/rackspace/queues.rb, line 164
def add_claim(ttl, grace)
  claim = MockClaim.new(self, ttl, grace)
  claims[claim.id] = claim
  claim
end
add_message(client_id, data, ttl) click to toggle source

Append a new message to the queue.

@param client_id [String] UUID for the service object. @param data [Hash] Message payload. @param ttl [Integer] Number of seconds that the message should exist. @return [MockMessage] The message object that was created.

# File lib/fog/rackspace/queues.rb, line 152
def add_message(client_id, data, ttl)
  id = @id_counter.to_s(16)
  @id_counter += 1
  message = MockMessage.new(id, self, client_id, data, ttl)
  @messages << message
  message
end
ageoff() click to toggle source

Remove any messages or claims whose ttls have expired.

# File lib/fog/rackspace/queues.rb, line 180
def ageoff
  messages.reject! { |m| m.expired? }

  claims.keys.dup.each do |id|
    claim = claims[id]
    if claim.expired? || claim.messages.empty?
      claim.messages.each { |m| m.claim = nil }
      claims.delete(id)
    end
  end
end
claim!(claim_id) click to toggle source

Access an existing MockClaim by id.

@param claim_id [String] An ID of an existing claim. @raises [Fog::Rackspace::Queues::NotFound] If no MockClaim with this ID exists. @return [MockClaim] The requested MockClaim.

# File lib/fog/rackspace/queues.rb, line 175
def claim!(claim_id)
  claims[claim_id] or raise NotFound.new
end
claimed() click to toggle source

The number of messages currently held by a claim.

@return [Integer]

# File lib/fog/rackspace/queues.rb, line 121
def claimed
  @messages.count { |msg| msg.claimed? }
end
free() click to toggle source

The number of messages not held by any claim.

@return [Integer]

# File lib/fog/rackspace/queues.rb, line 128
def free
  @messages.count { |msg| ! msg.claimed? }
end
newest() click to toggle source

The most recently published message on this queue, or `nil`.

@return [MockMessage|UndefinedObject]

# File lib/fog/rackspace/queues.rb, line 142
def newest
  @messages.last
end
oldest() click to toggle source

The oldest published message on this queue, or `nil`.

@return [MockMessage|UndefinedObject]

# File lib/fog/rackspace/queues.rb, line 135
def oldest
  @messages.first
end
total() click to toggle source

The total number of messages currently on the queue.

@return [Integer]

# File lib/fog/rackspace/queues.rb, line 114
def total
  @messages.size
end