Skip to content

Commit

Permalink
Call on_error if a non-hash JSON object is received. Closes tweetstre…
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Bleigh committed Feb 11, 2010
1 parent 2e52c85 commit 50f3057
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/tweetstream/client.rb
Expand Up @@ -194,7 +194,14 @@ def start(path, query_parameters = {}, &block) #:nodoc:
)

@stream.each_item do |item|
hash = TweetStream::Hash.new(@parser.decode(item)) # @parser.parse(item)
raw_hash = @parser.decode(item)

unless raw_hash.is_a?(::Hash)
error_proc.call("Unexpected JSON object in stream: #{item}")
next
end

hash = TweetStream::Hash.new(raw_hash) # @parser.parse(item)

if hash[:delete] && hash[:delete][:status]
delete_proc.call(hash[:delete][:status][:id], hash[:delete][:status][:user_id]) if delete_proc.is_a?(Proc)
Expand Down
7 changes: 7 additions & 0 deletions spec/tweetstream/client_spec.rb
Expand Up @@ -125,6 +125,13 @@
track.should == 1234
end.track('abc')
end

it 'should call on_error if a non-hash response is received' do
@stream.should_receive(:each_item).and_yield('["favorited"]')
@client.on_error do |message|
message.should == 'Unexpected JSON object in stream: ["favorited"]'
end.track('abc')
end
end

describe '#on_error' do
Expand Down

0 comments on commit 50f3057

Please sign in to comment.