Class: ApnsKit::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/apns_kit/client.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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_poolObject (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_topicObject (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_sizeObject (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

#inspectObject



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

#shutdownObject



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_sObject



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