pusher 0.7.1 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +16 -4
- data/lib/pusher.rb +1 -0
- data/lib/pusher/channel.rb +13 -4
- data/pusher.gemspec +1 -1
- data/spec/channel_spec.rb +25 -26
- metadata +3 -3
data/README.md
CHANGED
@@ -10,13 +10,25 @@ After registering at <http://pusherapp.com> configure your app with the security
|
|
10
10
|
Pusher.key = 'your-pusher-key'
|
11
11
|
Pusher.secret = 'your-pusher-secret'
|
12
12
|
|
13
|
-
Trigger an event
|
13
|
+
Trigger an event
|
14
14
|
|
15
|
-
Pusher['a_channel'].trigger('an_event', {:some => 'data'})
|
15
|
+
Pusher['a_channel'].trigger!('an_event', {:some => 'data'})
|
16
|
+
|
17
|
+
Handle errors by rescuing `Pusher::Error` (all Pusher errors are descendants of this error)
|
18
|
+
|
19
|
+
begin
|
20
|
+
Pusher['a_channel'].trigger!('an_event', {:some => 'data'})
|
21
|
+
rescue Pusher::Error => e
|
22
|
+
# (Pusher::AuthenticationError, Pusher::HTTPError, or Pusher::Error)
|
23
|
+
end
|
16
24
|
|
17
|
-
Optionally a socket id may be provided. This will
|
25
|
+
Optionally a socket id may be provided. This will exclude the event from being triggered on this socket id (see <http://pusherapp.com/docs/duplicates> for more info).
|
18
26
|
|
19
|
-
Pusher['a_channel'].trigger('an_event', {:some => 'data'}, socket_id)
|
27
|
+
Pusher['a_channel'].trigger!('an_event', {:some => 'data'}, socket_id)
|
28
|
+
|
29
|
+
If you don't need to handle failure cases, then you can simply use the `trigger` method, which will rescue and log any errors for you
|
30
|
+
|
31
|
+
Pusher['a_channel'].trigger('an_event', {:some => 'data'})
|
20
32
|
|
21
33
|
Logging
|
22
34
|
-------
|
data/lib/pusher.rb
CHANGED
data/lib/pusher/channel.rb
CHANGED
@@ -85,10 +85,19 @@ module Pusher
|
|
85
85
|
|
86
86
|
request = Pusher::Request.new(@uri, event_name, data, socket_id)
|
87
87
|
|
88
|
-
|
89
|
-
request.
|
88
|
+
begin
|
89
|
+
response = @http_sync.post("#{@uri.path}?#{request.query.to_params}",
|
90
|
+
request.body, { 'Content-Type'=> 'application/json' })
|
91
|
+
rescue Errno::EINVAL, Errno::ECONNRESET, Errno::ECONNREFUSED,
|
92
|
+
Timeout::Error, EOFError,
|
93
|
+
Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError,
|
94
|
+
Net::ProtocolError => e
|
95
|
+
error = Pusher::HTTPError.new("#{e.message} (#{e.class})")
|
96
|
+
error.original_error = e
|
97
|
+
raise error
|
98
|
+
end
|
90
99
|
|
91
|
-
handle_response(response.code.to_i, response.body.chomp)
|
100
|
+
return handle_response(response.code.to_i, response.body.chomp)
|
92
101
|
end
|
93
102
|
|
94
103
|
# Trigger event, catching and logging any errors.
|
@@ -98,7 +107,7 @@ module Pusher
|
|
98
107
|
#
|
99
108
|
def trigger(event_name, data, socket_id = nil)
|
100
109
|
trigger!(event_name, data, socket_id)
|
101
|
-
rescue
|
110
|
+
rescue Pusher::Error => e
|
102
111
|
Pusher.logger.error("#{e.message} (#{e.class})")
|
103
112
|
Pusher.logger.debug(e.backtrace.join("\n"))
|
104
113
|
end
|
data/pusher.gemspec
CHANGED
data/spec/channel_spec.rb
CHANGED
@@ -8,6 +8,11 @@ describe Pusher::Channel do
|
|
8
8
|
Pusher.host = 'api.pusherapp.com'
|
9
9
|
Pusher.port = 80
|
10
10
|
Pusher.encrypted = false
|
11
|
+
|
12
|
+
WebMock.reset!
|
13
|
+
WebMock.disable_net_connect!
|
14
|
+
|
15
|
+
@pusher_url_regexp = %r{/apps/20/channels/test_channel/events}
|
11
16
|
end
|
12
17
|
|
13
18
|
after do
|
@@ -18,10 +23,8 @@ describe Pusher::Channel do
|
|
18
23
|
|
19
24
|
describe 'trigger!' do
|
20
25
|
before :each do
|
21
|
-
WebMock.
|
22
|
-
|
23
|
-
:post, %r{/apps/20/channels/test_channel/events}
|
24
|
-
).to_return(:status => 202)
|
26
|
+
WebMock.stub_request(:post, @pusher_url_regexp).
|
27
|
+
to_return(:status => 202)
|
25
28
|
@channel = Pusher['test_channel']
|
26
29
|
end
|
27
30
|
|
@@ -71,13 +74,20 @@ describe Pusher::Channel do
|
|
71
74
|
}.should raise_error(JSON::GeneratorError)
|
72
75
|
end
|
73
76
|
|
74
|
-
it "should
|
77
|
+
it "should catch all Net::HTTP exceptions and raise a Pusher::HTTPError, exposing the original error if required" do
|
75
78
|
WebMock.stub_request(
|
76
79
|
:post, %r{/apps/20/channels/test_channel/events}
|
77
|
-
).to_raise(
|
78
|
-
|
80
|
+
).to_raise(Timeout::Error)
|
81
|
+
|
82
|
+
error_raised = nil
|
83
|
+
begin
|
79
84
|
Pusher['test_channel'].trigger!('new_event', 'Some data')
|
80
|
-
|
85
|
+
rescue => e
|
86
|
+
error_raised = e
|
87
|
+
end
|
88
|
+
error_raised.class.should == Pusher::HTTPError
|
89
|
+
error_raised.message.should == 'Exception from WebMock (Timeout::Error)'
|
90
|
+
error_raised.original_error.class.should == Timeout::Error
|
81
91
|
end
|
82
92
|
|
83
93
|
it "should raise AuthenticationError if pusher returns 401" do
|
@@ -110,34 +120,23 @@ describe Pusher::Channel do
|
|
110
120
|
end
|
111
121
|
|
112
122
|
describe 'trigger' do
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
end
|
117
|
-
|
118
|
-
it "should log failure if exception raised" do
|
119
|
-
@http.should_receive(:post).and_raise("Fail")
|
120
|
-
Pusher.logger.should_receive(:error).with("Fail (RuntimeError)")
|
123
|
+
it "should log failure if error raised in Net::HTTP call" do
|
124
|
+
stub_request(:post, @pusher_url_regexp).to_raise(Net::HTTPBadResponse)
|
125
|
+
Pusher.logger.should_receive(:error).with("Exception from WebMock (Net::HTTPBadResponse) (Pusher::HTTPError)")
|
121
126
|
Pusher.logger.should_receive(:debug) #backtrace
|
122
127
|
Pusher::Channel.new(Pusher.url, 'test_channel').trigger('new_event', 'Some data')
|
123
128
|
end
|
124
129
|
|
125
|
-
it "should log failure if
|
126
|
-
|
127
|
-
|
130
|
+
it "should log failure if Pusher returns an error response" do
|
131
|
+
stub_request(:post, @pusher_url_regexp).to_return(:status => 401)
|
132
|
+
# @http.should_receive(:post).and_raise(Net::HTTPBadResponse)
|
133
|
+
Pusher.logger.should_receive(:error).with(" (Pusher::AuthenticationError)")
|
128
134
|
Pusher.logger.should_receive(:debug) #backtrace
|
129
135
|
Pusher::Channel.new(Pusher.url, 'test_channel').trigger('new_event', 'Some data')
|
130
136
|
end
|
131
137
|
end
|
132
138
|
|
133
139
|
describe "trigger_async" do
|
134
|
-
before :each do
|
135
|
-
WebMock.reset!
|
136
|
-
WebMock.disable_net_connect!
|
137
|
-
|
138
|
-
@pusher_url_regexp = %r{/apps/20/channels/test_channel/events}
|
139
|
-
end
|
140
|
-
|
141
140
|
it "should by default POST to http api" do
|
142
141
|
EM.run {
|
143
142
|
stub_request(:post, @pusher_url_regexp).to_return(:status => 202)
|