pushmeup 0.0.3 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -38,7 +38,7 @@ and install it with
38
38
 
39
39
  $ openssl pkcs12 -in cert.p12 -out cert.pem -nodes -clcerts
40
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:
41
+ 3. After you have created your ``pem`` file. Set the host, port and certificate file location on the APNS class. You just need to set this once:
42
42
 
43
43
  APNS.host = 'gateway.push.apple.com'
44
44
  # gateway.sandbox.push.apple.com is default
@@ -153,6 +153,38 @@ for more information on parameters check documentation: [GCM | Android Developer
153
153
 
154
154
  Check this link [GCM: Getting Started](http://developer.android.com/guide/google/gcm/gs.html)
155
155
 
156
+ ### (Optional) You can add multiple keys for GCM
157
+
158
+ You can use multiple keys to send notifications, to do it just do this changes in the code
159
+
160
+ #### Configure
161
+
162
+ GCM.key = { :key1 => "123abc456def", :key2 => "456def123abc" }
163
+ # the ``:key1`` and the ``:key2`` can be any object, they can be the projectID, the date, the version, doesn't matter.
164
+ # The only restrain is: they need to be valid keys for a hash.
165
+
166
+ #### Usage
167
+
168
+ # For single notification
169
+ GCM.send_notification( destination, :identity => :key1 )
170
+ # Empty notification
171
+
172
+ GCM.send_notification( destination, data, :identity => :key1 )
173
+ # Notification with custom information
174
+
175
+ GCM.send_notification( destination, data, :collapse_key => "placar_score_global", :time_to_live => 3600, :delay_while_idle => false, :identity => :key1 )
176
+ # Notification with custom information and parameters
177
+
178
+ # For multiple notifications
179
+ options1 = {}
180
+ options2 = {..., :identity => :key2}
181
+ n1 = GCM::Notification.new(destination1, data1, options1.merge({:identity => :key2}))
182
+ n2 = GCM::Notification.new(destination2, data2, :identity => :key1)
183
+ n3 = GCM::Notification.new(destination3, data3, options2)
184
+
185
+ GCM.send_notifications( [n1, n2, n3] )
186
+ # In this case, every notification has his own parameters, options and key
187
+
156
188
  ## 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)
157
189
 
158
190
  ## Dependency Status [![Dependency Status](https://gemnasium.com/NicosKaralis/pushmeup.png?travis)](https://gemnasium.com/NicosKaralis/pushmeup)
@@ -32,19 +32,20 @@ module APNS
32
32
 
33
33
  def self.feedback
34
34
  raise "Not implemented yet"
35
- # sock, ssl = self.feedback_connection
36
- #
37
- # apns_feedback = []
38
- # while line = ssl.read(38) # Read lines from the socket
39
- # line.strip!
40
- # f = line.unpack('N1n1H140')
41
- # apns_feedback << [Time.at(f[0]), f[1], f[2]]
42
- # end
43
- #
44
- # ssl.close
45
- # sock.close
46
- #
47
- # return apns_feedback
35
+ sock, ssl = self.feedback_connection
36
+
37
+ apns_feedback = []
38
+
39
+ while line = sock.gets # Read lines from the socket
40
+ line.strip!
41
+ f = line.unpack('N1n1H140')
42
+ apns_feedback << [Time.at(f[0]), f[2]]
43
+ end
44
+
45
+ ssl.close
46
+ sock.close
47
+
48
+ return apns_feedback
48
49
  end
49
50
 
50
51
  protected
@@ -64,32 +65,22 @@ module APNS
64
65
  return sock, ssl
65
66
  end
66
67
 
67
- # def self.feedback_connection
68
- # raise "The path to your pem file is not set. (APNS.pem = /path/to/cert.pem)" unless self.pem
69
- # raise "The path to your pem file does not exist!" unless File.exist?(self.pem)
70
- #
71
- # context = OpenSSL::SSL::SSLContext.new
72
- # context.cert = OpenSSL::X509::Certificate.new(File.read(self.pem))
73
- # context.key = OpenSSL::PKey::RSA.new(File.read(self.pem), self.pass)
74
- # context.verify_mode = OpenSSL::SSL::VERIFY_NONE
75
- #
76
- # fhost = self.host.gsub('gateway','feedback')
77
- # puts fhost
78
- #
79
- # sock = TCPSocket.new(fhost, 2196)
80
- # ssl = OpenSSL::SSL::SSLSocket.new(sock,context)
81
- # # ssl.connect
82
- #
83
- # ssl.sync_close = true
84
- # ssl.connect
85
- #
86
- # ssl.puts("GET / HTTP/1.0")
87
- # ssl.puts("")
88
- # while line = ssl.gets
89
- # puts line
90
- # end
91
- #
92
- # return sock, ssl
93
- # end
68
+ def self.feedback_connection
69
+ raise "The path to your pem file is not set. (APNS.pem = /path/to/cert.pem)" unless self.pem
70
+ raise "The path to your pem file does not exist!" unless File.exist?(self.pem)
71
+
72
+ context = OpenSSL::SSL::SSLContext.new
73
+ context.cert = OpenSSL::X509::Certificate.new(File.read(self.pem))
74
+ context.key = OpenSSL::PKey::RSA.new(File.read(self.pem), self.pass)
75
+
76
+ fhost = self.host.gsub('gateway','feedback')
77
+ puts fhost
78
+
79
+ sock = TCPSocket.new(fhost, 2196)
80
+ ssl = OpenSSL::SSL::SSLSocket.new(sock,context)
81
+ ssl.connect
82
+
83
+ return sock, ssl
84
+ end
94
85
 
95
86
  end
@@ -35,6 +35,9 @@ module GCM
35
35
  if !n.collapse_key.nil? && n.time_to_live.nil?
36
36
  raise %q{If you are defining a "colapse key" you need a "time to live"}
37
37
  end
38
+ if self.key.is_a?(Hash) && n.identity.nil?
39
+ raise %{If your key is a hash of keys you'l need to pass a identifier to the notification!}
40
+ end
38
41
 
39
42
  if self.format == :json
40
43
  self.send_push_as_json(n)
@@ -47,7 +50,7 @@ module GCM
47
50
 
48
51
  def self.send_push_as_json(n)
49
52
  headers = {
50
- 'Authorization' => "key=#{self.key}",
53
+ 'Authorization' => "key=#{ self.key.is_a?(Hash) ? self.key[n.identity] : self.key }",
51
54
  'Content-Type' => 'application/json',
52
55
  }
53
56
  body = {
@@ -63,7 +66,8 @@ module GCM
63
66
  def self.send_push_as_plain_text(n)
64
67
  raise "Still has to be done: http://developer.android.com/guide/google/gcm/gcm.html"
65
68
  headers = {
66
- 'Authorization' => "key=#{self.key}",
69
+ # TODO: Aceitar key ser um hash
70
+ 'Authorization' => "key=#{ self.key.is_a?(Hash) ? self.key[n.identity] : self.key }",
67
71
  'Content-Type' => 'application/x-www-form-urlencoded;charset=UTF-8',
68
72
  }
69
73
  return self.send_to_server(headers, body)
@@ -78,7 +82,7 @@ module GCM
78
82
  def self.build_response(response)
79
83
  case response.code
80
84
  when 200
81
- {:response => 'success', :body => response.body, :headers => response.headers, :status_code => response.code}
85
+ {:response => 'success', :body => JSON.parse(response.body), :headers => response.headers, :status_code => response.code}
82
86
  when 400
83
87
  {:response => 'Only applies for JSON requests. Indicates that the request could not be parsed as JSON, or it contained invalid fields.', :status_code => response.code}
84
88
  when 401
@@ -1,6 +1,6 @@
1
1
  module GCM
2
2
  class Notification
3
- attr_accessor :device_tokens, :data, :collapse_key, :time_to_live, :delay_while_idle
3
+ attr_accessor :device_tokens, :data, :collapse_key, :time_to_live, :delay_while_idle, :identity
4
4
 
5
5
  def initialize(tokens, data, options = {})
6
6
  self.device_tokens = tokens
@@ -9,6 +9,7 @@ module GCM
9
9
  @collapse_key = options[:collapse_key]
10
10
  @time_to_live = options[:time_to_live]
11
11
  @delay_while_idle = options[:delay_while_idle]
12
+ @identity = options[:identity]
12
13
  end
13
14
 
14
15
  def device_tokens=(tokens)
@@ -1,3 +1,3 @@
1
1
  module Pushmeup
2
- VERSION = "0.0.3"
2
+ VERSION = "0.1.0"
3
3
  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.3
4
+ version: 0.1.0
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-15 00:00:00.000000000 Z
12
+ date: 2012-11-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: httparty
@@ -120,7 +120,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
120
120
  version: '0'
121
121
  segments:
122
122
  - 0
123
- hash: 1966969484721973380
123
+ hash: -2576380722783718588
124
124
  required_rubygems_version: !ruby/object:Gem::Requirement
125
125
  none: false
126
126
  requirements:
@@ -129,7 +129,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
129
129
  version: '0'
130
130
  segments:
131
131
  - 0
132
- hash: 1966969484721973380
132
+ hash: -2576380722783718588
133
133
  requirements: []
134
134
  rubyforge_project: pushmeup
135
135
  rubygems_version: 1.8.24