Permalink
Browse files

added ErrorResponse class for error handling callbacks

  • Loading branch information...
1 parent 2d270ef commit be11d38fc9c3e7f7b507b3acd317988f20ccc83b @stve stve committed Sep 28, 2012
Showing with 23 additions and 42 deletions.
  1. +8 −42 lib/tweetstream/client.rb
  2. +1 −0 lib/tweetstream/response.rb
  3. +14 −0 lib/tweetstream/response/error_response.rb
View
@@ -387,12 +387,12 @@ def connect(path, options = {}, &block)
begin
hash = MultiJson.decode(item, :symbolize_keys => true)
rescue MultiJson::DecodeError
- invoke_callback(callbacks['error'], "MultiJson::DecodeError occured in stream: #{item}")
+ TweetStream::ErrorResponse.new(callbacks['error'], "MultiJson::DecodeError occured in stream: #{item}").invoke
next
end
unless hash.is_a?(::Hash)
- invoke_callback(callbacks['error'], "Unexpected JSON object in stream: #{item}")
+ TweetStream::ErrorResponse.new(callbacks['error'], "Unexpected JSON object in stream: #{item}").invoke
next
end
@@ -401,30 +401,12 @@ def connect(path, options = {}, &block)
TweetStream::Response.new(self, hash, callbacks, &block).invoke
end
- @stream.on_error do |message|
- invoke_callback(callbacks['error'], message)
- end
-
- @stream.on_unauthorized do
- invoke_callback(callbacks['unauthorized'])
- end
-
- @stream.on_enhance_your_calm do
- invoke_callback(callbacks['enhance_your_calm'])
- end
-
- @stream.on_reconnect do |timeout, retries|
- invoke_callback(callbacks['reconnect'], timeout, retries)
- end
-
- @stream.on_max_reconnects do |timeout, retries|
- raise TweetStream::ReconnectError.new(timeout, retries)
- end
-
- @stream.on_no_data_received do
- invoke_callback(callbacks['no_data_received'])
- end
-
+ @stream.on_error { |message| TweetStream::ErrorResponse.new(callbacks['error'], message).invoke }
+ @stream.on_unauthorized { TweetStream::ErrorResponse.new(callbacks['unauthorized']).invoke }
+ @stream.on_enhance_your_calm { TweetStream::ErrorResponse.new(callbacks['enhance_your_calm']).invoke }
+ @stream.on_reconnect { |timeout, retries| TweetStream::ErrorResponse.new(callbacks['reconnect'], timeout, retries).invoke }
+ @stream.on_max_reconnects { |timeout, retries| raise TweetStream::ReconnectError.new(timeout, retries) }
+ @stream.on_no_data_received { TweetStream::ErrorResponse.new(callbacks['no_data_received']).invoke }
@stream
end
@@ -474,22 +456,6 @@ def auth_params
end
end
- # A utility method used to invoke callback methods against the Client
- def invoke_callback(callback, *args)
- callback.call(*args) if callback
- end
-
- def yield_message_to(procedure, message)
- # Give the block the option to receive either one
- # or two arguments, depending on its arity.
- if procedure.is_a?(Proc)
- case procedure.arity
- when 1 then invoke_callback(procedure, message)
- when 2 then invoke_callback(procedure, message, self)
- end
- end
- end
-
def connection_options(path, options)
warn_if_callbacks(options)
@@ -53,6 +53,7 @@ def yield_message_to(procedure, message)
end
end
+require 'tweetstream/response/error_response'
require 'tweetstream/response/anything_response'
require 'tweetstream/response/control_response'
require 'tweetstream/response/delete_response'
@@ -0,0 +1,14 @@
+module TweetStream
+ class ErrorResponse < Response
+
+ def initialize(callback, *args)
+ @callback = callback
+ @arguments = args
+ end
+
+ def invoke
+ invoke_callback(@callback, *@arguments)
+ end
+
+ end
+end

0 comments on commit be11d38

Please sign in to comment.