Class: Push0r::Queue

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

Overview

A Queue is used to register services to be used to transmit PushMessages. Single PushMessages are then put into the queue and a call to the #flush method transmits all enqueued messages using the registered services. In a sense, Queue is the class that ties all the other Push0r components together.

Examples:

queue = Push0r::Queue.new

gcm_service = Push0r::GcmService.new("__gcm_api_token__")
queue.register_service(gcm_service)

apns_service = Push0r::ApnsService.new(File.read("aps.pem"), true)
queue.register_service(apns_service)

gcm_message = Push0r::GcmPushMessage.new("__registration_id__")
gcm_message.attach({"data" => {"d" => "1"}})

apns_message = Push0r::ApnsPushMessage.new("__device_token__")
apns_message.attach({"data" => {"v" => "1"}}

queue.add(gcm_message)
queue.add(apns_message)

queue.flush

Instance Method Summary collapse

Constructor Details

#initializeQueue

Returns a new instance of Queue.



24
25
26
27
# File 'lib/push0r/Queue.rb', line 24

def initialize
  @services = []
  @queued_messages = {}
end

Instance Method Details

#add(message) ⇒ Boolean

Adds a PushMessage to the queue

Parameters:

  • message (PushMessage)

    the message to be added to the queue

Returns:

  • (Boolean)

    true if message was added to the queue (that is: if any of the registered services can handle the message), otherwise false

See Also:



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/push0r/Queue.rb', line 44

def add(message)
  @services.each do |service|
    if service.can_send?(message)
      if @queued_messages[service].nil?
        @queued_messages[service] = []
      end
      @queued_messages[service] << message
      return true
    end
  end
  return false
end

#flushFlushResult

Flushes the queue by transmitting the enqueued messages using the registered services

Returns:



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/push0r/Queue.rb', line 59

def flush
  failed_messages = []
  new_token_messages = []

  @queued_messages.each do |service, messages|
    service.init_push
    messages.each do |message|
      service.send(message)
    end
    (failed, new_token) = service.end_push
    failed_messages += failed
    new_token_messages += new_token
  end
  @queued_messages = {}
  return FlushResult.new(failed_messages, new_token_messages)
end

#register_service(service) ⇒ void

Note:

Every service can only be registered once with the same queue

This method returns an undefined value.

Registers a Service with the Queue

Parameters:

  • service (Service)

    the service to be registered with the queue

See Also:



34
35
36
37
38
# File 'lib/push0r/Queue.rb', line 34

def register_service(service)
  unless @services.include?(service)
    @services << service
  end
end