pusher 0.7.1 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
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. Channel and event names may only contain alphanumeric characters, '-' and '_'.
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 prevent the event from being triggered on this specific socket id (see <http://pusherapp.com/docs/duplicates> for more info).
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
@@ -16,6 +16,7 @@ module Pusher
16
16
  class Error < RuntimeError; end
17
17
  class AuthenticationError < Error; end
18
18
  class ConfigurationError < Error; end
19
+ class HTTPError < Error; attr_accessor :original_error; end
19
20
 
20
21
  class << self
21
22
  attr_accessor :scheme, :host, :port
@@ -85,10 +85,19 @@ module Pusher
85
85
 
86
86
  request = Pusher::Request.new(@uri, event_name, data, socket_id)
87
87
 
88
- response = @http_sync.post("#{@uri.path}?#{request.query.to_params}",
89
- request.body, { 'Content-Type'=> 'application/json' })
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 StandardError => e
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
@@ -3,7 +3,7 @@ $:.push File.expand_path("../lib", __FILE__)
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "pusher"
6
- s.version = "0.7.1"
6
+ s.version = "0.8.0"
7
7
  s.platform = Gem::Platform::RUBY
8
8
  s.authors = ["New Bamboo"]
9
9
  s.email = ["support@pusherapp.com"]
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.disable_net_connect!
22
- WebMock.stub_request(
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 propagate exception if exception raised" do
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(RuntimeError)
78
- lambda {
80
+ ).to_raise(Timeout::Error)
81
+
82
+ error_raised = nil
83
+ begin
79
84
  Pusher['test_channel'].trigger!('new_event', 'Some data')
80
- }.should raise_error(RuntimeError)
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
- before :each do
114
- @http = mock('HTTP', :post => 'posting')
115
- Net::HTTP.stub!(:new).and_return @http
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 exception raised" do
126
- @http.should_receive(:post).and_raise("Fail")
127
- Pusher.logger.should_receive(:error).with("Fail (RuntimeError)")
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)
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 7
8
- - 1
9
- version: 0.7.1
7
+ - 8
8
+ - 0
9
+ version: 0.8.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - New Bamboo