Sha256: 3b9a2428001d4b091b589bed2e5a436aebbce95defe23a0e72faded582084017

Contents?: true

Size: 1.66 KB

Versions: 1

Compression:

Stored size: 1.66 KB

Contents

require 'connection_pool'
require 'em-apn'

require 'apple_push/configuration'
require 'apple_push/server'

module ApplePush
  LIVE_URL    = 'gateway.push.apple.com'
  SANDBOX_URL = 'gateway.sandbox.push.apple.com'
  
  @@options = {}
  @@pools   = {}
  
  def self.configure(options={})
    config = ApplePush::Configuration.new(options)
    
    @@options.merge!(
      :host    => config.host    || '127.0.0.1',
      :port    => config.port    || '27000',
      :timeout => config.timeout || 5
    )
    
    if !config.sandbox? && !config.live?
      raise ArgumentError, "At least one environment should be defined."
    end
    
    self.setup_environment('sandbox', config.sandbox) if config.sandbox?
    self.setup_environment('live', config.live)       if config.live?
  end
  
  # Get current configuration options
  #
  def self.configuration
    @@options
  end
  
  # Get a connection from pool
  #
  def self.pool(env='sandbox')
    @@pools[env]
  end
  
  # Returns true if pools is configured
  #
  def self.pool?(env)
    @@pools.key?(env)
  end
  
  private
  
  # Configure an environment
  #
  def self.setup_environment(env, config)
    raise ArgumentError, "Path to certificate file required." if !config.cert?      
    raise ArgumentError, "Path to key file required." if !config.cert_key?
  
    opts = {
      :size => (Integer(config.pool || 1) rescue 1),
      :timeout => configuration[:timeout]
    }

    @@pools[env] = ConnectionPool.new(opts) do
      EM::APN::Client.connect(
        :gateway => env == 'live' ? LIVE_URL : SANDBOX_URL,
        :cert    => File.expand_path(config.cert),
        :key     => File.expand_path(config.cert_key)
      )
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
apple_push-0.1.2 lib/apple_push.rb