Class: Fog::Rackspace::Queues::Mock

Inherits:
Service
  • Object
show all
Includes:
Common
Defined in:
lib/fog/rackspace/queues.rb,
lib/fog/rackspace/requests/queues/get_queue.rb,
lib/fog/rackspace/requests/queues/get_claim.rb,
lib/fog/rackspace/requests/queues/get_message.rb,
lib/fog/rackspace/requests/queues/list_queues.rb,
lib/fog/rackspace/requests/queues/update_claim.rb,
lib/fog/rackspace/requests/queues/delete_claim.rb,
lib/fog/rackspace/requests/queues/delete_queue.rb,
lib/fog/rackspace/requests/queues/create_queue.rb,
lib/fog/rackspace/requests/queues/create_claim.rb,
lib/fog/rackspace/requests/queues/list_messages.rb,
lib/fog/rackspace/requests/queues/delete_message.rb,
lib/fog/rackspace/requests/queues/create_message.rb,
lib/fog/rackspace/requests/queues/get_queue_stats.rb

Defined Under Namespace

Classes: MockClaim, MockMessage, MockQueue

Constant Summary collapse

PATH_BASE =
"/v1/queues"

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Common

#apply_options, #authenticate, #client_id, #client_id=, #endpoint_uri, #region, #service_name

Methods inherited from Service

#authenticate, #endpoint_uri, #region, #request_without_retry, #service_name, #service_net?

Constructor Details

#initialize(options = {}) ⇒ Mock

Returns a new instance of Mock



321
322
323
324
325
# File 'lib/fog/rackspace/queues.rb', line 321

def initialize(options = {})
  apply_options(options)
  authenticate
  endpoint_uri
end

Class Method Details

.dataObject



327
328
329
330
331
# File 'lib/fog/rackspace/queues.rb', line 327

def self.data
  @data ||= Hash.new do |hash, key|
    hash[key] = {}
  end
end

Instance Method Details

#add_queue(queue_name) ⇒ MockQueue

Create and remember a MockQueue with a given name. An existing MockQueue with the same name will be overridden without warning.

Parameters:

  • Valid (String)

    queue name.

Returns:

  • (MockQueue)

    The MockQueue that was created.



342
343
344
345
346
# File 'lib/fog/rackspace/queues.rb', line 342

def add_queue(queue_name)
  queue = MockQueue.new(queue_name)
  data[queue_name] = queue
  queue
end

#ageoffObject

Remove any messages or expire any claims that have exceeded their ttl values. Invoked before every request.



368
369
370
# File 'lib/fog/rackspace/queues.rb', line 368

def ageoff
  data.values.each { |q| q.ageoff }
end

#create_claim(queue_name, ttl, grace, options = {}) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/fog/rackspace/requests/queues/create_claim.rb', line 38

def create_claim(queue_name, ttl, grace, options = {})
  queue = mock_queue!(queue_name)

  limit = options[:limit] || 10

  claim = queue.add_claim(ttl, grace)

  claimed = queue.messages.select do |message|
    ! message.claimed?
  end.first(limit)

  if claimed.empty?
    response = Excon::Response.new
    response.status = 204
    return response
  end

  claimed.each do |message|
    message.claim = claim

    # Extend the message's lifetime to include the lifetime of the claim, plus the claim's
    # grace period.
    message.extend_life
  end

  response = Excon::Response.new
  response.status = 201
  response.body = claimed.map { |msg| msg.to_h }
  response.headers['Location'] = "#{PATH_BASE}/#{queue_name}/claims/#{claim.id}"
  response
end

#create_message(client_id, queue_name, body, ttl) ⇒ Object

Raises:



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/fog/rackspace/requests/queues/create_message.rb', line 35

def create_message(client_id, queue_name, body, ttl)
  queue = mock_queue!(queue_name)

  raise BadRequest.new if body.nil? || body.empty?

  # Ensure that any Symbol keys within +body+ are converted to Strings, just as being
  # round-tripped through the API will.
  converted = MockData.stringify(body)
  message = queue.add_message(client_id, converted, ttl)

  response = Excon::Response.new
  response.status = 201
  response.body = {
    "partial" => false,
    "resources" => ["#{PATH_BASE}/#{queue_name}/messages/#{message.id}"]
  }
  response
end

#create_queue(queue_name) ⇒ Object

Raises:



26
27
28
29
30
31
32
33
34
35
# File 'lib/fog/rackspace/requests/queues/create_queue.rb', line 26

def create_queue(queue_name)
  raise MethodNotAllowed.new if queue_name.nil? || queue_name.empty?

  existed = ! mock_queue(queue_name).nil?
  add_queue(queue_name) unless existed

  response = Excon::Response.new
  response.status = existed ? 201 : 204
  response
end

#dataObject



333
334
335
# File 'lib/fog/rackspace/queues.rb', line 333

def data
  self.class.data[@rackspace_username]
end

#delete_claim(queue_name, claim_id) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/fog/rackspace/requests/queues/delete_claim.rb', line 26

def delete_claim(queue_name, claim_id)
  queue = mock_queue!(queue_name)
  claim = queue.claim!(claim_id)

  claim.messages.each do |message|
    message.claim = nil
  end
  queue.claims.delete(claim_id)

  response = Excon::Response.new
  response.status = 204
  response
end

#delete_message(queue_name, message_id, options = {}) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/fog/rackspace/requests/queues/delete_message.rb', line 31

def delete_message(queue_name, message_id, options = {})
  queue = mock_queue!(queue_name)

  claim_id = options[:claim_id]

  message = queue.messages.find { |m| m.id == message_id }

  perform_delete = true
  if message && message.claimed?
    unless message.claim.id == claim_id
      # Currently returns a 204 without deleting!
      perform_delete = false
    end
  else
    if claim_id
      # Currently returns a 204 without deleting!
      perform_delete = false
    end
  end

  if perform_delete
    queue.messages.reject! { |m| m.id == message_id }
  end

  response = Excon::Response.new
  response.status = 204
  response
end

#delete_queue(queue_name) ⇒ Object



24
25
26
27
28
29
# File 'lib/fog/rackspace/requests/queues/delete_queue.rb', line 24

def delete_queue(queue_name)
  data.delete(queue_name)
  response = Excon::Response.new
  response.status = 204
  response
end

#get_claim(queue_name, claim_id) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/fog/rackspace/requests/queues/get_claim.rb', line 25

def get_claim(queue_name, claim_id)
  queue = mock_queue!(queue_name)
  claim = queue.claim!(claim_id)

  response = Excon::Response.new
  response.status = 200
  response.body = claim.to_h
  response
end

#get_message(client_id, queue_name, message_id) ⇒ Object

Raises:

  • (NotFound)


27
28
29
30
31
32
33
34
35
36
37
# File 'lib/fog/rackspace/requests/queues/get_message.rb', line 27

def get_message(client_id, queue_name, message_id)
  queue = mock_queue!(queue_name)

  message = queue.messages.find { |msg| msg.id == message_id }
  raise NotFound.new unless message

  response = Excon::Response.new
  response.status = 200
  response.body = message.to_h
  response
end

#get_queue(queue_name) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/fog/rackspace/requests/queues/get_queue.rb', line 24

def get_queue(queue_name)
  if mock_queue(queue_name).nil?
    raise NotFound.new
  else
    response = Excon::Response.new
    response.status = 204
    response
  end
end

#get_queue_stats(queue_name) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/fog/rackspace/requests/queues/get_queue_stats.rb', line 24

def get_queue_stats(queue_name)
  queue = mock_queue!(queue_name)

  payload = {
    "claimed" => queue.claimed,
    "total" => queue.total,
    "free" => queue.free
  }

  report_message(payload, "oldest", queue.oldest)
  report_message(payload, "newest", queue.newest)

  response = Excon::Response.new
  response.status = 200
  response.body = { "messages" => payload }
  response
end

#list_messages(client_id, queue_name, options = {}) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/fog/rackspace/requests/queues/list_messages.rb', line 42

def list_messages(client_id, queue_name, options = {})
  queue = mock_queue!(queue_name)

  marker = (options[:marker] || "0").to_i
  limit = options[:limit] || 10
  echo = options[:echo] || false
  include_claimed = options[:include_claimed] || false

  next_marker = marker + limit + 1
  messages = queue.messages[marker...next_marker]
  messages.reject! { |m| m.producer_id == client_id } unless echo
  messages.reject! { |m| m.claimed? } unless include_claimed

  response = Excon::Response.new
  if queue.messages.empty?
    response.status = 204
  else
    response.status = 200
    response.body = {
      "messages" => messages.map { |m| m.to_h },
      "links" => [{
        "href" => "#{PATH_BASE}/#{queue_name}/messages?marker=#{next_marker}",
        "rel" => "next"
      }]
    }
  end
  response
end

#list_queues(options = {}) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/fog/rackspace/requests/queues/list_queues.rb', line 29

def list_queues(options={})
  limit = options[:limit] || 10
  marker = options[:marker]
  detailed = options[:detailed] || false

  queue_names = data.keys.sort
  start_index = marker.nil? ? 0 : queue_names.count { |name| name <= marker }
  stop_index = start_index + limit

  queue_names = queue_names[start_index..stop_index]
  queue_data = queue_names.map do |qname|
    { "href" => "#{PATH_BASE}/#{qname}", "name" => qname }
  end

  if detailed
    queue_data.each { |d| d["metadata"] = data[d["name"]]. }
  end

  response = Excon::Response.new
  if data.empty?
    response.status = 204
  else
    response.status = 200
    response.body = {
      "queues" => queue_data,
      "links" => [{ "href" => "#{PATH_BASE}?marker=#{queue_names.last}", "rel" => "next" }]
    }
  end
  response
end

#mock_queue(queue_name) ⇒ MockQueue|UndefinedObject

Access a MockQueue with the specified name, or return `nil`.

Parameters:

  • queue_name (String)

    Valid queue name.

Returns:

  • (MockQueue|UndefinedObject)

    The queue with the specified name, or `nil` if it doesn't exist.



353
354
355
# File 'lib/fog/rackspace/queues.rb', line 353

def mock_queue(queue_name)
  data[queue_name]
end

#mock_queue!(queue_name) ⇒ MockQueue

Access a MockQueue with the specified name, raising an exception if it doesn't exist.

Parameters:

  • queue_name (String)

    Valid queue name.

Returns:

  • (MockQueue)

    The queue with the specified name.



362
363
364
# File 'lib/fog/rackspace/queues.rb', line 362

def mock_queue!(queue_name)
  mock_queue(queue_name) or raise NotFound.new
end

#request(params) ⇒ Object



372
373
374
375
# File 'lib/fog/rackspace/queues.rb', line 372

def request(params)
  ageoff
  super
end

#update_claim(queue_name, claim_id, ttl) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/fog/rackspace/requests/queues/update_claim.rb', line 25

def update_claim(queue_name, claim_id, ttl)
  queue = mock_queue!(queue_name)
  claim = queue.claim!(claim_id)

  claim.touch!
  claim.ttl = ttl

  response = Excon::Response.new
  response.status = 204
  response
end