Module: C2dmBatch

Defined in:
lib/c2dm_batch.rb,
lib/c2dm_batch/core.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.auth_urlObject

Returns the value of attribute auth_url.



12
13
14
# File 'lib/c2dm_batch/core.rb', line 12

def auth_url
  @auth_url
end

.emailObject

Returns the value of attribute email.



12
13
14
# File 'lib/c2dm_batch/core.rb', line 12

def email
  @email
end

.passwordObject

Returns the value of attribute password.



12
13
14
# File 'lib/c2dm_batch/core.rb', line 12

def password
  @password
end

.send_urlObject

Returns the value of attribute send_url.



12
13
14
# File 'lib/c2dm_batch/core.rb', line 12

def send_url
  @send_url
end

.sourceObject

Returns the value of attribute source.



12
13
14
# File 'lib/c2dm_batch/core.rb', line 12

def source
  @source
end

Class Method Details

.authenticate!Object



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
41
42
43
44
# File 'lib/c2dm_batch/core.rb', line 15

def self.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



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
88
# File 'lib/c2dm_batch/core.rb', line 55

def self.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 }
        else
          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



46
47
48
49
50
51
52
53
# File 'lib/c2dm_batch/core.rb', line 46

def self.send_notification(notification)
  authenticate!
  request = create_notification_request(notification)

  @hydra.queue(request)
  @hydra.run
  response = request.response
end