Class: C2dmBatch

Inherits:
Object
  • Object
show all
Defined in:
lib/c2dm_batch/core.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeC2dmBatch

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_urlObject

Returns the value of attribute auth_url.



2
3
4
# File 'lib/c2dm_batch/core.rb', line 2

def auth_url
  @auth_url
end

#emailObject

Returns the value of attribute email.



2
3
4
# File 'lib/c2dm_batch/core.rb', line 2

def email
  @email
end

#loggerObject

Returns the value of attribute logger.



2
3
4
# File 'lib/c2dm_batch/core.rb', line 2

def logger
  @logger
end

#passwordObject

Returns the value of attribute password.



2
3
4
# File 'lib/c2dm_batch/core.rb', line 2

def password
  @password
end

#send_urlObject

Returns the value of attribute send_url.



2
3
4
# File 'lib/c2dm_batch/core.rb', line 2

def send_url
  @send_url
end

#sourceObject

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)
  auth_options = {
    'accountType' => 'HOSTED_OR_GOOGLE',
    'service'     => 'ac2dm',
    'Email'       => @email,
    'Passwd'      => @password,
    'source'      => @source,
  }
  request.body = build_post_body(auth_options)

  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