Sha256: 4d373a41e0fa593027fdfae02c9f56cbae6bef6c78a16cc6d2cba49336e55265

Contents?: true

Size: 1.51 KB

Versions: 1

Compression:

Stored size: 1.51 KB

Contents

module NotifyUser
  class APNConnection

    def initialize
      connection
    end

    def connection
      @connection ||= setup_connection
    end

    def write(data)
      raise "Connection is closed" unless @connection.open?
      @connection.write(data)
    end

    def reset
      @connection.close if @connection
      @connection = nil
      connection
    end

    private

    def apn_environment
      return nil unless ENV['APN_ENVIRONMENT']

      ENV['APN_ENVIRONMENT'].downcase.to_sym
    end

    def setup_connection
      return if Rails.env.test?

      @uri, @certificate = if Rails.env.development? || apn_environment == :development
        Rails.logger.info "Using development gateway. Rails env: #{Rails.env}, APN_ENVIRONMENT: #{apn_environment}"
        [
          ::Houston::APPLE_DEVELOPMENT_GATEWAY_URI,
          File.read(development_certificate)
        ]
      else
        Rails.logger.info "Using production gateway. Rails env: #{Rails.env}, APN_ENVIRONMENT: #{apn_environment}"
        [
          ::Houston::APPLE_PRODUCTION_GATEWAY_URI,
          File.read(production_certificate)
        ]
      end

      @connection = ::Houston::Connection.new(@uri, @certificate, nil)
    end

    def development_certificate
      file_path = ENV['APN_DEVELOPMENT_PATH'] || 'config/keys/development_push.pem'
      "#{Rails.root}/#{file_path}"
    end

    def production_certificate
      file_path = ENV['APN_PRODUCTION_PATH'] || "config/keys/production_push.pem"
      "#{Rails.root}/#{file_path}"
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
notify_user-0.3.1 app/models/notify_user/apn_connection.rb