zero_push 2.4.1 → 2.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3eccff0e44935d797e6160a61ef05064ede94bcb
4
- data.tar.gz: 0efedf88080e645e9d6cfd2d078402a25ff395e9
3
+ metadata.gz: 53395004125fe3080de63c22f54cf1c0a5bb03d3
4
+ data.tar.gz: 75b2fa3bb0bebfe3ced3da55a7574e3c837fba6e
5
5
  SHA512:
6
- metadata.gz: 51d67f43b23eb7b775b719949a2e3315c43d317904171e7e898090639ff2c05122ea015751f192a8db76caa42d34939b3df1e33b1485793182f09696799b79c1
7
- data.tar.gz: e44908c18525b36e72c94e428415bf97004ee734b52f057a8d52c9afe79d10165602eb8a46cfc7e44cb38ca317aff77c78338487305c3e02ed00b03d946b27f7
6
+ metadata.gz: 2d7aa1ff0fc3e1ef9929b980a8e09bfb974f833ab9195d989901e33dab01646709199e1a4c1eef38cf069205ac84e74f8f275a8cf53ef0eebbd8b8ed391e2b39
7
+ data.tar.gz: 57999b89d6297aea450bd392603c13e51e7f806966dc95c2f8e5142f940f2255e1db6ff0f2a7d42da91ea3cd4cb4354849e5139dc82e6fbdd83fe65c5616774b
data/.travis.yml CHANGED
@@ -3,6 +3,8 @@ language: ruby
3
3
  rvm:
4
4
  - 1.9.3
5
5
  - 2.0.0
6
+ - 2.1.1
7
+ - 2.2.0
6
8
  - jruby-19mode
7
9
  notifications:
8
10
  email:
data/CHANGELOG.md ADDED
@@ -0,0 +1,7 @@
1
+ ## 2.5.0 (2015-01-28)
2
+
3
+ Features:
4
+
5
+ * add endpoints `/devices`, `/device/{token}`, `/channels`, and `/channels/{channel_name}`
6
+ * add ability to configure with multiple device tokens.
7
+ * update http client to use JSON parameter encoding.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- zero_push (2.4.0)
4
+ zero_push (2.5.0)
5
5
  faraday (~> 0.9.0)
6
6
  faraday_middleware (~> 0.9.0)
7
7
 
@@ -23,8 +23,11 @@ GEM
23
23
  minitest (~> 5.1)
24
24
  thread_safe (~> 0.1)
25
25
  tzinfo (~> 1.1)
26
+ addressable (2.3.6)
26
27
  builder (3.2.2)
27
28
  coderay (1.1.0)
29
+ crack (0.4.2)
30
+ safe_yaml (~> 1.0.0)
28
31
  erubis (2.7.0)
29
32
  faraday (0.9.0)
30
33
  multipart-post (>= 1.2, < 3)
@@ -53,11 +56,15 @@ GEM
53
56
  rake (>= 0.8.7)
54
57
  thor (>= 0.18.1, < 2.0)
55
58
  rake (10.3.2)
59
+ safe_yaml (1.0.4)
56
60
  slop (3.4.7)
57
61
  thor (0.19.1)
58
62
  thread_safe (0.3.4)
59
63
  tzinfo (1.2.1)
60
64
  thread_safe (~> 0.1)
65
+ webmock (1.20.4)
66
+ addressable (>= 2.3.6)
67
+ crack (>= 0.3.2)
61
68
 
62
69
  PLATFORMS
63
70
  ruby
@@ -69,4 +76,5 @@ DEPENDENCIES
69
76
  pry
70
77
  railties (~> 4.1.4)
71
78
  rake (~> 10.3.2)
79
+ webmock (~> 1.20.4)
72
80
  zero_push!
@@ -1,4 +1,6 @@
1
- Copyright (c) 2014 Symmetric Infinity LLC
1
+ ## License
2
+
3
+ Copyright (c) 2015 Symmetric Infinity LLC
2
4
 
3
5
  MIT License
4
6
 
@@ -20,3 +22,4 @@ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
22
  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
23
  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
24
  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
+
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  [![ZeroPush](https://raw.github.com/ZeroPush/zero_push/master/zeropush-header.png)](https://zeropush.com)
2
2
 
3
- Build Status: [![Build Status](https://travis-ci.org/ZeroPush/zero_push.png?branch=master)](https://travis-ci.org/ZeroPush/zero_push)
3
+ [![Build Status](http://img.shields.io/travis/ZeroPush/zero_push.svg)](https://travis-ci.org/ZeroPush/zero_push) [![Code Climate](https://codeclimate.com/github/ZeroPush/zero_push/badges/gpa.svg)](https://codeclimate.com/github/ZeroPush/zero_push) [![Gem Version](http://img.shields.io/gem/v/zero_push.svg)](http://rubygems.org/gems/zero_push)
4
4
 
5
5
  ## Installation
6
6
 
@@ -25,21 +25,50 @@ Generate the ZeroPush initializer if you are using Ruby on Rails.
25
25
 
26
26
  ### API Client
27
27
 
28
- The easiest way to use the API client is to set the auth token at the module level and call methods on the ZeroPush module.
28
+ The easiest way to use the API client is to set the server `auth_token` at the module level and call methods on the ZeroPush module. You can find the token on settings page for your app.
29
29
 
30
- ZeroPush.auth_token = 'your-auth-token'
31
- ZeroPush.verify_credentials
32
- => true
30
+ ```ruby
31
+ ZeroPush.auth_token = 'iosprod_your-server-token'
32
+ ZeroPush.verify_credentials
33
+ => true
34
+ ZeroPush.notify(device_tokens: ['abcdef'], alert: 'hello, world', badge: '+1')
35
+ ```
33
36
 
34
- If your application requires multiple API client instances, that can be achieved as well.
37
+ If your web application supports must support multiple mobile apps, you may configure it like this:
35
38
 
36
- client_1 = ZeroPush.client('auth-token-1')
37
- client_1.verify_credentials
38
- => true
39
+ ```ruby
40
+ if Rails.env == 'development' #or ENV['RACK_ENV']
41
+ ZeroPush.auth_tokens = {
42
+ apns: 'iosdev_XYZ',
43
+ gcm: 'gcmdev_ABC',
44
+ }
45
+ else
46
+ ZeroPush.auth_tokens = {
47
+ apns: 'iosprod_XYZ',
48
+ gcm: 'gcmprod_ABC',
49
+ }
50
+ end
51
+ ```
39
52
 
40
- client_2 = ZeroPush.client('auth-token-2')
41
- client_2.verify_credentials
42
- => true
53
+ You may then instantiate clients by calling the method that matches the auth token key:
54
+
55
+ ```ruby
56
+ ZeroPush.apns.broadcast( ... )
57
+ ZeroPush.gcm.broadcast( ... )
58
+ ```
59
+
60
+
61
+ Lastly, if you have many apps you may instantiate clients API Clients
62
+
63
+ ```ruby
64
+ client_1 = ZeroPush.client('iosprod_app-server-token-1')
65
+ client_1.broadcast(alert: 'hello, app1')
66
+
67
+ client_2 = ZeroPush.client('iosprod_app-server-token-2')
68
+ client_1.broadcast(alert: 'hello, app2')
69
+ ```
70
+
71
+ Methods supported by this gem and their parameters can be found in the [API Reference](https://zeropush.com/documentation/api_reference)
43
72
 
44
73
  For more documentation, check our [Getting Started Guide with ZeroPush](https://zeropush.com/documentation)
45
74
 
@@ -51,29 +80,3 @@ For more documentation, check our [Getting Started Guide with ZeroPush](https://
51
80
  1. Commit your changes (`git commit -am 'Add some feature'`)
52
81
  1. Push to the branch (`git push origin my-new-feature`)
53
82
  1. Create new Pull Request
54
-
55
- ## License
56
-
57
- Copyright (c) 2014 Symmetric Infinity LLC
58
-
59
- MIT License
60
-
61
- Permission is hereby granted, free of charge, to any person obtaining
62
- a copy of this software and associated documentation files (the
63
- "Software"), to deal in the Software without restriction, including
64
- without limitation the rights to use, copy, modify, merge, publish,
65
- distribute, sublicense, and/or sell copies of the Software, and to
66
- permit persons to whom the Software is furnished to do so, subject to
67
- the following conditions:
68
-
69
- The above copyright notice and this permission notice shall be
70
- included in all copies or substantial portions of the Software.
71
-
72
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
73
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
74
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
75
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
76
- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
77
- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
78
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
79
-
data/lib/zero_push.rb CHANGED
@@ -4,46 +4,40 @@ require 'faraday'
4
4
 
5
5
  module ZeroPush
6
6
  class << self
7
- attr_accessor :auth_token
7
+ extend Forwardable
8
+ attr_accessor :auth_token, :auth_tokens, :config
9
+
10
+ def_delegators :client,
11
+ :verify_credentials,
12
+ :notify,
13
+ :broadcast,
14
+ :subscribe,
15
+ :unsubscribe,
16
+ :register,
17
+ :unregister,
18
+ :set_badge,
19
+ :inactive_tokens,
20
+ :devices,
21
+ :set_device,
22
+ :update_device,
23
+ :channels,
24
+ :channel,
25
+ :delete_channel
8
26
 
9
- def verify_credentials
10
- client.verify_credentials
11
- end
12
-
13
- def notify(params)
14
- client.notify(params)
15
- end
16
-
17
- def broadcast(params)
18
- client.broadcast(params)
19
- end
20
-
21
- def subscribe(device_token, channel)
22
- client.subscribe(device_token, channel)
23
- end
24
-
25
- def unsubscribe(device_token, channel)
26
- client.unsubscribe(device_token, channel)
27
- end
28
-
29
- def register(device_token, channel=nil)
30
- client.register(device_token, channel)
31
- end
32
-
33
- def unregister(device_token)
34
- client.unregister(device_token)
35
- end
36
-
37
- def set_badge(device_token, badge)
38
- client.set_badge(device_token, badge)
27
+ def client(auth_token = self.auth_token)
28
+ ZeroPush::Client.new(auth_token)
39
29
  end
40
30
 
41
- def inactive_tokens
42
- client.inactive_tokens
31
+ def config
32
+ @config ||= {:http_adapter => Faraday.default_adapter, request_encoding: :json}
43
33
  end
44
34
 
45
- def client(auth_token = self.auth_token)
46
- ZeroPush::Client.new(auth_token)
35
+ def method_missing(method, *params, &block)
36
+ if auth_tokens.is_a?(Hash) && auth_tokens.keys.include?(method)
37
+ self.client(auth_tokens[method])
38
+ else
39
+ super
40
+ end
47
41
  end
48
42
  end
49
43
  end
@@ -1,3 +1,4 @@
1
+ require 'zero_push/compatibility'
1
2
  require 'faraday_middleware'
2
3
 
3
4
  module ZeroPush
@@ -8,13 +9,14 @@ module ZeroPush
8
9
 
9
10
  def initialize(auth_token)
10
11
  self.auth_token = auth_token
12
+ self.extend(Compatibility)
11
13
  end
12
14
 
13
15
  # verifies credentials
14
16
  #
15
17
  # @return [Boolean]
16
18
  def verify_credentials
17
- response = client.get('/verify_credentials')
19
+ response = http.get('/verify_credentials')
18
20
  response.status == 200
19
21
  end
20
22
 
@@ -22,20 +24,20 @@ module ZeroPush
22
24
  #
23
25
  # @param params [Hash]
24
26
  #
25
- # Ex.
27
+ # Example response
26
28
  # {"sent_count":10,"inactive_tokens":[],"unregistered_tokens":["abc"]}
27
29
  def notify(params)
28
- client.post('/notify', params)
30
+ http.post('/notify', params)
29
31
  end
30
32
 
31
33
  # Sends a notification to all of the devices registered with the ZeroPush backend
32
34
  #
33
35
  # @param params [Hash]
34
36
  #
35
- # Ex.
37
+ # Example response
36
38
  # {"sent_count":10}
37
39
  def broadcast(params)
38
- client.post('/broadcast', params)
40
+ http.post('/broadcast', params)
39
41
  end
40
42
 
41
43
  # Subscribes a device to a particular notification channel
@@ -43,10 +45,10 @@ module ZeroPush
43
45
  # @param device_token [String]
44
46
  # @param channel [String]
45
47
  #
46
- # Ex.
48
+ # Example response
47
49
  # {"device_token":"abc", "channels":["foo"]}
48
50
  def subscribe(device_token, channel)
49
- client.post("/subscribe/#{channel}", device_token:device_token)
51
+ http.post("/subscribe/#{channel}", device_token:device_token)
50
52
  end
51
53
 
52
54
  # Unsubscribes a device from a particular notification channel
@@ -54,22 +56,22 @@ module ZeroPush
54
56
  # @param device_token [String]
55
57
  # @param channel [String]
56
58
  #
57
- # Ex.
59
+ # Example response
58
60
  # {"device_token":"abc", "channels":[]}
59
61
  def unsubscribe(device_token, channel)
60
- client.delete("/subscribe/#{channel}", device_token:device_token)
62
+ http.delete("/subscribe/#{channel}", device_token:device_token)
61
63
  end
62
64
 
63
65
  # Registers a device token with the ZeroPush backend
64
66
  #
65
67
  # @param device_token
66
68
  #
67
- # Ex.
69
+ # Example response
68
70
  # {"message":"ok"}
69
71
  def register(device_token, channel=nil)
70
72
  params = {device_token: device_token}
71
73
  params.merge!(channel: channel) unless channel.nil?
72
- client.post('/register', params)
74
+ http.post('/register', params)
73
75
  end
74
76
 
75
77
  # Unregisters a device token that has previously been registered with
@@ -77,10 +79,10 @@ module ZeroPush
77
79
  #
78
80
  # @param device_token
79
81
  #
80
- # Ex.
82
+ # Example response
81
83
  # {"message":"ok"}
82
84
  def unregister(device_token)
83
- client.delete('/unregister', device_token: device_token)
85
+ http.delete('/unregister', device_token: device_token)
84
86
  end
85
87
 
86
88
  # Sets the badge for a particular device
@@ -88,15 +90,15 @@ module ZeroPush
88
90
  # @param device_token
89
91
  # @param badge
90
92
  #
91
- # Ex.
93
+ # Example response
92
94
  # {"message":"ok"}
93
95
  def set_badge(device_token, badge)
94
- client.post('/set_badge', device_token: device_token, badge: badge)
96
+ http.post('/set_badge', device_token: device_token, badge: badge)
95
97
  end
96
98
 
97
99
  # Returns a list of tokens that have been marked inactive
98
100
  #
99
- # Ex.
101
+ # Example response
100
102
  # [
101
103
  # {
102
104
  # "device_token":"238b8cb09011850cb4bd544dfe0c8f5eeab73d7eeaae9bdca59076db4ae49947",
@@ -107,19 +109,160 @@ module ZeroPush
107
109
  # "marked_inactive_at":"2013-07-17T01:27:50-04:00"
108
110
  # }
109
111
  # ]
110
- def inactive_tokens
111
- client.get('/inactive_tokens')
112
+ def inactive_tokens(params = {page:1})
113
+ http.get('/inactive_tokens', params)
112
114
  end
113
115
 
114
- # the HTTP client configured for API requests
116
+ # Returns a paginated list of devices
117
+ # https://zeropush.com/documentation/api_reference#devices_index
115
118
  #
116
- def client
119
+ # Example response
120
+ # [
121
+ # {
122
+ # "token": "1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcedf",
123
+ # "active": true,
124
+ # "marked_inactive_at": null,
125
+ # "badge": 1
126
+ # },
127
+ # {
128
+ # "token": "234567890abcdef1234567890abcdef1234567890abcdef1234567890abcedf0",
129
+ # "active": true,
130
+ # "marked_inactive_at": null,
131
+ # "badge": 2
132
+ # }
133
+ # ]
134
+ def devices(params = {page:1})
135
+ http.get('/devices', params)
136
+ end
137
+
138
+ # Return detailed information about a device
139
+ # https://zeropush.com/documentation/api_reference#devices_show
140
+ #
141
+ # Example response
142
+ # {
143
+ # "token": "1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcedf",
144
+ # "active": true,
145
+ # "marked_inactive_at": null,
146
+ # "badge": 1,
147
+ # "channels": [
148
+ # "testflight",
149
+ # "user@example.com"
150
+ # ]
151
+ # }
152
+ def device(token)
153
+ http.get("/devices/#{token}")
154
+ end
155
+
156
+ # Replace the channel subscriptions with a new set of channels. This will
157
+ # remove all previous subscriptions of the device. If you want to append a
158
+ # list of channels, use #update_device.
159
+ # https://zeropush.com/documentation/api_reference#devices_update_put
160
+ #
161
+ # @param token String token identifying the device
162
+ # @param channel_list String Comma separated list of channels
163
+ #
164
+ # Example Request
165
+ #
166
+ # ZeroPush.set_device(token, channel_list: 'player-1, game-256')
167
+ #
168
+ # Example Response
169
+ # {
170
+ # "token": "1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcedf",
171
+ # "active": true,
172
+ # "marked_inactive_at": null,
173
+ # "badge": 1,
174
+ # "channels": [
175
+ # "player-1",
176
+ # "game-256"
177
+ # ]
178
+ # }
179
+ def set_device(token, params)
180
+ http.put("/devices/#{token}", params)
181
+ end
182
+
183
+ # Append the channel subscriptions with a set of new channels. If you want
184
+ # to replace the list of channels, use #set_device.
185
+ # https://zeropush.com/documentation/api_reference#devices_update_patch
186
+ #
187
+ # @param token String token identifying the device
188
+ # @param channel_list String Comma separated list of channels
189
+ #
190
+ # Example Request
191
+ #
192
+ # ZeroPush.update_device(token, channel_list: 'player-1, game-256')
193
+ #
194
+ # Example Response
195
+ # {
196
+ # "token": "1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcedf",
197
+ # "active": true,
198
+ # "marked_inactive_at": null,
199
+ # "badge": 1,
200
+ # "channels": [
201
+ # "player-1",
202
+ # "game-256"
203
+ # ]
204
+ # }
205
+ def update_device(token, params)
206
+ http.patch("/devices/#{token}", params)
207
+ end
208
+
209
+ # Returns paginated list of channels
210
+ # https://zeropush.com/documentation/api_reference#channels_index
211
+ #
212
+ # Example Response:
213
+ # [
214
+ # "player-1",
215
+ # "player-2",
216
+ # "player-9",
217
+ # "game-256",
218
+ # "admins",
219
+ # "lobby"
220
+ # ]
221
+ def channels(params = {page:1})
222
+ http.get('/channels', params)
223
+ end
224
+
225
+ # Returns the list of device tokens for the given channel
226
+ # https://zeropush.com/documentation/api_reference#channels_show
227
+ #
228
+ # Example Response:
229
+ # {
230
+ # "channel": "player-1",
231
+ # "device_tokens": [
232
+ # "1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcedf"
233
+ # ]
234
+ # }
235
+ def channel(channel_name)
236
+ http.get("/channels/#{channel_name}")
237
+ end
238
+
239
+ # Deletes a channels and unsubscribes all of the devices from it.
240
+ # https://zeropush.com/documentation/api_reference#channels_destroy
241
+ #
242
+ # {
243
+ # "channel": "player-1",
244
+ # "device_tokens": [
245
+ # "1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcedf"
246
+ # ]
247
+ # }
248
+ def delete_channel(channel_name)
249
+ http.delete("/channels/#{channel_name}")
250
+ end
251
+
252
+ # Instantiate a new http client configured for making requests to the API
253
+ def http
117
254
  Faraday.new(url: URL) do |c|
118
255
  c.token_auth self.auth_token
119
- c.request :url_encoded # form-encode POST params
256
+ c.request http_config[:request_encoding]
120
257
  c.response :json, :content_type => /\bjson$/ # parse responses to JSON
121
- c.adapter Faraday.default_adapter
258
+ c.adapter http_config[:http_adapter]
122
259
  end
123
260
  end
261
+ alias client http
262
+
263
+ protected
264
+ def http_config
265
+ @http_config ||= ZeroPush.config.dup
266
+ end
124
267
  end
125
268
  end
@@ -0,0 +1,21 @@
1
+ module ZeroPush
2
+ module Compatibility
3
+ def warn_on_deprecated_parameters(params)
4
+ value = (params[:info] || params['info'] || params[:data] || params['data'])
5
+ if value.is_a?(String)
6
+ warn "[DEPRECATION] `info` or `data` encoded as a string will not be supported in version 3.0.0; Use a hash instead."
7
+ http_config[:request_encoding] = :url_encoded
8
+ end
9
+ end
10
+
11
+ def notify(params)
12
+ warn_on_deprecated_parameters(params)
13
+ super
14
+ end
15
+
16
+ def broadcast(params)
17
+ warn_on_deprecated_parameters(params)
18
+ super
19
+ end
20
+ end
21
+ end
@@ -1,3 +1,3 @@
1
1
  module ZeroPush
2
- VERSION = '2.4.1'
2
+ VERSION = '2.5.0'
3
3
  end
data/spec/spec_helper.rb CHANGED
@@ -1,8 +1,9 @@
1
1
  require 'minitest/autorun'
2
2
  require 'minitest/spec'
3
+ require 'webmock/minitest'
3
4
  require 'zero_push'
4
5
  require 'pry'
5
6
 
6
7
  ENV['AUTH_TOKEN'] ||= 'test-token'
7
8
 
8
- require 'fixtures/responses' if ENV['AUTH_TOKEN'] == 'test-token'
9
+ WebMock.enable!
@@ -1,18 +1,57 @@
1
1
  require 'spec_helper'
2
+ require 'json'
2
3
 
3
4
  describe ZeroPush::Client do
4
5
 
5
6
  let(:auth_token){ ENV['AUTH_TOKEN'] }
6
7
  let(:client){ ZeroPush.client(auth_token) }
7
8
  let(:device_token) { 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' }
9
+ before do
10
+ stub_request(:post, "https://api.zeropush.com/register").
11
+ with(body: '{"device_token":"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}', headers: {'Authorization'=>'Token token="test-token"', 'Content-Type'=>'application/json'}).
12
+ to_return(status: 200, body: '{"message":"ok"}', headers: {'Content-Type' => 'application/json'})
13
+
14
+ stub_request(:delete, "https://api.zeropush.com/unregister?device_token=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa").
15
+ with(headers: {'Authorization'=>"Token token=\"#{auth_token}\""}).
16
+ to_return(status: 200, body: '{"message":"ok"}', headers: {'Content-Type' => 'application/json'})
17
+ end
8
18
 
9
- describe '#verify_credentials' do
19
+ describe 'compatibility' do
20
+ it 'should use url_encoding if `info` or `data` params are strings' do
21
+ request = stub_request(:post, "https://api.zeropush.com/notify").
22
+ with(body: {"info" => "{\"a\":1}"}, headers: {'Content-Type'=>'application/x-www-form-urlencoded'}).to_return(status: 200)
23
+ client.notify(info: JSON.dump({a: 1}))
24
+ assert_request_requested request
25
+ end
26
+ it 'should use JSON encoding if `info` or `data` params are hashes' do
27
+ request = stub_request(:post, "https://api.zeropush.com/notify").
28
+ with(body: '{"info":{"a":1}}', headers: {'Content-Type' => 'application/json'}).to_return(status: 200)
29
+ client.notify(info: {a: 1})
30
+ assert_request_requested request
31
+ end
32
+ end
10
33
 
34
+ describe '#http' do
35
+ it 'should instantiate a default http client' do
36
+ client.http.must_be_instance_of Faraday::Connection
37
+ client.http.headers['Authorization'].must_equal 'Token token="test-token"'
38
+ end
39
+ end
40
+
41
+ describe '#verify_credentials' do
11
42
  it 'should verify credentials successfully' do
43
+ stub_request(:get, "https://api.zeropush.com/verify_credentials").
44
+ with(headers: {'Authorization'=>'Token token="test-token"'}).
45
+ to_return(status: 200, body: '{"message": "authenticated"}', headers: {'Content-Type' => 'application/json'})
46
+
12
47
  client.verify_credentials.must_equal true
13
48
  end
14
49
 
15
50
  it 'should fail to verify credentials' do
51
+ stub_request(:get, "https://api.zeropush.com/verify_credentials").
52
+ with(headers: {'Authorization'=>'Token token="not a valid token"'}).
53
+ to_return(status: 401, body: '{"error": "unauthorized"}', headers: {'Content-Type' => 'application/json'})
54
+
16
55
  client = ZeroPush.client('not a valid token')
17
56
  client.verify_credentials.must_equal false
18
57
  end
@@ -20,6 +59,9 @@ describe ZeroPush::Client do
20
59
 
21
60
  describe '#notify' do
22
61
  before do
62
+ stub_request(:post, "https://api.zeropush.com/notify").
63
+ with(body: '{"device_tokens":["aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"],"alert":"hi"}').
64
+ to_return(status: 200, body: '{"sent_count":1, "inactive_tokens":[], "unregistered_tokens":[]}', headers: {'Content-Type' => 'application/json'})
23
65
  client.register(device_token)
24
66
  end
25
67
 
@@ -40,7 +82,6 @@ describe ZeroPush::Client do
40
82
  end
41
83
 
42
84
  describe '#register' do
43
-
44
85
  describe 'without a channel parameter' do
45
86
  it 'should return a hash' do
46
87
  client.register(device_token).body.class.must_equal Hash
@@ -52,6 +93,12 @@ describe ZeroPush::Client do
52
93
  end
53
94
 
54
95
  describe 'with a channel parameter' do
96
+ before do
97
+ stub_request(:post, "https://api.zeropush.com/register").
98
+ with(body: '{"device_token":"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa","channel":"foo"}',
99
+ headers: {'Authorization'=>'Token token="test-token"', 'Content-Type'=>'application/json'}).
100
+ to_return(status: 200, body: '{"message":"ok"}', headers: {'Content-Type'=>'application/json'})
101
+ end
55
102
  it 'should return a hash' do
56
103
  client.register(device_token, 'foo').body.class.must_equal Hash
57
104
  end
@@ -68,6 +115,11 @@ describe ZeroPush::Client do
68
115
  end
69
116
 
70
117
  describe 'when the device has been registered' do
118
+ before do
119
+ stub_request(:delete, "https://api.zeropush.com/unregister?device_token=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa").
120
+ to_return(status: 200, body: '{"message":"ok"}', headers: {'Content-Type' => 'application/json'})
121
+ end
122
+
71
123
  it 'should return a hash' do
72
124
  client.unregister(device_token).body.class.must_equal Hash
73
125
  end
@@ -76,16 +128,13 @@ describe ZeroPush::Client do
76
128
  client.unregister(device_token).body['message'].must_equal 'ok'
77
129
  end
78
130
  end
79
-
80
131
  end
81
132
 
82
133
  describe '#subscribe' do
83
134
  before do
84
- client.register(device_token)
85
- end
86
-
87
- after do
88
- client.unregister(device_token)
135
+ stub_request(:post, "https://api.zeropush.com/subscribe/foo_channel").
136
+ with(body: '{"device_token":"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}').
137
+ to_return(status: 200, body: '{"device_token": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa","channels":["foo_channel"]}', headers: {'Content-Type' => 'application/json'})
89
138
  end
90
139
 
91
140
  let(:response){client.subscribe(device_token, 'foo_channel')}
@@ -102,12 +151,8 @@ describe ZeroPush::Client do
102
151
 
103
152
  describe '#unsubscribe' do
104
153
  before do
105
- client.register(device_token)
106
- client.subscribe(device_token, 'foo_channel')
107
- end
108
-
109
- after do
110
- client.unregister(device_token)
154
+ stub_request(:delete, "https://api.zeropush.com/subscribe/foo_channel?device_token=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa").
155
+ to_return(status: 200, body: '{"device_token": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa","channels":[]}', headers: {'Content-Type' => 'application/json'})
111
156
  end
112
157
 
113
158
  let(:response){client.unsubscribe(device_token, 'foo_channel')}
@@ -123,6 +168,11 @@ describe ZeroPush::Client do
123
168
  end
124
169
 
125
170
  describe '#broadcast' do
171
+ before do
172
+ stub_request(:post, "https://api.zeropush.com/broadcast").
173
+ with(body: '{"alert":"hi"}').
174
+ to_return(status: 200, body: '{"sent_count":10}', headers: {'Content-Type' => 'application/json'})
175
+ end
126
176
  let(:response){client.broadcast(alert:'hi')}
127
177
 
128
178
  it 'should return a hash' do
@@ -132,15 +182,20 @@ describe ZeroPush::Client do
132
182
  it 'should broadcast a notification to all the devices' do
133
183
  response.body['sent_count'].must_equal 10
134
184
  end
185
+
186
+ it 'should use json encoding for custom data' do
187
+ request = stub_request(:post, "https://api.zeropush.com/broadcast").
188
+ with(body: '{"data":{"alert":"hi","user_id":5}}', headers: {'Content-Type'=>'application/json'}).to_return(status: 200)
189
+ client.broadcast(data: {alert: "hi", user_id: 5})
190
+ assert_request_requested request
191
+ end
135
192
  end
136
193
 
137
194
  describe '#set_badge' do
138
195
  before do
139
- client.register(device_token)
140
- end
141
-
142
- after do
143
- client.unregister(device_token)
196
+ stub_request(:post, "https://api.zeropush.com/set_badge").
197
+ with(body: '{"device_token":"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa","badge":10}').
198
+ to_return(status: 200, body: '{"message":"ok"}', headers: {'Content-Type'=>'application/json'})
144
199
  end
145
200
 
146
201
  let(:response){client.set_badge(device_token, 10)}
@@ -157,6 +212,10 @@ describe ZeroPush::Client do
157
212
  describe '#inactive_tokens' do
158
213
 
159
214
  let(:response){client.inactive_tokens}
215
+ before do
216
+ stub_request(:get, "https://api.zeropush.com/inactive_tokens?page=1").
217
+ to_return(status: 200, body: '[{"device_token": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa","marked_inactive_at": "2013-03-11T16:25:14-04:00"}]', headers: {'Content-Type'=>'application/json'})
218
+ end
160
219
 
161
220
  it 'should return an array' do
162
221
  response.body.class.must_equal Array
@@ -166,4 +225,92 @@ describe ZeroPush::Client do
166
225
  response.body.count.must_equal 1
167
226
  end
168
227
  end
228
+
229
+ describe '#devices' do
230
+ let(:response){client.devices}
231
+ before do
232
+ stub_request(:get, "https://api.zeropush.com/devices?page=1").
233
+ to_return(status: 200, body: '[]', headers: {'Content-Type' => 'application/json', 'Link' => '<https://api.zeropush.com/devices?page=10&per_page=25>; rel="last",<https://api.zeropush.com/devices?page=2&per_page=25>; rel="next"'})
234
+ end
235
+ it 'should return an array' do
236
+ response.body.class.must_equal Array
237
+ end
238
+ it 'should have paginated results' do
239
+ response.headers["link"].must_equal '<https://api.zeropush.com/devices?page=10&per_page=25>; rel="last",<https://api.zeropush.com/devices?page=2&per_page=25>; rel="next"'
240
+ end
241
+ end
242
+
243
+ describe '#device' do
244
+ before do
245
+ stub_request(:get, "https://api.zeropush.com/devices/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa").
246
+ to_return(status: 200, body: '{
247
+ "token": "1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcedf",
248
+ "active": true,
249
+ "marked_inactive_at": null,
250
+ "badge": 1,
251
+ "channels": [
252
+ "testflight",
253
+ "user@example.com"
254
+ ]
255
+ }', headers: {'Content-Type'=>'application/json'})
256
+ end
257
+ let(:response){client.device('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')}
258
+
259
+ it 'should return a device hash' do
260
+ response.body.class.must_equal Hash
261
+ response.body.keys.must_equal %w[token active marked_inactive_at badge channels]
262
+ end
263
+ end
264
+
265
+ describe '#channels' do
266
+ before do
267
+ stub_request(:get, "https://api.zeropush.com/channels?page=1").
268
+ to_return(status: 200, body: '["player-1","foobar"]', headers: {'Content-Type'=>'application/json', 'Link' => '<https://api.zeropush.com/channels?page=10&per_page=25>; rel="last",<https://api.zeropush.com/channels?page=2&per_page=25>; rel="next"'})
269
+ end
270
+ let(:response){client.channels}
271
+ it 'should return an array' do
272
+ response.body.class.must_equal Array
273
+ end
274
+ it 'should have paginated results' do
275
+ response.headers["link"].must_equal '<https://api.zeropush.com/channels?page=10&per_page=25>; rel="last",<https://api.zeropush.com/channels?page=2&per_page=25>; rel="next"'
276
+ end
277
+ end
278
+
279
+ describe '#channel' do
280
+ before do
281
+ stub_request(:get, 'https://api.zeropush.com/channels/player-1').
282
+ to_return(status: 200, body:'{
283
+ "channel": "player-1",
284
+ "device_tokens": [
285
+ "1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcedf"
286
+ ]
287
+ }', headers: {'Content-Type' => 'application/json'})
288
+ end
289
+
290
+ let(:response) { client.channel('player-1') }
291
+
292
+ it 'should return a channel hash' do
293
+ response.body.class.must_equal Hash
294
+ response.body.keys.must_equal %w[channel device_tokens]
295
+ end
296
+ end
297
+
298
+ describe '#delete_channel' do
299
+ before do
300
+ stub_request(:delete, 'https://api.zeropush.com/channels/player-1').
301
+ to_return(status: 200, body:'{
302
+ "channel": "player-1",
303
+ "device_tokens": [
304
+ "1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcedf"
305
+ ]
306
+ }', headers: {'Content-Type' => 'application/json'})
307
+ end
308
+
309
+ let(:response) { client.delete_channel('player-1') }
310
+
311
+ it 'should return a channel hash' do
312
+ response.body.class.must_equal Hash
313
+ response.body.keys.must_equal %w[channel device_tokens]
314
+ end
315
+ end
169
316
  end
@@ -5,6 +5,17 @@ describe ZeroPush do
5
5
  ZeroPush.auth_token = ENV['AUTH_TOKEN'] || 'test_token'
6
6
  end
7
7
 
8
+ describe 'using different auth_tokens' do
9
+ it 'should accept a hash of tokens' do
10
+ ZeroPush.auth_tokens = {
11
+ apns: 'test-apns-token',
12
+ gcm: 'test-gcm-token'
13
+ }
14
+ ZeroPush.apns.auth_token.must_equal 'test-apns-token'
15
+ ZeroPush.gcm.auth_token.must_equal 'test-gcm-token'
16
+ end
17
+ end
18
+
8
19
  describe '.client' do
9
20
  it 'should return a client instance' do
10
21
  ZeroPush.client.class.must_equal ZeroPush::Client
data/zero_push.gemspec CHANGED
@@ -28,4 +28,5 @@ Gem::Specification.new do |gem|
28
28
  gem.add_development_dependency 'rake', '~> 10.3.2'
29
29
  gem.add_development_dependency 'railties', '~> 4.1.4'
30
30
  gem.add_development_dependency 'pry'
31
+ gem.add_development_dependency 'webmock', '~> 1.20.4'
31
32
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zero_push
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.4.1
4
+ version: 2.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stefan Natchev
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-07-08 00:00:00.000000000 Z
12
+ date: 2015-01-31 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: faraday
@@ -123,6 +123,20 @@ dependencies:
123
123
  - - ">="
124
124
  - !ruby/object:Gem::Version
125
125
  version: '0'
126
+ - !ruby/object:Gem::Dependency
127
+ name: webmock
128
+ requirement: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - "~>"
131
+ - !ruby/object:Gem::Version
132
+ version: 1.20.4
133
+ type: :development
134
+ prerelease: false
135
+ version_requirements: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - "~>"
138
+ - !ruby/object:Gem::Version
139
+ version: 1.20.4
126
140
  description: ZeroPush is a simple service for sending iOS push notifications. (http://zeropush.com)
127
141
  email:
128
142
  - stefan.natchev@gmail.com
@@ -133,17 +147,18 @@ extra_rdoc_files: []
133
147
  files:
134
148
  - ".gitignore"
135
149
  - ".travis.yml"
150
+ - CHANGELOG.md
136
151
  - Gemfile
137
152
  - Gemfile.lock
138
- - LICENSE
153
+ - LICENSE.md
139
154
  - README.md
140
155
  - Rakefile
141
156
  - lib/generators/zero_push/install_generator.rb
142
157
  - lib/generators/zero_push/templates/zero_push.rb
143
158
  - lib/zero_push.rb
144
159
  - lib/zero_push/client.rb
160
+ - lib/zero_push/compatibility.rb
145
161
  - lib/zero_push/version.rb
146
- - spec/fixtures/responses.rb
147
162
  - spec/generator_spec.rb
148
163
  - spec/spec_helper.rb
149
164
  - spec/zero_push_client_spec.rb
@@ -170,13 +185,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
170
185
  version: '0'
171
186
  requirements: []
172
187
  rubyforge_project:
173
- rubygems_version: 2.2.2
188
+ rubygems_version: 2.4.5
174
189
  signing_key:
175
190
  specification_version: 4
176
191
  summary: A gem for interacting with the ZeroPush API. (http://zeropush.com)
177
192
  test_files:
178
- - spec/fixtures/responses.rb
179
193
  - spec/generator_spec.rb
180
194
  - spec/spec_helper.rb
181
195
  - spec/zero_push_client_spec.rb
182
196
  - spec/zero_push_spec.rb
197
+ has_rdoc:
@@ -1,25 +0,0 @@
1
- class ZeroPush::Client
2
- def client
3
- Faraday.new do |builder|
4
- builder.token_auth self.auth_token
5
- builder.response :json, :content_type => /\bjson$/ # parse responses to JSON
6
- builder.adapter :test do |stub|
7
- stub.get('/verify_credentials') do |env|
8
- if env.request_headers['Authorization'] == "Token token=\"not a valid token\""
9
- [401, {'Content-Type' => 'application/json'}, '{"error": "unauthorized"}']
10
- else
11
- [200, {'Content-Type' => 'application/json'}, '{"message": "authenticated"}']
12
- end
13
- end
14
- stub.post('/register') { [200, {'Content-Type' => 'application/json'}, '{"message":"ok"}'] }
15
- stub.delete('/unregister') { [200, {'Content-Type' => 'application/json'}, '{"message":"ok"}'] }
16
- stub.post('/notify') { [200, {'Content-Type' => 'application/json'}, '{"sent_count":1, "inactive_tokens":[]}'] }
17
- stub.post('/broadcast') { [200, {'Content-Type' => 'application/json'}, '{"sent_count":10}'] }
18
- stub.post('/subscribe/foo_channel') { [200, {'Content-Type' => 'application/json'}, '{"device_token": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa","channels":["foo_channel"]}'] }
19
- stub.delete('/subscribe/foo_channel') { [200, {'Content-Type' => 'application/json'}, '{"device_token": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa","channels":[]}'] }
20
- stub.post('/set_badge') { [200, {'Content-Type' => 'application/json'}, '{"message":"ok"}'] }
21
- stub.get('/inactive_tokens') { [200, {'Content-Type' => 'application/json'}, '[{"device_token": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa","marked_inactive_at": "2013-03-11T16:25:14-04:00"}]'] }
22
- end
23
- end
24
- end
25
- end