zero_push 1.2.0 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile.lock CHANGED
@@ -1,8 +1,9 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- zero_push (1.2.0)
4
+ zero_push (1.3.0)
5
5
  faraday (~> 0.8.5)
6
+ faraday_middleware (~> 0.9.0)
6
7
 
7
8
  GEM
8
9
  remote: https://rubygems.org/
@@ -25,8 +26,10 @@ GEM
25
26
  multi_json (~> 1.0)
26
27
  builder (3.0.4)
27
28
  erubis (2.7.0)
28
- faraday (0.8.7)
29
- multipart-post (~> 1.1)
29
+ faraday (0.8.8)
30
+ multipart-post (~> 1.2.0)
31
+ faraday_middleware (0.9.0)
32
+ faraday (>= 0.7.4, < 0.9)
30
33
  hike (1.2.1)
31
34
  i18n (0.6.1)
32
35
  journey (1.0.4)
@@ -1,57 +1,39 @@
1
+ require 'faraday_middleware'
2
+
1
3
  module ZeroPush
2
- URL = "https://api.zeropush.com"
4
+ class Client
3
5
 
4
- class << self
5
- attr_accessor :auth_token
6
+ def initialize(auth_token)
7
+ @faraday = Faraday.new(url: ZeroPush::URL) do |c|
8
+ c.token_auth auth_token # Set the Authorization header
9
+ c.request :url_encoded # form-encode POST params
10
+ c.response :json, :content_type => /\bjson$/ # parse responses to JSON
11
+ c.adapter Faraday.default_adapter # Net::HTTP
12
+ end
13
+ end
6
14
 
7
- # verifies credentials
8
- #
9
- # @return [Boolean]
10
15
  def verify_credentials
11
- response = client.get('/verify_credentials')
16
+ response = faraday.get('/verify_credentials')
12
17
  response.status == 200
13
18
  end
14
19
 
15
- # Sends a notification to the list of devices
16
- #
17
- # @param params [Hash]
18
- # @return response
19
20
  def notify(params)
20
- client.post('/notify', params)
21
+ faraday.post('/notify', params)
21
22
  end
22
23
 
23
- # Registers a device token with the ZeroPush backend
24
- #
25
- # @param device_token
26
- # @return response
27
24
  def register(device_token)
28
- client.post('/register', device_token: device_token)
25
+ faraday.post('/register', device_token: device_token)
29
26
  end
30
27
 
31
- # Sets the badge for a particular device
32
- #
33
- # @param device_token
34
- # @param badge
35
- # @return response
36
28
  def set_badge(device_token, badge)
37
- client.post('/set_badge', device_token: device_token, badge: badge)
29
+ faraday.post('/set_badge', device_token: device_token, badge: badge)
38
30
  end
39
31
 
40
- # Returns a list of tokens that have been marked inactive
41
- #
42
- # @returns array
43
32
  def inactive_tokens
44
- client.get('/inactive_tokens')
33
+ faraday.get('/inactive_tokens')
45
34
  end
46
35
 
47
- # the HTTP client configured for API requests
48
- #
49
- def client
50
- Faraday.new(url: URL) do |c|
51
- c.token_auth self.auth_token
52
- c.request :url_encoded # form-encode POST params
53
- c.adapter Faraday.default_adapter # Net::HTTP
54
- end
55
- end
36
+ private
37
+ attr_reader :faraday
56
38
  end
57
39
  end
@@ -1,3 +1,3 @@
1
1
  module ZeroPush
2
- VERSION = "1.2.0"
2
+ VERSION = "1.3.0"
3
3
  end
data/lib/zero_push.rb CHANGED
@@ -1,3 +1,61 @@
1
- require 'zero_push/version'
2
- require 'zero_push/client'
3
1
  require 'faraday'
2
+ require 'zero_push/client'
3
+ require 'zero_push/version'
4
+
5
+ module ZeroPush
6
+ URL = "https://api.zeropush.com"
7
+
8
+ class << self
9
+ attr_accessor :auth_token
10
+
11
+ # verifies credentials
12
+ #
13
+ # @return [Boolean]
14
+ def verify_credentials
15
+ response = client.get('/verify_credentials')
16
+ response.status == 200
17
+ end
18
+
19
+ # Sends a notification to the list of devices
20
+ #
21
+ # @param params [Hash]
22
+ # @return response
23
+ def notify(params)
24
+ client.post('/notify', params)
25
+ end
26
+
27
+ # Registers a device token with the ZeroPush backend
28
+ #
29
+ # @param device_token
30
+ # @return response
31
+ def register(device_token)
32
+ client.post('/register', device_token: device_token)
33
+ end
34
+
35
+ # Sets the badge for a particular device
36
+ #
37
+ # @param device_token
38
+ # @param badge
39
+ # @return response
40
+ def set_badge(device_token, badge)
41
+ client.post('/set_badge', device_token: device_token, badge: badge)
42
+ end
43
+
44
+ # Returns a list of tokens that have been marked inactive
45
+ #
46
+ # @returns array
47
+ def inactive_tokens
48
+ client.get('/inactive_tokens')
49
+ end
50
+
51
+ # the HTTP client configured for API requests
52
+ #
53
+ def client
54
+ Faraday.new(url: URL) do |c|
55
+ c.token_auth self.auth_token
56
+ c.request :url_encoded # form-encode POST params
57
+ c.adapter Faraday.default_adapter # Net::HTTP
58
+ end
59
+ end
60
+ end
61
+ end
@@ -10,7 +10,7 @@ http_interactions:
10
10
  Authorization:
11
11
  - Token token="test_token"
12
12
  User-Agent:
13
- - Faraday v0.8.7
13
+ - Faraday v0.8.8
14
14
  response:
15
15
  status:
16
16
  code: 200
@@ -10,7 +10,7 @@ http_interactions:
10
10
  Authorization:
11
11
  - Token token="test_token"
12
12
  User-Agent:
13
- - Faraday v0.8.7
13
+ - Faraday v0.8.8
14
14
  Content-Type:
15
15
  - application/x-www-form-urlencoded
16
16
  response:
@@ -10,7 +10,7 @@ http_interactions:
10
10
  Authorization:
11
11
  - Token token="test_token"
12
12
  User-Agent:
13
- - Faraday v0.8.7
13
+ - Faraday v0.8.8
14
14
  Content-Type:
15
15
  - application/x-www-form-urlencoded
16
16
  response:
@@ -10,7 +10,7 @@ http_interactions:
10
10
  Authorization:
11
11
  - Token token="test_token"
12
12
  User-Agent:
13
- - Faraday v0.8.7
13
+ - Faraday v0.8.8
14
14
  Content-Type:
15
15
  - application/x-www-form-urlencoded
16
16
  response:
@@ -10,7 +10,7 @@ http_interactions:
10
10
  Authorization:
11
11
  - Token token="test_token"
12
12
  User-Agent:
13
- - Faraday v0.8.7
13
+ - Faraday v0.8.8
14
14
  response:
15
15
  status:
16
16
  code: 200
@@ -74,7 +74,7 @@ http_interactions:
74
74
  Authorization:
75
75
  - Token token="not a valid token"
76
76
  User-Agent:
77
- - Faraday v0.8.7
77
+ - Faraday v0.8.8
78
78
  response:
79
79
  status:
80
80
  code: 401
@@ -0,0 +1,130 @@
1
+ require 'spec_helper'
2
+
3
+ describe ZeroPush::Client do
4
+
5
+ let(:auth_token){ENV["AUTH_TOKEN"] || "test_token"}
6
+ let(:verified_client){ZeroPush::Client.new(auth_token)}
7
+ let(:unverified_client){ZeroPush::Client.new("not a valid token")}
8
+
9
+ describe "#verify_credentials" do
10
+ before do
11
+ VCR.insert_cassette "verify_credentials"
12
+ end
13
+
14
+ after do
15
+ VCR.eject_cassette
16
+ end
17
+
18
+ describe "for a verified client" do
19
+ let(:response){verified_client.verify_credentials}
20
+
21
+ it "should verify credentials successfully" do
22
+ response.must_equal true
23
+ end
24
+ end
25
+
26
+ describe "for an unverified client" do
27
+ let(:response){unverified_client.verify_credentials}
28
+
29
+ it "should fail to verify credentials" do
30
+ response.must_equal false
31
+ end
32
+ end
33
+ end
34
+
35
+ describe "#notify" do
36
+ before do
37
+ VCR.insert_cassette "notify"
38
+ end
39
+
40
+ after do
41
+ VCR.eject_cassette
42
+ end
43
+
44
+ let(:response){verified_client.notify(device_tokens: ['abc'], alert: 'hi')}
45
+
46
+ it "should return a Faraday::Response" do
47
+ response.class.must_equal Faraday::Response
48
+ end
49
+
50
+ it "should return a Hash for a response body" do
51
+ response.body.class.must_equal Hash
52
+ end
53
+
54
+ it "should be successful" do
55
+ response.status.must_equal 200
56
+ end
57
+ end
58
+
59
+ describe "#register" do
60
+ before do
61
+ VCR.insert_cassette "register"
62
+ end
63
+
64
+ after do
65
+ VCR.eject_cassette
66
+ end
67
+
68
+ let(:response){verified_client.register('abc')}
69
+
70
+ it "should return a Faraday::Response" do
71
+ response.class.must_equal Faraday::Response
72
+ end
73
+
74
+ it "should return a Hash for a response body" do
75
+ response.body.class.must_equal Hash
76
+ end
77
+
78
+ it "should register the device" do
79
+ response.status.must_equal 200
80
+ end
81
+ end
82
+
83
+ describe "#set_badge" do
84
+ before do
85
+ VCR.insert_cassette "set_badge"
86
+ end
87
+
88
+ after do
89
+ VCR.eject_cassette
90
+ end
91
+
92
+ let(:response){verified_client.set_badge('abc', 10)}
93
+
94
+ it "should return a Faraday::Response" do
95
+ response.class.must_equal Faraday::Response
96
+ end
97
+
98
+ it "should return a Hash for a response body" do
99
+ response.body.class.must_equal Hash
100
+ end
101
+
102
+ it "should set the device's badge" do
103
+ response.status.must_equal 200
104
+ end
105
+ end
106
+
107
+ describe "#inactive_tokens" do
108
+ before do
109
+ VCR.insert_cassette "inactive_tokens"
110
+ end
111
+
112
+ after do
113
+ VCR.eject_cassette
114
+ end
115
+
116
+ let(:response){verified_client.inactive_tokens}
117
+
118
+ it "should return a Faraday::Response" do
119
+ response.class.must_equal Faraday::Response
120
+ end
121
+
122
+ it "should return an Array for a response body" do
123
+ response.body.class.must_equal Array
124
+ end
125
+
126
+ it "should get a list of inactive tokens" do
127
+ response.status.must_equal 200
128
+ end
129
+ end
130
+ end
@@ -1,11 +1,21 @@
1
1
  require 'spec_helper'
2
+ require 'json'
2
3
 
3
4
  describe ZeroPush do
4
5
  before do
5
6
  ZeroPush.auth_token = ENV["AUTH_TOKEN"] || "test_token"
6
7
  end
7
8
 
8
- describe "/verify_credentials" do
9
+ describe ".client" do
10
+
11
+ let(:client){ZeroPush.client}
12
+
13
+ it "should return a Faraday client" do
14
+ client.class.must_equal Faraday::Connection
15
+ end
16
+ end
17
+
18
+ describe ".verify_credentials" do
9
19
  before do
10
20
  VCR.insert_cassette "verify_credentials"
11
21
  end
@@ -14,17 +24,19 @@ describe ZeroPush do
14
24
  VCR.eject_cassette
15
25
  end
16
26
 
27
+ let(:response){ZeroPush.verify_credentials}
28
+
17
29
  it "should verify credentials successfully" do
18
- ZeroPush.verify_credentials.must_equal true
30
+ response.must_equal true
19
31
  end
20
32
 
21
33
  it "should fail to verify credentials" do
22
34
  ZeroPush.auth_token = "not a valid token"
23
- ZeroPush.verify_credentials.must_equal false
35
+ response.must_equal false
24
36
  end
25
37
  end
26
38
 
27
- describe "/notify" do
39
+ describe ".notify" do
28
40
  before do
29
41
  VCR.insert_cassette "notify"
30
42
  end
@@ -33,13 +45,22 @@ describe ZeroPush do
33
45
  VCR.eject_cassette
34
46
  end
35
47
 
36
- it "should construct the request" do
37
- response = ZeroPush.notify(device_tokens: ['abc'], alert: 'hi')
48
+ let(:response){ZeroPush.notify(device_tokens: ['abc'], alert: 'hi')}
49
+
50
+ it "should return a Faraday::Response" do
51
+ response.class.must_equal Faraday::Response
52
+ end
53
+
54
+ it "should return a response with a body that can be parsed into a Hash" do
55
+ JSON.parse(response.body).class.must_equal Hash
56
+ end
57
+
58
+ it "should be successful" do
38
59
  response.status.must_equal 200
39
60
  end
40
61
  end
41
62
 
42
- describe "/register" do
63
+ describe ".register" do
43
64
  before do
44
65
  VCR.insert_cassette "register"
45
66
  end
@@ -48,13 +69,22 @@ describe ZeroPush do
48
69
  VCR.eject_cassette
49
70
  end
50
71
 
72
+ let(:response){ZeroPush.register('abc')}
73
+
74
+ it "should return a Faraday::Response" do
75
+ response.class.must_equal Faraday::Response
76
+ end
77
+
78
+ it "should return a response with a body that can be parsed into a Hash" do
79
+ JSON.parse(response.body).class.must_equal Hash
80
+ end
81
+
51
82
  it "should register the device" do
52
- response = ZeroPush.register('abc')
53
83
  response.status.must_equal 200
54
84
  end
55
85
  end
56
86
 
57
- describe "/set_badge" do
87
+ describe ".set_badge" do
58
88
  before do
59
89
  VCR.insert_cassette "set_badge"
60
90
  end
@@ -63,13 +93,22 @@ describe ZeroPush do
63
93
  VCR.eject_cassette
64
94
  end
65
95
 
96
+ let(:response){ZeroPush.set_badge('abc', 10)}
97
+
98
+ it "should return a Faraday::Response" do
99
+ response.class.must_equal Faraday::Response
100
+ end
101
+
102
+ it "should return a response with a body that can be parsed into a Hash" do
103
+ JSON.parse(response.body).class.must_equal Hash
104
+ end
105
+
66
106
  it "should set the device's badge" do
67
- response = ZeroPush.set_badge('abc', 10)
68
107
  response.status.must_equal 200
69
108
  end
70
109
  end
71
110
 
72
- describe "/inactive_tokens" do
111
+ describe ".inactive_tokens" do
73
112
  before do
74
113
  VCR.insert_cassette "inactive_tokens"
75
114
  end
@@ -78,8 +117,17 @@ describe ZeroPush do
78
117
  VCR.eject_cassette
79
118
  end
80
119
 
120
+ let(:response){ZeroPush.inactive_tokens}
121
+
122
+ it "should return a Faraday::Response" do
123
+ response.class.must_equal Faraday::Response
124
+ end
125
+
126
+ it "should return a response with a body that can be parsed into an Array" do
127
+ JSON.parse(response.body).class.must_equal Array
128
+ end
129
+
81
130
  it "should get a list of inactive tokens" do
82
- response = ZeroPush.inactive_tokens
83
131
  response.status.must_equal 200
84
132
  end
85
133
  end
data/zero_push.gemspec CHANGED
@@ -19,6 +19,7 @@ Gem::Specification.new do |gem|
19
19
  gem.required_ruby_version = '>= 1.9'
20
20
 
21
21
  gem.add_dependency "faraday", "~> 0.8.5"
22
+ gem.add_dependency "faraday_middleware", "~> 0.9.0"
22
23
 
23
24
  gem.add_development_dependency 'actionpack', '~> 3.2.11'
24
25
  gem.add_development_dependency 'activesupport', '~> 3.2.11'
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: 1.2.0
4
+ version: 1.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2013-08-09 00:00:00.000000000 Z
13
+ date: 2013-08-20 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: faraday
@@ -28,6 +28,22 @@ dependencies:
28
28
  - - ~>
29
29
  - !ruby/object:Gem::Version
30
30
  version: 0.8.5
31
+ - !ruby/object:Gem::Dependency
32
+ name: faraday_middleware
33
+ requirement: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ~>
37
+ - !ruby/object:Gem::Version
38
+ version: 0.9.0
39
+ type: :runtime
40
+ prerelease: false
41
+ version_requirements: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ~>
45
+ - !ruby/object:Gem::Version
46
+ version: 0.9.0
31
47
  - !ruby/object:Gem::Dependency
32
48
  name: actionpack
33
49
  requirement: !ruby/object:Gem::Requirement
@@ -167,6 +183,7 @@ files:
167
183
  - spec/fixtures/verify_credentials.yml
168
184
  - spec/generator_spec.rb
169
185
  - spec/spec_helper.rb
186
+ - spec/zero_push_client_spec.rb
170
187
  - spec/zero_push_spec.rb
171
188
  - zero_push.gemspec
172
189
  - zeropush-header.png
@@ -202,5 +219,6 @@ test_files:
202
219
  - spec/fixtures/verify_credentials.yml
203
220
  - spec/generator_spec.rb
204
221
  - spec/spec_helper.rb
222
+ - spec/zero_push_client_spec.rb
205
223
  - spec/zero_push_spec.rb
206
224
  has_rdoc: