Sha256: f5685ea98739b8471e24cbe2c744a4fb13cb91d9bcc4a2b2af4c44e573edf601

Contents?: true

Size: 1.34 KB

Versions: 1

Compression:

Stored size: 1.34 KB

Contents

module LeadZeppelin
  module APNS
    class Application
      CONNECTION_POOL_SIZE = 5
      CONNECTION_POOL_TIMEOUT = 5

      def initialize(name, opts={})
        @name = name
        @opts = opts

        @ssl_context = OpenSSL::SSL::SSLContext.new

        if opts[:p12]
          pem = OpenSSL::PKCS12.new opts[:p12], opts[:p12_pass]
          @ssl_context.cert = pem.certificate
          @ssl_context.key  = pem.key
        elsif opts[:pem]
          @ssl_context.cert = OpenSSL::X509::Certificate.new opts[:pem]
          @ssl_context.key  = OpenSSL::PKey::RSA.new opts[:pem], opts[:pem_pass]
        else
          raise ArgumentError, 'opts[:p12] or opts[:pem] required'
        end
      end

      def connect
        cp_args = {size:    (@opts[:connection_pool_size] || CONNECTION_POOL_SIZE),
                   timeout: (@opts[:connection_pool_timeout] || CONNECTION_POOL_TIMEOUT)}

        @gateway_connection_pool = ConnectionPool.new(cp_args) do
          Gateway.new @ssl_context, (@opts[:gateway_opts] || {}).merge(error_block: @opts[:error_block])
        end
      end

      def message(device_id, message, opts={})
        connect if @gateway_connection_pool.nil?

        @gateway_connection_pool.with_connection do |gateway|
          gateway.write Notification.new(device_id, message, opts)
        end

        true
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
lead_zeppelin-0.1.1 lib/lead_zeppelin/apns/application.rb