Sha256: 135250ad0f7ff2bc851bd8d82356e791b2502ff091c9017a6e6fd6a23a5a250a

Contents?: true

Size: 1.15 KB

Versions: 2

Compression:

Stored size: 1.15 KB

Contents

require 'openssl'
require 'socket'

module ApnsSimple
  class Client

    attr_reader :certificate, :passphrase, :host, :port

    def initialize(options)
      @certificate = options.fetch(:certificate)
      @passphrase = options[:passphrase] || ''
      gateway_uri = options[:gateway_uri] || 'apn://gateway.push.apple.com:2195'
      @host, @port = parse_gateway_uri(gateway_uri)
    end

    def push(notification)
      begin
        ctx = OpenSSL::SSL::SSLContext.new
        ctx.key = OpenSSL::PKey::RSA.new(certificate, passphrase)
        ctx.cert = OpenSSL::X509::Certificate.new(certificate)

        sock = TCPSocket.new(host, port)
        ssl = OpenSSL::SSL::SSLSocket.new(sock, ctx)
        ssl.connect
        ssl.write(notification.payload)
        ssl.flush

        if IO.select([ssl], nil, nil, 1) && error = ssl.read(6)
          _command, status, _index = error.unpack("ccN")
          notification.error = status
        end
      ensure
        ssl.close if ssl
        sock.close if sock
      end
    end

    private

    def parse_gateway_uri(uri)
      first, last = uri.sub('apn://', '').split(':')
      [first, last.to_i]
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
apns_simple-0.1.3 lib/apns_simple/client.rb
apns_simple-0.1.2 lib/apns_simple/client.rb