class GCM
module GoogleCloudMessaging
Attributes
base_uri[RW]
body[R]
canonicals[R]
code[R]
errors[R]
raw[R]
results[R]
successes[R]
Public Class Methods
new(key, options = {})
click to toggle source
# File lib/google_cloud_messaging.rb, line 20 def initialize(key, options = {}) options = options || {} @key = key @base_uri = options[:base_uri] || 'https://gcm-http.googleapis.com/gcm/send' # TODO: allow user to pass in their own Typhoeus object... end
Public Instance Methods
send(data, tokens)
click to toggle source
# File lib/google_cloud_messaging.rb, line 49 def send(data, tokens) data['registration_ids'] = tokens request = Typhoeus::Request.new( @base_uri, method: :post, body: data.to_json, headers: { 'Content-Type' => 'application/json', 'Authorization' => "key=#{@key}" } ) @code = nil @raw = nil @body = nil @results = [] @successes = [] @canonicals = [] @errors = [] request.run @raw = got = request.response @code = got.code if got.code == 200 @body = data = JSON.parse(got.response_body) if got.response_body gres = data['results'] || [] (0..(gres.length-1)).each { |idx| result = gres[idx] token = tokens[idx] @results << Result.new(id: token, data: result) if result['message_id'] @successes << Result.new(id: token, message_id: result['message_id']) elsif result['error'] @errors << Result.new(id: token, error: result['error']) elsif result['registration_id'] @canonicals << Result.new(id: token, registration_id: result['registration_id']) end } end @body end