pushmeup 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/.rvmrc CHANGED
@@ -34,15 +34,15 @@ else
34
34
  fi
35
35
 
36
36
  # If you use bundler, this might be useful to you:
37
- # if [[ -s Gemfile ]] && {
38
- # ! builtin command -v bundle >/dev/null ||
39
- # builtin command -v bundle | grep $rvm_path/bin/bundle >/dev/null
40
- # }
41
- # then
42
- # printf "%b" "The rubygem 'bundler' is not installed. Installing it now.\n"
43
- # gem install bundler
44
- # fi
45
- # if [[ -s Gemfile ]] && builtin command -v bundle >/dev/null
46
- # then
47
- # bundle install | grep -vE '^Using|Your bundle is complete'
48
- # fi
37
+ if [[ -s Gemfile ]] && {
38
+ ! builtin command -v bundle >/dev/null ||
39
+ builtin command -v bundle | grep $rvm_path/bin/bundle >/dev/null
40
+ }
41
+ then
42
+ printf "%b" "The rubygem 'bundler' is not installed. Installing it now.\n"
43
+ gem install bundler
44
+ fi
45
+ if [[ -s Gemfile ]] && builtin command -v bundle >/dev/null
46
+ then
47
+ bundle install | grep -vE '^Using|Your bundle is complete'
48
+ fi
Binary file
data/README.md CHANGED
@@ -1,21 +1,164 @@
1
- # Periodic Table
1
+ # Pushmeup
2
2
 
3
- [![Build Status](https://secure.travis-ci.org/NicosKaralis/pushmeup.png?branch=master)](http://travis-ci.org/NicosKaralis/pushmeup)
3
+ ### a gem for various push notification services.
4
+
5
+ ## Goals
6
+
7
+ Pushmeup is an attempt to create an push notifications center that could send push to devices like:
8
+
9
+ - Android
10
+ - iOS
11
+ - Mac OS X
12
+ - Windows Phone
13
+ - And many others
14
+
15
+ Currently we have only support for ``iOS`` and ``Android`` but we are planning code for more plataforms.
4
16
 
5
17
  ## Installation
6
18
 
7
- gem install pushmeup
19
+ $ gem install pushmeup
20
+
21
+ or add to your ``Gemfile``
22
+
23
+ gem 'pushmeup'
24
+
25
+ and install it with
26
+
27
+ $ bundle install
28
+
29
+ ## APNS (Apple iOS)
30
+
31
+ ### Configure
32
+
33
+ 1. In Keychain access export your certificate and your private key as a ``p12``.
34
+
35
+ ![Keychain Access](https://raw.github.com/NicosKaralis/pushmeup/master/Keychain Access.jpg)
36
+
37
+ 2. Run the following command to convert the ``p12`` to a ``pem`` file
38
+
39
+ $ openssl pkcs12 -in cert.p12 -out cert.pem -nodes -clcerts
40
+
41
+ 3. After you have created your ``pem`` file. Set what host, port, certificate file location on the APNS class. You just need to set this once:
42
+
43
+ APNS.host = 'gateway.push.apple.com'
44
+ # gateway.sandbox.push.apple.com is default
45
+
46
+ APNS.port = 2195
47
+ # this is also the default. Shouldn't ever have to set this, but just in case Apple goes crazy, you can.
48
+
49
+ APNS.pem = '/path/to/pem/file'
50
+ # this is the file you just created
51
+
52
+ APNS.pass = ''
53
+ # Just in case your pem need a password
54
+
55
+ ### Usage
56
+
57
+ #### Sending a single notification:
58
+
59
+ device_token = '123abc456def'
60
+ APNS.send_notification(device_token, 'Hello iPhone!' )
61
+ APNS.send_notification(device_token, :alert => 'Hello iPhone!', :badge => 1, :sound => 'default')
62
+
63
+ #### Sending multiple notifications
64
+
65
+ device_token = '123abc456def'
66
+ n1 = APNS::Notification.new(device_token, 'Hello iPhone!' )
67
+ n2 = APNS::Notification.new(device_token, :alert => 'Hello iPhone!', :badge => 1, :sound => 'default')
68
+ APNS.send_notifications([n1, n2])
69
+
70
+ #### Sending more information along
71
+
72
+ APNS.send_notification(device_token, :alert => 'Hello iPhone!', :badge => 1, :sound => 'default',
73
+ :other => {:sent => 'with apns gem', :custom_param => "value"})
74
+
75
+ this will result in a payload like this:
76
+
77
+ {"aps":{"alert":"Hello iPhone!","badge":1,"sound":"default"},"sent":"with apns gem", "custom_param":"value"}
78
+
79
+ ### Getting your iOS device token
80
+
81
+ - (void)applicationDidFinishLaunching:(UIApplication *)application {
82
+ // Register with apple that this app will use push notification
83
+ ...
84
+
85
+ [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeBadge)];
86
+
87
+ ...
88
+
89
+ }
90
+
91
+ - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
92
+ // Show the device token obtained from apple to the log
93
+ NSLog("deviceToken: %", deviceToken);
94
+ }
95
+
96
+ ## GCM (Google Cloud Messaging)
97
+
98
+ ### Configure
99
+
100
+ GCM.host = 'https://android.googleapis.com/gcm/send'
101
+ # https://android.googleapis.com/gcm/send is default
102
+
103
+ GCM.format = :json
104
+ # :json is default and only available at the moment
105
+
106
+ GCM.key = "123abc456def"
107
+ # this is the apiKey obtained from here https://code.google.com/apis/console/
108
+
109
+ ### Usage
110
+
111
+ #### Sending a single notification:
112
+
113
+ destination = ["device1", "device2", "device3"]
114
+ # can be an string or an array of strings containing the regIds of the devices you want to send
115
+
116
+ data = {:key => "value", :key2 => ["array", "value"]}
117
+ # must be an hash with all values you want inside you notification
118
+
119
+ GCM.send_notification( destination )
120
+ # Empty notification
121
+
122
+ GCM.send_notification( destination, data )
123
+ # Notification with custom information
124
+
125
+ GCM.send_notification( destination, data, :collapse_key => "placar_score_global", :time_to_live => 3600, :delay_while_idle => false )
126
+ # Notification with custom information and parameters
127
+
128
+ for more information on parameters check documentation: [GCM | Android Developers](http://developer.android.com/guide/google/gcm/gcm.html#request)
129
+
130
+ #### Sending multiple notifications:
131
+
132
+ destination1 = "device1"
133
+ destination2 = ["device2"]
134
+ destination3 = ["device1", "device2", "device3"]
135
+ # can be an string or an array of strings containing the regIds of the devices you want to send
136
+
137
+ data1 = {:key => "value", :key2 => ["array", "value"]}
138
+ # must be an hash with all values you want inside you notification
139
+
140
+ options1 = {:collapse_key => "placar_score_global", :time_to_live => 3600, :delay_while_idle => false}
141
+ # options for the notification
142
+
143
+ n1 = GCM::Notification.new(destination1, data1, options1)
144
+ n2 = GCM::Notification.new(destination2, data2)
145
+ n3 = GCM::Notification.new(destination3, data3, options2)
146
+
147
+ GCM.send_notifications( [n1, n2, n3] )
148
+ # In this case, every notification has his own parameters
149
+
150
+ for more information on parameters check documentation: [GCM | Android Developers](http://developer.android.com/guide/google/gcm/gcm.html#request)
151
+
152
+ #### Getting your Android device token (regId)
153
+
154
+ Check this link [GCM: Getting Started](http://developer.android.com/guide/google/gcm/gs.html)
8
155
 
9
- ## Usage
156
+ ## Build Status [![Build Status](https://secure.travis-ci.org/NicosKaralis/pushmeup.png?branch=master)](http://travis-ci.org/NicosKaralis/pushmeup) [![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/NicosKaralis/pushmeup)
10
157
 
11
- require 'pushmeup'
158
+ ## Dependency Status [![Dependency Status](https://gemnasium.com/NicosKaralis/pushmeup.png?travis)](https://gemnasium.com/NicosKaralis/pushmeup)
12
159
 
13
- # TODO: Make usage cases
160
+ ## License
14
161
 
15
- ## Contributing
162
+ Pushmeup is released under the MIT license:
16
163
 
17
- 1. Fork it
18
- 2. Create your feature branch (`git checkout -b my-new-feature`)
19
- 3. Commit your changes (`git commit -am 'Added some feature'`)
20
- 4. Push to the branch (`git push origin my-new-feature`)
21
- 5. Create new Pull Request
164
+ http://www.opensource.org/licenses/MIT
@@ -1 +1,2 @@
1
1
  require "pushmeup/gcm/core"
2
+ require "pushmeup/gcm/notification"
@@ -1,57 +1,80 @@
1
1
  require 'httparty'
2
- require 'cgi'
2
+ # require 'cgi'
3
3
  require 'json'
4
4
 
5
- class GCM
5
+ module GCM
6
6
  include HTTParty
7
7
 
8
8
  @host = 'https://android.googleapis.com/gcm/send'
9
-
10
- @base_uri = 'https://android.googleapis.com/gcm/send'
11
- @timeout = 30
12
9
  @format = :json
13
10
  @key = nil
14
11
 
15
12
  class << self
16
- attr_accessor :base_uri, :timeout, :format, :key
13
+ attr_accessor :host, :format, :key
17
14
  end
18
-
19
- # {
20
- # "collapse_key": "score_update",
21
- # "time_to_live": 108,
22
- # "delay_while_idle": true,
23
- # "registration_ids": ["4", "8", "15", "16", "23", "42"],
24
- # "data" : {
25
- # "score": "5x1",
26
- # "time": "15:10"
27
- # }
28
- # }
29
- # gcm = GCM.new(api_key)
30
- # gcm.send_notification({registration_ids: ["4sdsx", "8sdsd"], data: {score: "5x1"}})
31
- def self.send_notification(registration_ids, options = {})
32
- post_body = build_post_body(registration_ids, options)
33
-
34
- params = {
35
- :body => post_body.to_json,
36
- :headers => {
37
- 'Authorization' => "key=#{@key}",
38
- 'Content-Type' => 'application/json',
39
- }
40
- }
41
-
42
- response = self.post(@base_uri, params)
43
- build_response(response)
44
- # {body: response.body, headers: response.headers, status: response.code}
15
+
16
+ def self.send_notification(device_tokens, data = {}, options = {})
17
+ n = GCM::Notification.new(device_tokens, data, options)
18
+ self.send_notifications([n])
19
+ end
20
+
21
+ def self.send_notifications(notifications)
22
+ responses = []
23
+ notifications.each do |n|
24
+ responses << self.prepare_and_send(n)
25
+ end
26
+ responses
45
27
  end
46
28
 
47
29
  private
48
-
49
- def self.build_post_body(registration_ids, options={})
50
- body = {:registration_ids => registration_ids}.merge(options)
51
- #p body
52
- #raise exception if options[:time_to_live] && !options[:collapse_key]
30
+
31
+ def self.prepare_and_send(n)
32
+ if n.device_tokens.count < 1 || n.device_tokens.count > 1000
33
+ raise "Number of device_tokens invalid, keep it betwen 1 and 1000"
34
+ end
35
+ if !n.collapse_key.nil? && n.time_to_live.nil?
36
+ raise %q{If you are defining a "colapse key" you need a "time to live"}
37
+ end
38
+
39
+ if self.format == :json
40
+ self.send_push_as_json(n)
41
+ elsif self.format == :text
42
+ self.send_push_as_plain_text(n)
43
+ else
44
+ raise "Invalid format"
45
+ end
53
46
  end
54
-
47
+
48
+ def self.send_push_as_json(n)
49
+ headers = {
50
+ 'Authorization' => "key=#{self.key}",
51
+ 'Content-Type' => 'application/json',
52
+ }
53
+ body = {
54
+ :registration_ids => n.device_tokens,
55
+ :data => n.data,
56
+ :collapse_key => n.collapse_key,
57
+ :time_to_live => n.time_to_live,
58
+ :delay_while_idle => n.delay_while_idle
59
+ }
60
+ return self.send_to_server(headers, body.to_json)
61
+ end
62
+
63
+ def self.send_push_as_plain_text(n)
64
+ raise "Still has to be done: http://developer.android.com/guide/google/gcm/gcm.html"
65
+ headers = {
66
+ 'Authorization' => "key=#{self.key}",
67
+ 'Content-Type' => 'application/x-www-form-urlencoded;charset=UTF-8',
68
+ }
69
+ return self.send_to_server(headers, body)
70
+ end
71
+
72
+ def self.send_to_server(headers, body)
73
+ params = {:headers => headers, :body => body}
74
+ response = self.post('https://android.googleapis.com/gcm/send', params)
75
+ return build_response(response)
76
+ end
77
+
55
78
  def self.build_response(response)
56
79
  case response.code
57
80
  when 200
@@ -66,4 +89,5 @@ class GCM
66
89
  {:response => 'Server is temporarily unavailable.', :status_code => response.code}
67
90
  end
68
91
  end
92
+
69
93
  end
@@ -0,0 +1,45 @@
1
+ module GCM
2
+ class Notification
3
+ attr_accessor :device_tokens, :data, :collapse_key, :time_to_live, :delay_while_idle
4
+
5
+ def initialize(tokens, data, options = {})
6
+ self.device_tokens = tokens
7
+ self.data = data
8
+
9
+ @collapse_key = options[:collapse_key]
10
+ @time_to_live = options[:time_to_live]
11
+ @delay_while_idle = options[:delay_while_idle]
12
+ end
13
+
14
+ def device_tokens=(tokens)
15
+ if tokens.is_a?(Array)
16
+ @device_tokens = tokens
17
+ elsif tokens.is_a?(String)
18
+ @device_tokens = [tokens]
19
+ else
20
+ raise "device_tokens needs to be either a hash or string"
21
+ end
22
+ end
23
+
24
+ def data=(data)
25
+ if data.is_a?(Hash)
26
+ @data = data
27
+ else
28
+ raise "data parameter must be the tpe of Hash"
29
+ end
30
+ end
31
+
32
+ def delay_while_idle=(delay_while_idle)
33
+ @delay_while_idle = (delay_while_idle == true || delay_while_idle == :true)
34
+ end
35
+
36
+ def time_to_live=(time_to_live)
37
+ if time_to_live.is_a?(Integer)
38
+ @time_to_live = time_to_live
39
+ else
40
+ raise %q{"time_to_live" must be seconds as an integer value, like "100"}
41
+ end
42
+ end
43
+
44
+ end
45
+ end
@@ -1,3 +1,3 @@
1
1
  module Pushmeup
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -26,8 +26,8 @@ Gem::Specification.new do |s|
26
26
 
27
27
  s.require_paths = ["lib"]
28
28
 
29
- s.add_dependency('httparty')
30
- s.add_dependency('json')
29
+ s.add_dependency 'httparty'
30
+ s.add_dependency 'json'
31
31
 
32
32
  s.add_development_dependency 'rake'
33
33
  s.add_development_dependency 'rspec'
@@ -8,7 +8,7 @@ describe Pushmeup do
8
8
 
9
9
  it "should not forget the APNS default parameters" do
10
10
  APNS.host.should == "gateway.sandbox.push.apple.com"
11
- APNS.port.should be_equal(2195)
11
+ APNS.port.should == 2195
12
12
  APNS.pem.should be_equal(nil)
13
13
  APNS.pass.should be_equal(nil)
14
14
  end
@@ -16,8 +16,42 @@ describe Pushmeup do
16
16
  end
17
17
 
18
18
  describe "GCM" do
19
- it "should have a APNS object" do
19
+ it "should have a GCM object" do
20
20
  defined?(GCM).should_not be_false
21
21
  end
22
+
23
+ describe "Notifications" do
24
+
25
+ before do
26
+ @options = {:data => "dummy data"}
27
+ end
28
+
29
+ it "should allow only notifications with device_tokens as array" do
30
+ n = GCM::Notification.new("id", @options)
31
+ n.device_tokens.is_a?(Array).should be_true
32
+
33
+ n.device_tokens = ["a" "b", "c"]
34
+ n.device_tokens.is_a?(Array).should be_true
35
+
36
+ n.device_tokens = "a"
37
+ n.device_tokens.is_a?(Array).should be_true
38
+ end
39
+
40
+ it "should allow only notifications with data as hash with :data root" do
41
+ n = GCM::Notification.new("id", {:data => "data"})
42
+
43
+ n.data.is_a?(Hash).should be_true
44
+ n.data.should == {:data => "data"}
45
+
46
+ n.data = {:a => ["a", "b", "c"]}
47
+ n.data.is_a?(Hash).should be_true
48
+ n.data.should == {:a => ["a", "b", "c"]}
49
+
50
+ n.data = {:a => "a"}
51
+ n.data.is_a?(Hash).should be_true
52
+ n.data.should == {:a => "a"}
53
+ end
54
+
55
+ end
22
56
  end
23
57
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pushmeup
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-14 00:00:00.000000000 Z
12
+ date: 2012-08-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: httparty
@@ -91,6 +91,7 @@ files:
91
91
  - .rvmrc
92
92
  - .travis.yml
93
93
  - Gemfile
94
+ - Keychain Access.jpg
94
95
  - LICENSE
95
96
  - README.md
96
97
  - Rakefile
@@ -100,6 +101,7 @@ files:
100
101
  - lib/pushmeup/apns/notification.rb
101
102
  - lib/pushmeup/apple.rb
102
103
  - lib/pushmeup/gcm/core.rb
104
+ - lib/pushmeup/gcm/notification.rb
103
105
  - lib/pushmeup/version.rb
104
106
  - pushmeup.gemspec
105
107
  - spec/lib/pushmeup_spec.rb
@@ -118,7 +120,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
118
120
  version: '0'
119
121
  segments:
120
122
  - 0
121
- hash: -3467176487939634331
123
+ hash: 1966969484721973380
122
124
  required_rubygems_version: !ruby/object:Gem::Requirement
123
125
  none: false
124
126
  requirements:
@@ -127,7 +129,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
127
129
  version: '0'
128
130
  segments:
129
131
  - 0
130
- hash: -3467176487939634331
132
+ hash: 1966969484721973380
131
133
  requirements: []
132
134
  rubyforge_project: pushmeup
133
135
  rubygems_version: 1.8.24