Sha256: 37963e41f38363da9dbfcbff785b3ada4b8bb8043e4f5a52e135a3ae7da922c4

Contents?: true

Size: 1.19 KB

Versions: 7

Compression:

Stored size: 1.19 KB

Contents

require "em-http"

module Suj
  module Pusher
    class GCMConnection
      include Suj::Pusher::Logger

      GATEWAY = "https://android.googleapis.com/gcm/send"

      def initialize(pool, key, options = {})
        @pool = pool
        @options = options
        @key = key
        @headers =  {
          'Content-Type' => 'application/json',
          'Authorization' => "key=#{options[:api_key]}"
        }
      end

      def deliver(msg)

        return if msg[:gcm_ids].empty?

        body = MultiJson.dump({
          registration_ids: msg[:gcm_ids],
          time_to_live: msg[:time_to_live] || 0,
          data: msg[:data] || {}
        })

        http = EventMachine::HttpRequest.new(GATEWAY).post( head: @headers, body: body )

        http.errback do
          error "GCM network error"
          @pool.remove_connection(@key)
        end
        http.callback do
          if http.response_header.status != 200
            error "GCM push error #{http.response_header.status}"
            error http.response
          else
            info "GCM push notification send"
            info http.response
          end
          @pool.remove_connection(@key)
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
suj-pusher-0.2.5 lib/suj/pusher/gcm_connection.rb
suj-pusher-0.2.3 lib/suj/pusher/gcm_connection.rb
suj-pusher-0.2.0 lib/suj/pusher/gcm_connection.rb
suj-pusher-0.1.5 lib/suj/pusher/gcm_connection.rb
suj-pusher-0.1.4 lib/suj/pusher/gcm_connection.rb
suj-pusher-0.1.3 lib/suj/pusher/gcm_connection.rb
suj-pusher-0.1.0 lib/suj/pusher/gcm_connection.rb