Push0r 0.4.4 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,76 +1,76 @@
1
1
  module Push0r
2
-
3
- # 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.
4
- # @example
5
- # queue = Push0r::Queue.new
6
- #
7
- # gcm_service = Push0r::GcmService.new("__gcm_api_token__")
8
- # queue.register_service(gcm_service)
9
- #
10
- # apns_service = Push0r::ApnsService.new(File.read("aps.pem"), true)
11
- # queue.register_service(apns_service)
12
- #
13
- # gcm_message = Push0r::GcmPushMessage.new("__registration_id__")
14
- # gcm_message.attach({"data" => {"d" => "1"}})
15
- #
16
- # apns_message = Push0r::ApnsPushMessage.new("__device_token__")
17
- # apns_message.attach({"data" => {"v" => "1"}}
18
- #
19
- # queue.add(gcm_message)
20
- # queue.add(apns_message)
21
- #
22
- # queue.flush
23
- class Queue
24
- def initialize
25
- @services = []
26
- @queued_messages = {}
27
- end
28
-
29
- # Registers a Service with the Queue
30
- # @note Every service can only be registered once with the same queue
31
- # @param service [Service] the service to be registered with the queue
32
- # @return [void]
33
- # @see Service
34
- def register_service(service)
35
- unless @services.include?(service)
36
- @services << service
37
- end
38
- end
39
-
40
- # Adds a PushMessage to the queue
41
- # @param message [PushMessage] the message to be added to the queue
42
- # @return [Boolean] true if message was added to the queue (that is: if any of the registered services can handle the message), otherwise false
43
- # @see PushMessage
44
- def add(message)
45
- @services.each do |service|
46
- if service.can_send?(message)
47
- if @queued_messages[service].nil?
48
- @queued_messages[service] = []
49
- end
50
- @queued_messages[service] << message
51
- return true
52
- end
53
- end
54
- return false
55
- end
56
-
57
- # Flushes the queue by transmitting the enqueued messages using the registered services
58
- # @return [FlushResult] the result of the operation
59
- def flush
60
- failed_messages = []
61
- new_token_messages = []
62
-
63
- @queued_messages.each do |service, messages|
64
- service.init_push
65
- messages.each do |message|
66
- service.send(message)
67
- end
68
- (failed, new_token) = service.end_push
69
- failed_messages += failed
70
- new_token_messages += new_token
71
- end
72
- @queued_messages = {}
73
- return FlushResult.new(failed_messages, new_token_messages)
74
- end
75
- end
2
+
3
+ # 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.
4
+ # @example
5
+ # queue = Push0r::Queue.new
6
+ #
7
+ # gcm_service = Push0r::GcmService.new("__gcm_api_token__")
8
+ # queue.register_service(gcm_service)
9
+ #
10
+ # apns_service = Push0r::ApnsService.new(File.read("aps.pem"), true)
11
+ # queue.register_service(apns_service)
12
+ #
13
+ # gcm_message = Push0r::GcmPushMessage.new("__registration_id__")
14
+ # gcm_message.attach({"data" => {"d" => "1"}})
15
+ #
16
+ # apns_message = Push0r::ApnsPushMessage.new("__device_token__")
17
+ # apns_message.attach({"data" => {"v" => "1"}}
18
+ #
19
+ # queue.add(gcm_message)
20
+ # queue.add(apns_message)
21
+ #
22
+ # queue.flush
23
+ class Queue
24
+ def initialize
25
+ @services = []
26
+ @queued_messages = {}
27
+ end
28
+
29
+ # Registers a Service with the Queue
30
+ # @note Every service can only be registered once with the same queue
31
+ # @param service [Service] the service to be registered with the queue
32
+ # @return [void]
33
+ # @see Service
34
+ def register_service(service)
35
+ unless @services.include?(service)
36
+ @services << service
37
+ end
38
+ end
39
+
40
+ # Adds a PushMessage to the queue
41
+ # @param message [PushMessage] the message to be added to the queue
42
+ # @return [Boolean] true if message was added to the queue (that is: if any of the registered services can handle the message), otherwise false
43
+ # @see PushMessage
44
+ def add(message)
45
+ @services.each do |service|
46
+ if service.can_send?(message)
47
+ if @queued_messages[service].nil?
48
+ @queued_messages[service] = []
49
+ end
50
+ @queued_messages[service] << message
51
+ return true
52
+ end
53
+ end
54
+ return false
55
+ end
56
+
57
+ # Flushes the queue by transmitting the enqueued messages using the registered services
58
+ # @return [FlushResult] the result of the operation
59
+ def flush
60
+ failed_messages = []
61
+ new_token_messages = []
62
+
63
+ @queued_messages.each do |service, messages|
64
+ service.init_push
65
+ messages.each do |message|
66
+ service.send(message)
67
+ end
68
+ (failed, new_token) = service.end_push
69
+ failed_messages += failed
70
+ new_token_messages += new_token
71
+ end
72
+ @queued_messages = {}
73
+ return FlushResult.new(failed_messages, new_token_messages)
74
+ end
75
+ end
76
76
  end
@@ -1,51 +1,51 @@
1
1
  module Push0r
2
- module ErrorCodes
3
- NO_ERROR = -1
4
- end
2
+ module ErrorCodes
3
+ NO_ERROR = -1
4
+ end
5
5
 
6
- # Service is the base class for all implemented push services. A Service encapsulates everything that is necessary to take a batch of push notifications and transmit it to the receivers.
7
- # @abstract
8
- class Service
6
+ # Service is the base class for all implemented push services. A Service encapsulates everything that is necessary to take a batch of push notifications and transmit it to the receivers.
7
+ # @abstract
8
+ class Service
9
9
 
10
- # Called on the service every time a PushMessage is added to a Queue in order to determine whether it can send the given message.
11
- # @param message [PushMessage] the message
12
- # @return [Boolean] true if this service can send the given message, otherwise false
13
- # @abstract
14
- # @see PushMessage
15
- # @see Queue
16
- def can_send?(message)
17
- return false
18
- end
10
+ # Called on the service every time a PushMessage is added to a Queue in order to determine whether it can send the given message.
11
+ # @param message [PushMessage] the message
12
+ # @return [Boolean] true if this service can send the given message, otherwise false
13
+ # @abstract
14
+ # @see PushMessage
15
+ # @see Queue
16
+ def can_send?(message)
17
+ return false
18
+ end
19
19
 
20
- # Sends a single push message. This is called during the flushing of a Queue for every enqueued PushMessage. The service may create its own internal queue in order to efficiently batch the messages.
21
- # @param message [PushMessage] the message to be sent
22
- # @return [void]
23
- # @abstract
24
- # @see PushMessage
25
- # @see Queue
26
- def send(message)
27
- ## empty
28
- end
20
+ # Sends a single push message. This is called during the flushing of a Queue for every enqueued PushMessage. The service may create its own internal queue in order to efficiently batch the messages.
21
+ # @param message [PushMessage] the message to be sent
22
+ # @return [void]
23
+ # @abstract
24
+ # @see PushMessage
25
+ # @see Queue
26
+ def send(message)
27
+ ## empty
28
+ end
29
29
 
30
- # Called on the service during the flushing of a Queue before the first PushMessage is sent.
31
- # @return [void]
32
- # @abstract
33
- # @see PushMessage
34
- # @see Queue
35
- def init_push
36
- ## empty
37
- end
30
+ # Called on the service during the flushing of a Queue before the first PushMessage is sent.
31
+ # @return [void]
32
+ # @abstract
33
+ # @see PushMessage
34
+ # @see Queue
35
+ def init_push
36
+ ## empty
37
+ end
38
38
 
39
- # Called on the service during the flushing of a Queue after the last PushMessage has been sent. If the service manages its own internal queue, this is the place to actually transmit all messages.
40
- # @return [Array(Array<String>, Array<String>)] Failed new RegId Messages
41
- # @abstract
42
- # @see PushMessage
43
- # @see Queue
44
- def end_push
45
- ## empty
46
- return [[], []]
47
- end
48
- end
39
+ # Called on the service during the flushing of a Queue after the last PushMessage has been sent. If the service manages its own internal queue, this is the place to actually transmit all messages.
40
+ # @return [Array(Array<String>, Array<String>)] Failed new RegId Messages
41
+ # @abstract
42
+ # @see PushMessage
43
+ # @see Queue
44
+ def end_push
45
+ ## empty
46
+ return [[], []]
47
+ end
48
+ end
49
49
  end
50
50
 
51
51
  require_relative 'APNS/ApnsService'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: Push0r
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.4
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kai Straßmann
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-05-22 00:00:00.000000000 Z
11
+ date: 2015-06-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -64,3 +64,4 @@ signing_key:
64
64
  specification_version: 4
65
65
  summary: Push0r gem
66
66
  test_files: []
67
+ has_rdoc: