Class: C2dmBatch
- Inherits:
-
Object
- Object
- C2dmBatch
- Defined in:
- lib/c2dm_batch/core.rb
Instance Attribute Summary collapse
-
#auth_url ⇒ Object
Returns the value of attribute auth_url.
-
#email ⇒ Object
Returns the value of attribute email.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#password ⇒ Object
Returns the value of attribute password.
-
#send_url ⇒ Object
Returns the value of attribute send_url.
-
#source ⇒ Object
Returns the value of attribute source.
Instance Method Summary collapse
- #authenticate! ⇒ Object
-
#initialize ⇒ C2dmBatch
constructor
A new instance of C2dmBatch.
- #send_batch_notifications(notifications) ⇒ Object
- #send_notification(notification) ⇒ Object
Constructor Details
#initialize ⇒ C2dmBatch
Returns a new instance of C2dmBatch.
4 5 6 7 8 9 |
# File 'lib/c2dm_batch/core.rb', line 4 def initialize @auth_url = 'https://www.google.com/accounts/ClientLogin' @send_url = 'https://android.apis.google.com/c2dm/send' @hydra = Typhoeus::Hydra.new @logger = Logger.new(STDOUT) end |
Instance Attribute Details
#auth_url ⇒ Object
Returns the value of attribute auth_url.
2 3 4 |
# File 'lib/c2dm_batch/core.rb', line 2 def auth_url @auth_url end |
#email ⇒ Object
Returns the value of attribute email.
2 3 4 |
# File 'lib/c2dm_batch/core.rb', line 2 def email @email end |
#logger ⇒ Object
Returns the value of attribute logger.
2 3 4 |
# File 'lib/c2dm_batch/core.rb', line 2 def logger @logger end |
#password ⇒ Object
Returns the value of attribute password.
2 3 4 |
# File 'lib/c2dm_batch/core.rb', line 2 def password @password end |
#send_url ⇒ Object
Returns the value of attribute send_url.
2 3 4 |
# File 'lib/c2dm_batch/core.rb', line 2 def send_url @send_url end |
#source ⇒ Object
Returns the value of attribute source.
2 3 4 |
# File 'lib/c2dm_batch/core.rb', line 2 def source @source end |
Instance Method Details
#authenticate! ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/c2dm_batch/core.rb', line 11 def authenticate! request = Typhoeus::Request.new(@auth_url) = { 'accountType' => 'HOSTED_OR_GOOGLE', 'service' => 'ac2dm', 'Email' => @email, 'Passwd' => @password, 'source' => @source, } request.body = build_post_body() headers = { 'Content-length' => request.body.length.to_s } request.headers = headers @hydra.queue(request) @hydra.run response = request.response auth_token = "" if response.success? response.body.split("\n").each do |line| if line =~ /^Auth=/ auth_token = line.gsub('Auth=', '') end end end @auth_token = auth_token end |
#send_batch_notifications(notifications) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/c2dm_batch/core.rb', line 51 def send_batch_notifications(notifications) authenticate! requests = [] errors = [] notifications.each do |notification| request = create_notification_request(notification) requests << request request.method = :post request.on_complete do |response| if response.success? if response.body =~ /Error=(\w+)/ errors << { :registration_id => notification[:registration_id], :error => $1 } @logger.error("Error received: #{response.body}") @logger.info("Error sending: #{notification.to_json}") else @logger.info("Sent notification: #{notification.to_json}") requests.delete(request) end elsif response.code == 503 @hydra.abort raise RuntimeError end end @hydra.queue(request) end begin @hydra.run rescue RuntimeError requests.each do |failed_request| errors << { :registration_id => failed_request.body.match(/registration_id=(\w+)/)[1], :error => 503 } end end errors end |
#send_notification(notification) ⇒ Object
42 43 44 45 46 47 48 49 |
# File 'lib/c2dm_batch/core.rb', line 42 def send_notification(notification) authenticate! request = create_notification_request(notification) @hydra.queue(request) @hydra.run response = request.response end |