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_claim.rb,
lib/fog/rackspace/requests/queues/get_queue.rb,
lib/fog/rackspace/requests/queues/get_message.rb,
lib/fog/rackspace/requests/queues/list_queues.rb,
lib/fog/rackspace/requests/queues/create_claim.rb,
lib/fog/rackspace/requests/queues/create_queue.rb,
lib/fog/rackspace/requests/queues/delete_claim.rb,
lib/fog/rackspace/requests/queues/delete_queue.rb,
lib/fog/rackspace/requests/queues/update_claim.rb,
lib/fog/rackspace/requests/queues/list_messages.rb,
lib/fog/rackspace/requests/queues/create_message.rb,
lib/fog/rackspace/requests/queues/delete_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.



315
316
317
318
319
# File 'lib/fog/rackspace/queues.rb', line 315

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

Class Method Details

.dataObject



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

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.



336
337
338
339
340
# File 'lib/fog/rackspace/queues.rb', line 336

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.



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

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

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



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
69
70
71
# File 'lib/fog/rackspace/requests/queues/create_claim.rb', line 41

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:



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

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:



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

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



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

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

#delete_claim(queue_name, claim_id) ⇒ Object



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

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



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
59
60
61
# File 'lib/fog/rackspace/requests/queues/delete_message.rb', line 34

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

  claim_id = options[:claim_id]

  message = queue.messages.detect { |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



27
28
29
30
31
32
# File 'lib/fog/rackspace/requests/queues/delete_queue.rb', line 27

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



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

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:



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/fog/rackspace/requests/queues/get_message.rb', line 30

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



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

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



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

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



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
70
71
# File 'lib/fog/rackspace/requests/queues/list_messages.rb', line 44

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



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
59
# File 'lib/fog/rackspace/requests/queues/list_queues.rb', line 30

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.



347
348
349
# File 'lib/fog/rackspace/queues.rb', line 347

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.



356
357
358
# File 'lib/fog/rackspace/queues.rb', line 356

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

#request(params) ⇒ Object



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

def request(params)
  ageoff
  super
end

#update_claim(queue_name, claim_id, ttl) ⇒ Object



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

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