Sha256: 7d4bc039e555a9c103afbc7c5187cd6a8c017323b3421cd73389b3c0d000588f

Contents?: true

Size: 691 Bytes

Versions: 1

Compression:

Stored size: 691 Bytes

Contents

module GCMMiddleware
  class CanonicalId
    def initialize(app, options = {})
      @app = app
    end

    def call(env)
      save_ids(env.body)

      app.call(env).on_complete do |env|
        inject_original_ids(env.body) if has_registration_ids && env.body
      end
    end

    private

    attr_reader :registration_ids, :app

    def inject_original_ids(body)
      body['results'].each_with_index do |result, i|
        result['original_id'] = registration_ids[i]
      end
    end

    def save_ids(body)
      @registration_ids = body.fetch('registration_ids', []) if body
    end

    def has_registration_ids
      registration_ids && registration_ids.any?
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
gcm_middleware-0.0.1 lib/gcm_middleware/canonical_id.rb