c2dm-rails 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/Gemfile +15 -0
- data/Gemfile.lock +99 -0
- data/LICENSE.txt +20 -0
- data/README.markdown +54 -0
- data/Rakefile +46 -0
- data/VERSION +1 -0
- data/app/models/c2dm/app.rb +75 -0
- data/app/models/c2dm/base.rb +4 -0
- data/app/models/c2dm/device.rb +6 -0
- data/app/models/c2dm/notification.rb +9 -0
- data/c2dm-rails.gemspec +69 -0
- data/db/migrate/01_create_c2dm_apps.rb +17 -0
- data/db/migrate/02_create_c2dm_devices.rb +14 -0
- data/db/migrate/03_create_c2dm_notifications.rb +22 -0
- data/lib/c2dm-rails.rb +24 -0
- data/lib/connection.rb +92 -0
- data/test/helper.rb +18 -0
- metadata +115 -0
data/.document
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
# Add dependencies required to use your gem here.
|
3
|
+
# Example:
|
4
|
+
# gem "activesupport", ">= 2.3.5"
|
5
|
+
|
6
|
+
|
7
|
+
gem 'rails', "~> 3.1.0"
|
8
|
+
gem 'httparty', "> 0.6.0"
|
9
|
+
|
10
|
+
# Add dependencies to develop your gem here.
|
11
|
+
# Include everything needed to run rake, tests, features, etc.
|
12
|
+
group :development do
|
13
|
+
gem "bundler", "~> 1.0.0"
|
14
|
+
gem "jeweler", "~> 1.5.1"
|
15
|
+
end
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
actionmailer (3.1.3)
|
5
|
+
actionpack (= 3.1.3)
|
6
|
+
mail (~> 2.3.0)
|
7
|
+
actionpack (3.1.3)
|
8
|
+
activemodel (= 3.1.3)
|
9
|
+
activesupport (= 3.1.3)
|
10
|
+
builder (~> 3.0.0)
|
11
|
+
erubis (~> 2.7.0)
|
12
|
+
i18n (~> 0.6)
|
13
|
+
rack (~> 1.3.5)
|
14
|
+
rack-cache (~> 1.1)
|
15
|
+
rack-mount (~> 0.8.2)
|
16
|
+
rack-test (~> 0.6.1)
|
17
|
+
sprockets (~> 2.0.3)
|
18
|
+
activemodel (3.1.3)
|
19
|
+
activesupport (= 3.1.3)
|
20
|
+
builder (~> 3.0.0)
|
21
|
+
i18n (~> 0.6)
|
22
|
+
activerecord (3.1.3)
|
23
|
+
activemodel (= 3.1.3)
|
24
|
+
activesupport (= 3.1.3)
|
25
|
+
arel (~> 2.2.1)
|
26
|
+
tzinfo (~> 0.3.29)
|
27
|
+
activeresource (3.1.3)
|
28
|
+
activemodel (= 3.1.3)
|
29
|
+
activesupport (= 3.1.3)
|
30
|
+
activesupport (3.1.3)
|
31
|
+
multi_json (~> 1.0)
|
32
|
+
arel (2.2.1)
|
33
|
+
builder (3.0.0)
|
34
|
+
erubis (2.7.0)
|
35
|
+
git (1.2.5)
|
36
|
+
hike (1.2.1)
|
37
|
+
httparty (0.8.1)
|
38
|
+
multi_json
|
39
|
+
multi_xml
|
40
|
+
i18n (0.6.0)
|
41
|
+
jeweler (1.5.2)
|
42
|
+
bundler (~> 1.0.0)
|
43
|
+
git (>= 1.2.5)
|
44
|
+
rake
|
45
|
+
json (1.6.5)
|
46
|
+
mail (2.3.0)
|
47
|
+
i18n (>= 0.4.0)
|
48
|
+
mime-types (~> 1.16)
|
49
|
+
treetop (~> 1.4.8)
|
50
|
+
mime-types (1.17.2)
|
51
|
+
multi_json (1.0.4)
|
52
|
+
multi_xml (0.4.1)
|
53
|
+
polyglot (0.3.3)
|
54
|
+
rack (1.3.6)
|
55
|
+
rack-cache (1.1)
|
56
|
+
rack (>= 0.4)
|
57
|
+
rack-mount (0.8.3)
|
58
|
+
rack (>= 1.0.0)
|
59
|
+
rack-ssl (1.3.2)
|
60
|
+
rack
|
61
|
+
rack-test (0.6.1)
|
62
|
+
rack (>= 1.0)
|
63
|
+
rails (3.1.3)
|
64
|
+
actionmailer (= 3.1.3)
|
65
|
+
actionpack (= 3.1.3)
|
66
|
+
activerecord (= 3.1.3)
|
67
|
+
activeresource (= 3.1.3)
|
68
|
+
activesupport (= 3.1.3)
|
69
|
+
bundler (~> 1.0)
|
70
|
+
railties (= 3.1.3)
|
71
|
+
railties (3.1.3)
|
72
|
+
actionpack (= 3.1.3)
|
73
|
+
activesupport (= 3.1.3)
|
74
|
+
rack-ssl (~> 1.3.2)
|
75
|
+
rake (>= 0.8.7)
|
76
|
+
rdoc (~> 3.4)
|
77
|
+
thor (~> 0.14.6)
|
78
|
+
rake (0.9.2.2)
|
79
|
+
rdoc (3.12)
|
80
|
+
json (~> 1.4)
|
81
|
+
sprockets (2.0.3)
|
82
|
+
hike (~> 1.2)
|
83
|
+
rack (~> 1.0)
|
84
|
+
tilt (~> 1.1, != 1.3.0)
|
85
|
+
thor (0.14.6)
|
86
|
+
tilt (1.3.3)
|
87
|
+
treetop (1.4.10)
|
88
|
+
polyglot
|
89
|
+
polyglot (>= 0.3.1)
|
90
|
+
tzinfo (0.3.31)
|
91
|
+
|
92
|
+
PLATFORMS
|
93
|
+
ruby
|
94
|
+
|
95
|
+
DEPENDENCIES
|
96
|
+
bundler (~> 1.0.0)
|
97
|
+
httparty (> 0.6.0)
|
98
|
+
jeweler (~> 1.5.1)
|
99
|
+
rails (~> 3.1.0)
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010 Heiko Moeller, empuxa gmbh
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.markdown
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
# c2dm-rails
|
2
|
+
|
3
|
+
c2dm-rails provides infrastructure to send push notifications to Android devices via google [c2dm](http://code.google.com/android/c2dm/index.html).
|
4
|
+
|
5
|
+
##Installation
|
6
|
+
|
7
|
+
$ gem install c2dm-rails
|
8
|
+
$ rake c2dm:install:migrations
|
9
|
+
$ rake db:migrate
|
10
|
+
|
11
|
+
After you installed the gem, you need to generate the migrations necessary for the gem to work and run them.
|
12
|
+
|
13
|
+
##Requirements
|
14
|
+
|
15
|
+
An Android device running 2.2 or newer, its registration token, and a google account registered for c2dm.
|
16
|
+
|
17
|
+
##Usage
|
18
|
+
|
19
|
+
The gem is database driven. First of all, you need to create an app with the following credentials:
|
20
|
+
|
21
|
+
username: the username of the google account you entered in the c2dm registration
|
22
|
+
password: password for this account
|
23
|
+
application_id: the package of the android app (e.g. com.example)
|
24
|
+
sender_id: email account associated with the registration process. Can be the same as the user used to login
|
25
|
+
source: application identifier for google logging purposes. Should be of the form "companyName-applicationName-versionID"
|
26
|
+
|
27
|
+
This can be done from the console, as these credentials won't change much.
|
28
|
+
|
29
|
+
After you configured the app, you'll need to add devices to it. A device is identified by a `registration_id`, this can be send by the client each time it logs in, or can be done explicitly with a controller.
|
30
|
+
|
31
|
+
Once you have the device, you are able to add messages to it, simply create a new `C2dm::Notification` object via `device.notifications.new` with the following information:
|
32
|
+
|
33
|
+
data: a hash, { :key1 => "value1", :key2 => "value2" }
|
34
|
+
delay_while_idle: boolean telling the google server if it should wake the device if it is idle
|
35
|
+
collapse_key: this details a group of messages. If the device is offline, it will only receive the "last" message. As there is now guarantee of delivering order, the "last" message might not be last message sent by your application
|
36
|
+
|
37
|
+
Any other field is used to keep state by the delivery engine and should not be modified.
|
38
|
+
|
39
|
+
After you added messages, get an instance of your app and send the messages:
|
40
|
+
|
41
|
+
app = C2dm::App.find(some_id)
|
42
|
+
app.deliver_notifications
|
43
|
+
|
44
|
+
The app contains an exponential back-off mechanism for notifications that failed to deliver. It is of the form `last_sent_date + 10 ** tries`, with a maximum of 4 tries (about three hours).
|
45
|
+
|
46
|
+
|
47
|
+
|
48
|
+
##Copyrights
|
49
|
+
|
50
|
+
* Copyright (c) 2010-2011 Heiko Moeller, empuxa gmbh. See LICENSE.txt for details.
|
51
|
+
|
52
|
+
##Thanks
|
53
|
+
|
54
|
+
* [amro](https://github.com/amro/c2dm) for the basis of this gem
|
data/Rakefile
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
begin
|
4
|
+
Bundler.setup(:default, :development)
|
5
|
+
rescue Bundler::BundlerError => e
|
6
|
+
$stderr.puts e.message
|
7
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
8
|
+
exit e.status_code
|
9
|
+
end
|
10
|
+
require 'rake'
|
11
|
+
|
12
|
+
require 'jeweler'
|
13
|
+
Jeweler::Tasks.new do |gem|
|
14
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
15
|
+
gem.name = "c2dm-rails"
|
16
|
+
gem.homepage = "https://github.com/Femaref/c2dm-rails"
|
17
|
+
gem.license = "MIT"
|
18
|
+
gem.summary = %Q{c2dm-rails provides infrastructure to send push notifications to android devices via google c2dm.}
|
19
|
+
gem.description = %Q{c2dm-rails provides infrastructure to send push notifications to android devices via google c2dm.}
|
20
|
+
gem.email = "femaref@gmail.com"
|
21
|
+
gem.authors = ["Heiko Moeller"]
|
22
|
+
# Include your dependencies below. Runtime dependencies are required when using your gem,
|
23
|
+
# and development dependencies are only needed for development (ie running rake tasks, tests, etc)
|
24
|
+
# gem.add_runtime_dependency 'jabber4r', '> 0.1'
|
25
|
+
# gem.add_development_dependency 'rspec', '> 1.2.3'
|
26
|
+
end
|
27
|
+
Jeweler::RubygemsDotOrgTasks.new
|
28
|
+
|
29
|
+
require 'rake/testtask'
|
30
|
+
Rake::TestTask.new(:test) do |test|
|
31
|
+
test.libs << 'lib' << 'test'
|
32
|
+
test.pattern = 'test/**/test_*.rb'
|
33
|
+
test.verbose = true
|
34
|
+
end
|
35
|
+
|
36
|
+
task :default => :test
|
37
|
+
|
38
|
+
require 'rdoc/task'
|
39
|
+
Rake::RDocTask.new do |rdoc|
|
40
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
41
|
+
|
42
|
+
rdoc.rdoc_dir = 'rdoc'
|
43
|
+
rdoc.title = "c2dm-rails #{version}"
|
44
|
+
rdoc.rdoc_files.include('README*')
|
45
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
46
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.0.0
|
@@ -0,0 +1,75 @@
|
|
1
|
+
class C2dm::App < C2dm::Base
|
2
|
+
set_table_name "c2dm_apps"
|
3
|
+
|
4
|
+
has_many :devices, :class_name => "C2dm::Device", :dependent => :destroy
|
5
|
+
has_many :notifications, :through => :devices, :dependent => :destroy
|
6
|
+
|
7
|
+
def deliver_notifications
|
8
|
+
connection = C2dm::Connection.new(username, password, source)
|
9
|
+
|
10
|
+
self.notifications.deliverable.each do |notification|
|
11
|
+
if (notification.sent_at + (10 ** notification.tries).seconds) < Time.now
|
12
|
+
deliver_notification(notification, connection)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def deliver_notification (notification, connection = nil)
|
18
|
+
connection ||= C2dm::Connection.new(username, password, source)
|
19
|
+
|
20
|
+
options = {
|
21
|
+
:registration_id => notification.device.registration_id,
|
22
|
+
:data => notification.data,
|
23
|
+
:collapse_key => notification.collapse_key,
|
24
|
+
:delay_while_idle => notification.delay_while_idle
|
25
|
+
}
|
26
|
+
|
27
|
+
response = connection.send_notification(options)
|
28
|
+
|
29
|
+
answer = Hash[response.body.scan(/(\w+)=(\w+)/).map{ |a| [a[0].downcase.to_sym, a[1]]}]
|
30
|
+
|
31
|
+
notification.message_id = answer[:id]
|
32
|
+
notification.sent_at = Time.now
|
33
|
+
notification.deliver = false
|
34
|
+
|
35
|
+
if response.response.code == 200 # request executed correctly
|
36
|
+
if answer.has_key? :error
|
37
|
+
notification.error = answer[:error]
|
38
|
+
|
39
|
+
# these are recoverable errors
|
40
|
+
if notification.error == "QuotaExceeded" || notification.error == "DeviceQuotaExceeded"
|
41
|
+
notification.try += 1
|
42
|
+
if notification.try <= 4
|
43
|
+
notification.deliver = true
|
44
|
+
else
|
45
|
+
notification.error = "TriesExceeded"
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
# not a device connected with this app, delete it
|
50
|
+
if notification.error == "InvalidRegistration" || notification.error == "NotRegistered"
|
51
|
+
notification.device.delete
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
elsif response.response.code == 503 # service not available, redeliver message later
|
56
|
+
notification.error = "Service temporarily unavailable"
|
57
|
+
|
58
|
+
notification.try += 1
|
59
|
+
|
60
|
+
if notification.try <= 4
|
61
|
+
notification.deliver = true
|
62
|
+
else
|
63
|
+
notification.error = "TriesExceeded"
|
64
|
+
end
|
65
|
+
|
66
|
+
elsif response.response.code == 401 # wrong api credentials
|
67
|
+
notification.error = "wrong authtoken, check corresponding login"
|
68
|
+
else
|
69
|
+
notification.error = "unknown error"
|
70
|
+
end
|
71
|
+
|
72
|
+
notification.save
|
73
|
+
end
|
74
|
+
|
75
|
+
end
|
data/c2dm-rails.gemspec
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = "c2dm-rails"
|
8
|
+
s.version = "1.0.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Heiko Moeller"]
|
12
|
+
s.date = "2012-02-08"
|
13
|
+
s.description = "c2dm-rails provides infrastructure to send push notifications to android devices via google c2dm."
|
14
|
+
s.email = "femaref@gmail.com"
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE.txt",
|
17
|
+
"README.markdown"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
"Gemfile",
|
22
|
+
"Gemfile.lock",
|
23
|
+
"LICENSE.txt",
|
24
|
+
"README.markdown",
|
25
|
+
"Rakefile",
|
26
|
+
"VERSION",
|
27
|
+
"app/models/c2dm/app.rb",
|
28
|
+
"app/models/c2dm/base.rb",
|
29
|
+
"app/models/c2dm/device.rb",
|
30
|
+
"app/models/c2dm/notification.rb",
|
31
|
+
"c2dm-rails.gemspec",
|
32
|
+
"db/migrate/01_create_c2dm_apps.rb",
|
33
|
+
"db/migrate/02_create_c2dm_devices.rb",
|
34
|
+
"db/migrate/03_create_c2dm_notifications.rb",
|
35
|
+
"lib/c2dm-rails.rb",
|
36
|
+
"lib/connection.rb",
|
37
|
+
"test/helper.rb"
|
38
|
+
]
|
39
|
+
s.homepage = "https://github.com/Femaref/c2dm-rails"
|
40
|
+
s.licenses = ["MIT"]
|
41
|
+
s.require_paths = ["lib"]
|
42
|
+
s.rubygems_version = "1.8.10"
|
43
|
+
s.summary = "c2dm-rails provides infrastructure to send push notifications to android devices via google c2dm."
|
44
|
+
s.test_files = [
|
45
|
+
"test/helper.rb"
|
46
|
+
]
|
47
|
+
|
48
|
+
if s.respond_to? :specification_version then
|
49
|
+
s.specification_version = 3
|
50
|
+
|
51
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
52
|
+
s.add_runtime_dependency(%q<rails>, ["~> 3.1.0"])
|
53
|
+
s.add_runtime_dependency(%q<httparty>, ["> 0.6.0"])
|
54
|
+
s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
|
55
|
+
s.add_development_dependency(%q<jeweler>, ["~> 1.5.1"])
|
56
|
+
else
|
57
|
+
s.add_dependency(%q<rails>, ["~> 3.1.0"])
|
58
|
+
s.add_dependency(%q<httparty>, ["> 0.6.0"])
|
59
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
60
|
+
s.add_dependency(%q<jeweler>, ["~> 1.5.1"])
|
61
|
+
end
|
62
|
+
else
|
63
|
+
s.add_dependency(%q<rails>, ["~> 3.1.0"])
|
64
|
+
s.add_dependency(%q<httparty>, ["> 0.6.0"])
|
65
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
66
|
+
s.add_dependency(%q<jeweler>, ["~> 1.5.1"])
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
@@ -0,0 +1,17 @@
|
|
1
|
+
class CreateC2dmApps < ActiveRecord::Migration
|
2
|
+
def self.up
|
3
|
+
create_table :c2dm_apps do |t|
|
4
|
+
t.string :username
|
5
|
+
t.string :password
|
6
|
+
t.string :application_id
|
7
|
+
t.string :sender_id
|
8
|
+
t.string :source
|
9
|
+
|
10
|
+
t.timestamps
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.down
|
15
|
+
drop_table :c2dm_apps
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
class CreateC2dmNotifications < ActiveRecord::Migration
|
2
|
+
def self.up
|
3
|
+
create_table :c2dm_notifications do |t|
|
4
|
+
t.string :collapse_key
|
5
|
+
t.text :data
|
6
|
+
t.boolean :delay_while_idle
|
7
|
+
t.integer :device_id
|
8
|
+
|
9
|
+
t.integer :message_id
|
10
|
+
t.string :error
|
11
|
+
t.boolean :deliver, :default => true
|
12
|
+
t.datetime :sent_at
|
13
|
+
t.integer :try
|
14
|
+
|
15
|
+
t.timestamps
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.down
|
20
|
+
drop_table :c2dm_notifications
|
21
|
+
end
|
22
|
+
end
|
data/lib/c2dm-rails.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require "c2dm-rails"
|
2
|
+
require "connection"
|
3
|
+
|
4
|
+
module C2dm
|
5
|
+
mattr_accessor :auth_url
|
6
|
+
mattr_accessor :push_url
|
7
|
+
|
8
|
+
@@auth_url = 'https://www.google.com/accounts/ClientLogin'
|
9
|
+
@@push_url = 'https://android.apis.google.com/c2dm/send'
|
10
|
+
|
11
|
+
def self.setup
|
12
|
+
yield self
|
13
|
+
end
|
14
|
+
|
15
|
+
class Engine < Rails::Engine
|
16
|
+
engine_name "c2dm"
|
17
|
+
|
18
|
+
config.c2dm = C2dm
|
19
|
+
|
20
|
+
initializer "c2dm.initialize" do |app|
|
21
|
+
app.config.c2dm = C2dm
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
data/lib/connection.rb
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
require 'httparty'
|
2
|
+
require 'cgi'
|
3
|
+
|
4
|
+
module C2dm
|
5
|
+
class Connection
|
6
|
+
include HTTParty
|
7
|
+
default_timeout 30
|
8
|
+
|
9
|
+
attr_accessor :timeout, :username, :password, :source, :access_token
|
10
|
+
|
11
|
+
def initialize(username, password, source)
|
12
|
+
@username = username
|
13
|
+
@password = password
|
14
|
+
@source = source
|
15
|
+
|
16
|
+
authenticate!
|
17
|
+
end
|
18
|
+
|
19
|
+
def authenticated?
|
20
|
+
!@auth_token.nil?
|
21
|
+
end
|
22
|
+
|
23
|
+
def authenticate!
|
24
|
+
auth_options = {
|
25
|
+
'accountType' => 'HOSTED_OR_GOOGLE',
|
26
|
+
'service' => 'ac2dm',
|
27
|
+
'Email' => self.username,
|
28
|
+
'Passwd' => self.password,
|
29
|
+
'source' => self.source
|
30
|
+
}
|
31
|
+
post_body = build_post_body(auth_options)
|
32
|
+
|
33
|
+
params = {
|
34
|
+
:body => post_body,
|
35
|
+
:headers => {
|
36
|
+
'Content-type' => 'application/x-www-form-urlencoded',
|
37
|
+
'Content-length' => post_body.length.to_s
|
38
|
+
}
|
39
|
+
}
|
40
|
+
|
41
|
+
response = self.class.post(C2dm.auth_url, params)
|
42
|
+
|
43
|
+
# check for authentication failures
|
44
|
+
raise response.parsed_response if response['Error=']
|
45
|
+
|
46
|
+
@auth_token = response.body.split("\n")[2].gsub('Auth=', '')
|
47
|
+
end
|
48
|
+
|
49
|
+
# {
|
50
|
+
# :registration_id => "...",
|
51
|
+
# :data => {
|
52
|
+
# :some_message => "Hi!",
|
53
|
+
# :another_message => 7
|
54
|
+
# }
|
55
|
+
# :collapse_key => "optional collapse_key string"
|
56
|
+
# }
|
57
|
+
def send_notification(options)
|
58
|
+
options[:collapse_key] ||= 'foo'
|
59
|
+
post_body = build_post_body(options)
|
60
|
+
|
61
|
+
params = {
|
62
|
+
:body => post_body,
|
63
|
+
:headers => {
|
64
|
+
'Authorization' => "GoogleLogin auth=#{@auth_token}",
|
65
|
+
'Content-type' => 'application/x-www-form-urlencoded',
|
66
|
+
'Content-length' => "#{post_body.length}"
|
67
|
+
}
|
68
|
+
}
|
69
|
+
|
70
|
+
self.class.post(C2dm.push_url, params)
|
71
|
+
end
|
72
|
+
|
73
|
+
|
74
|
+
private
|
75
|
+
def build_post_body(options={})
|
76
|
+
post_body = []
|
77
|
+
|
78
|
+
# data attributes need a key in the form of "data.key"...
|
79
|
+
data_attributes = options.delete(:data)
|
80
|
+
data_attributes.each_pair do |k,v|
|
81
|
+
post_body << "data.#{k}=#{CGI::escape(v.to_s)}"
|
82
|
+
end if data_attributes
|
83
|
+
|
84
|
+
options.each_pair do |k,v|
|
85
|
+
post_body << "#{k}=#{CGI::escape(v.to_s)}"
|
86
|
+
end
|
87
|
+
|
88
|
+
post_body.join('&')
|
89
|
+
end
|
90
|
+
|
91
|
+
end
|
92
|
+
end
|
data/test/helper.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
begin
|
4
|
+
Bundler.setup(:default, :development)
|
5
|
+
rescue Bundler::BundlerError => e
|
6
|
+
$stderr.puts e.message
|
7
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
8
|
+
exit e.status_code
|
9
|
+
end
|
10
|
+
require 'test/unit'
|
11
|
+
require 'shoulda'
|
12
|
+
|
13
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
14
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
15
|
+
require 'c2dm-rails'
|
16
|
+
|
17
|
+
class Test::Unit::TestCase
|
18
|
+
end
|
metadata
ADDED
@@ -0,0 +1,115 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: c2dm-rails
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Heiko Moeller
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-02-08 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rails
|
16
|
+
requirement: &70282073720480 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 3.1.0
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70282073720480
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: httparty
|
27
|
+
requirement: &70282073719080 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>'
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 0.6.0
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70282073719080
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: bundler
|
38
|
+
requirement: &70282069687240 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ~>
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 1.0.0
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *70282069687240
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: jeweler
|
49
|
+
requirement: &70282069685320 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 1.5.1
|
55
|
+
type: :development
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *70282069685320
|
58
|
+
description: c2dm-rails provides infrastructure to send push notifications to android
|
59
|
+
devices via google c2dm.
|
60
|
+
email: femaref@gmail.com
|
61
|
+
executables: []
|
62
|
+
extensions: []
|
63
|
+
extra_rdoc_files:
|
64
|
+
- LICENSE.txt
|
65
|
+
- README.markdown
|
66
|
+
files:
|
67
|
+
- .document
|
68
|
+
- Gemfile
|
69
|
+
- Gemfile.lock
|
70
|
+
- LICENSE.txt
|
71
|
+
- README.markdown
|
72
|
+
- Rakefile
|
73
|
+
- VERSION
|
74
|
+
- app/models/c2dm/app.rb
|
75
|
+
- app/models/c2dm/base.rb
|
76
|
+
- app/models/c2dm/device.rb
|
77
|
+
- app/models/c2dm/notification.rb
|
78
|
+
- c2dm-rails.gemspec
|
79
|
+
- db/migrate/01_create_c2dm_apps.rb
|
80
|
+
- db/migrate/02_create_c2dm_devices.rb
|
81
|
+
- db/migrate/03_create_c2dm_notifications.rb
|
82
|
+
- lib/c2dm-rails.rb
|
83
|
+
- lib/connection.rb
|
84
|
+
- test/helper.rb
|
85
|
+
homepage: https://github.com/Femaref/c2dm-rails
|
86
|
+
licenses:
|
87
|
+
- MIT
|
88
|
+
post_install_message:
|
89
|
+
rdoc_options: []
|
90
|
+
require_paths:
|
91
|
+
- lib
|
92
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
93
|
+
none: false
|
94
|
+
requirements:
|
95
|
+
- - ! '>='
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
|
+
segments:
|
99
|
+
- 0
|
100
|
+
hash: 2951700663620131590
|
101
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
102
|
+
none: false
|
103
|
+
requirements:
|
104
|
+
- - ! '>='
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
version: '0'
|
107
|
+
requirements: []
|
108
|
+
rubyforge_project:
|
109
|
+
rubygems_version: 1.8.10
|
110
|
+
signing_key:
|
111
|
+
specification_version: 3
|
112
|
+
summary: c2dm-rails provides infrastructure to send push notifications to android
|
113
|
+
devices via google c2dm.
|
114
|
+
test_files:
|
115
|
+
- test/helper.rb
|