Sha256: 3102ddae5644b2595df6dd884f768e6d14c3f32127c9f0b601f217203b8e045b

Contents?: true

Size: 1.14 KB

Versions: 1

Compression:

Stored size: 1.14 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],
          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

1 entries across 1 versions & 1 rubygems

Version Path
suj-pusher-0.0.1 lib/suj/pusher/gcm_connection.rb