class Notifiable::Gcm::Spacialdb::Batch

Public Instance Methods

batch() click to toggle source
# File lib/notifiable/gcm/spacialdb/batch.rb, line 15
def batch
  @batch ||= []
end
batch_size() click to toggle source
# File lib/notifiable/gcm/spacialdb/batch.rb, line 11
def batch_size
  @batch_size || 1000
end

Protected Instance Methods

enqueue(device_token, notification) click to toggle source
# File lib/notifiable/gcm/spacialdb/batch.rb, line 21
              def enqueue(device_token, notification)
raise "API Key missing" if @api_key.nil?

@batch_notification = notification
batch << device_token                                                                 
send_batch(notification) if batch.count >= batch_size
      end
flush() click to toggle source
# File lib/notifiable/gcm/spacialdb/batch.rb, line 29
def flush
  send_batch(@batch_notification) unless batch.empty?
end

Private Instance Methods

error_code(error_message) click to toggle source
# File lib/notifiable/gcm/spacialdb/batch.rb, line 74
def error_code(error_message)
  case error_message
  when "MissingRegistration"
    1
  when "InvalidRegistration"
    2
  when "MismatchSenderId"
    3
  when "NotRegistered"
    4
  when "MessageTooBig"
    5
  when "InvalidDataKey"
    6
  when "InvalidTtl"
    7
  when "Unavailable"
    8
  when "InternalServerError" 
    9   
  when "InvalidPackageName"
    10
  else
    0
  end
end
process_success_response(response) click to toggle source
# File lib/notifiable/gcm/spacialdb/batch.rb, line 52
def process_success_response(response)
                        body = JSON.parse(response.fetch(:body, "{}"))
                        results = body.fetch("results", [])
                        results.each_with_index do |result, i|
    dt = batch[i]
    
    if result["error"]
                                  dt.destroy if ["InvalidRegistration", "NotRegistered"].include? result["error"] 
      processed(dt, Notifiable::NotificationStatus::REJECTED_STATUS, error_code(result["error"]), result["error"])
      next; 
      
    elsif result["registration_id"] && Notifiable::DeviceToken.exists?(:token => result["registration_id"])
      dt.destroy
    elsif result["registration_id"]
      dt.update_attribute('token', result["registration_id"])
    end
    
    processed(dt)
                        end

end
send_batch(notification) click to toggle source
# File lib/notifiable/gcm/spacialdb/batch.rb, line 35
              def send_batch(notification)
                      fcm = ::FCM.new(@api_key)
        

data = {message: notification.message}
data[:title] = notification.title if notification.title
data = data.merge(notification.send_params)          
                      response = fcm.send(batch.collect{|dt| dt.token}, {:data => data})

if response[:status_code] == 200
                      process_success_response(response)
else
  logger.error "Sending notification id: #{notification.id} code: #{response[:status_code]} response: #{response[:response]}"
end         
@batch = []
              end