Skip to content

Commit

Permalink
decouple authenticate from being an instance method
Browse files Browse the repository at this point in the history
  • Loading branch information
JC authored and amro committed Feb 7, 2012
1 parent f072ba5 commit b5101ac
Showing 1 changed file with 13 additions and 18 deletions.
31 changes: 13 additions & 18 deletions lib/c2dm.rb
Expand Up @@ -5,30 +5,18 @@ class C2DM
include HTTParty
default_timeout 30

attr_accessor :timeout, :username, :password, :source, :auth_token
attr_accessor :timeout, :auth_token

AUTH_URL = 'https://www.google.com/accounts/ClientLogin'
PUSH_URL = 'https://android.apis.google.com/c2dm/send'
DEFAULT_SOURCE = 'MyCompany-MyAppName-1.0'

def initialize(username=nil, password=nil, source=DEFAULT_SOURCE, auth_token=nil)
@username = username
@password = password
@source = source
@auth_token = auth_token
end

def authenticated?
!@auth_token.nil?
end

def authenticate!(username=nil, password=nil, source=nil)
def self.authenticate!(username=nil, password=nil, source=nil)
auth_options = {
'accountType' => 'HOSTED_OR_GOOGLE',
'service' => 'ac2dm',
'Email' => username || self.username,
'Passwd' => password || self.password,
'source' => source || self.source
'source' => source || 'MyCompany-MyAppName-1.0'
}
post_body = build_post_body(auth_options)

Expand All @@ -48,6 +36,10 @@ def authenticate!(username=nil, password=nil, source=nil)
@auth_token = response.body.split("\n")[2].gsub('Auth=', '')
end

def initialize(auth_token=nil)
@auth_token = auth_token
end

# {
# :registration_id => "...",
# :data => {
Expand All @@ -73,8 +65,8 @@ def send_notification(options)
end

class << self
def send_notifications(username=nil, password=nil, notifications=[], source=nil)
c2dm = C2DM.new(username, password, source)
def send_notifications(auth_token=nil, notifications=[])
c2dm = C2DM.new(auth_token)

notifications.collect do |notification|
{ :body => c2dm.send_notification(notification),
Expand All @@ -83,7 +75,10 @@ def send_notifications(username=nil, password=nil, notifications=[], source=nil)
end
end

private

private


def build_post_body(options={})
post_body = []

Expand Down

0 comments on commit b5101ac

Please sign in to comment.