Module: C2dm::Connection

Defined in:
lib/c2dm_on_rails/libs/connection.rb

Class Method Summary collapse

Class Method Details

.open {|token| ... } ⇒ Object

Yields:

  • (token)


29
30
31
32
33
34
35
36
# File 'lib/c2dm_on_rails/libs/connection.rb', line 29

def open
   = GData::Auth::ClientLogin.new('ac2dm', :account_type => 'HOSTED_OR_GOOGLE')
  token = .get_token(configatron.c2dm.username,
                                         configatron.c2dm.password,
                                         configatron.c2dm.app_name)

  yield token
end

.send_notification(noty, token) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/c2dm_on_rails/libs/connection.rb', line 9

def send_notification(noty, token)
  headers = { "Content-Type" => "application/x-www-form-urlencoded", 
              "Authorization" => "GoogleLogin auth=#{token}" }

  message_data = noty.data.map{|k, v| "&data.#{k}=#{URI.escape(v)}"}.reduce{|k, v| k + v}
  data = "registration_id=#{noty.device.registration_id}&collapse_key=#{noty.collapse_key}#{message_data}"

  data = data + "&delay_while_idle" if noty.delay_while_idle

  url_string = configatron.c2dm.api_url
  url=URI.parse url_string
  http = Net::HTTP.new(url.host, url.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
   
  resp, dat = http.post(url.path, data, headers)

  return {:code => resp.code.to_i, :message => dat} 
end