Class: ApnsKit::Request

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

Instance Method Summary collapse

Constructor Details

#initialize(notifications) ⇒ Request

Returns a new instance of Request.



7
8
9
# File 'lib/apns_kit/request.rb', line 7

def initialize(notifications)
    @notifications = notifications
end

Instance Method Details

#perform_blocking_send(connection) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/apns_kit/request.rb', line 11

def perform_blocking_send(connection)
    connection.open

    responses = Concurrent::Array.new
    latch = Concurrent::CountDownLatch.new(@notifications.size)

    perform_nonblocking_send(connection) do |response|
        responses.push(response)
        latch.count_down
    end

    latch.wait
    return responses
end

#perform_nonblocking_send(connection) ⇒ Object



26
27
28
29
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
# File 'lib/apns_kit/request.rb', line 26

def perform_nonblocking_send(connection)
    connection.open

    ApnsKit.log_info("Sending #{@notifications.size} notifications")
    @notifications.each do |notification|
        stream = connection.http.new_stream

        response = ApnsKit::Response.new
        response.notification = notification

        stream.on(:headers) do |headers|
            headers = Hash[*headers.flatten]
            response.headers = headers
            ApnsKit.log_debug("Received headers #{headers}")
            if response.success?
                yield response if block_given?
            end
        end

        stream.on(:data) do |data|
            response.raw_body ||= ""
            response.raw_body << data
            ApnsKit.log_debug("Received data #{data}")
            yield response if block_given?
        end

        stream.headers(notification.header, end_stream: false)
        stream.data(notification.payload)
    end

end