Class: ApnsKit::Client
- Inherits:
-
Object
- Object
- ApnsKit::Client
- Defined in:
- lib/apns_kit/client.rb
Instance Attribute Summary collapse
-
#connection_pool ⇒ Object
readonly
Returns the value of attribute connection_pool.
-
#default_topic ⇒ Object
readonly
Returns the value of attribute default_topic.
-
#pool_size ⇒ Object
readonly
Returns the value of attribute pool_size.
Class Method Summary collapse
- .development(certificate, pool_size: 1, heartbeat_interval: 60) ⇒ Object
- .production(certificate, pool_size: 1, heartbeat_interval: 60) ⇒ Object
Instance Method Summary collapse
-
#initialize(uri, certificate, pool_size: 1, heartbeat_interval: 60) ⇒ Client
constructor
A new instance of Client.
- #inspect ⇒ Object
- #send(*notifications) ⇒ Object
- #send_async(*notifications, &block) ⇒ Object
- #shutdown ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(uri, certificate, pool_size: 1, heartbeat_interval: 60) ⇒ Client
Returns a new instance of Client.
30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/apns_kit/client.rb', line 30 def initialize(uri, certificate, pool_size: 1, heartbeat_interval: 60) @pool_size = pool_size @connection_pool = @pool_size.times.map { ApnsKit::Connection.new(uri, certificate) }.freeze @default_topic = certificate.app_bundle_id if heartbeat_interval > 0 ApnsKit.log_info("Setting up heartbeat checker") @heartbeat_checker = Concurrent::TimerTask.new { @connection_pool.each(&:ping) } @heartbeat_checker.execution_interval = heartbeat_interval @heartbeat_checker.execute end end |
Instance Attribute Details
#connection_pool ⇒ Object (readonly)
Returns the value of attribute connection_pool.
13 14 15 |
# File 'lib/apns_kit/client.rb', line 13 def connection_pool @connection_pool end |
#default_topic ⇒ Object (readonly)
Returns the value of attribute default_topic.
15 16 17 |
# File 'lib/apns_kit/client.rb', line 15 def default_topic @default_topic end |
#pool_size ⇒ Object (readonly)
Returns the value of attribute pool_size.
11 12 13 |
# File 'lib/apns_kit/client.rb', line 11 def pool_size @pool_size end |
Class Method Details
.development(certificate, pool_size: 1, heartbeat_interval: 60) ⇒ Object
24 25 26 |
# File 'lib/apns_kit/client.rb', line 24 def development(certificate, pool_size: 1, heartbeat_interval: 60) client = self.new(APPLE_DEVELOPMENT_API_URI, certificate, pool_size: pool_size, heartbeat_interval: heartbeat_interval) end |
.production(certificate, pool_size: 1, heartbeat_interval: 60) ⇒ Object
19 20 21 22 |
# File 'lib/apns_kit/client.rb', line 19 def production(certificate, pool_size: 1, heartbeat_interval: 60) client = self.new(APPLE_PRODUCTION_API_URI, certificate, pool_size: pool_size, heartbeat_interval: heartbeat_interval) client end |
Instance Method Details
#inspect ⇒ Object
74 75 76 |
# File 'lib/apns_kit/client.rb', line 74 def inspect "#<ApnsKit::Client:#{"0x00%x" % (object_id << 1)} #{to_s}>" end |
#send(*notifications) ⇒ Object
62 63 64 65 66 67 68 |
# File 'lib/apns_kit/client.rb', line 62 def send(*notifications) return [] if notifications.empty? notifications.flatten! notifications.each { |notification| notification.topic = default_topic if notification.topic.nil? } request = ApnsKit::Request.new(notifications) return Concurrent::Future.execute{ request.perform_blocking_send(connection_pool.sample) }.value end |
#send_async(*notifications, &block) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/apns_kit/client.rb', line 48 def send_async(*notifications, &block) notifications.flatten! notifications.each { |notification| notification.topic = default_topic if notification.topic.nil? } request = ApnsKit::Request.new(notifications) if block Concurrent::Future.execute{ request.perform_nonblocking_send(connection_pool.sample, &block) } else Concurrent::Future.execute{ request.perform_nonblocking_send(connection_pool.sample) } end return true end |
#shutdown ⇒ Object
42 43 44 45 46 |
# File 'lib/apns_kit/client.rb', line 42 def shutdown @heartbeat_checker.shutdown if @heartbeat_checker @connection_pool.each(&:close) return true end |
#to_s ⇒ Object
70 71 72 |
# File 'lib/apns_kit/client.rb', line 70 def to_s "uri=#{connection_pool.first.uri} connected=#{connection_pool.map(&:connected)} pool_size=#{pool_size}" end |