From b571e03ed18cd63ec1d1a57c03e5744b284111d6 Mon Sep 17 00:00:00 2001 From: Eric Roberts Date: Mon, 28 Apr 2014 16:33:10 -0400 Subject: [PATCH] Make the streaming API raise exceptions for status codes. --- lib/twitter/streaming/response.rb | 6 +++--- spec/twitter/streaming/response_spec.rb | 21 +++++++++++++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) create mode 100644 spec/twitter/streaming/response_spec.rb diff --git a/lib/twitter/streaming/response.rb b/lib/twitter/streaming/response.rb index 71e9e8f06..a9f8c483d 100644 --- a/lib/twitter/streaming/response.rb +++ b/lib/twitter/streaming/response.rb @@ -16,9 +16,9 @@ def <<(data) @parser << data end - def on_headers_complete(headers) - # TODO: handle response codes - p(:status_code => @parser.status_code, :header => headers) unless @parser.status_code == 200 + def on_headers_complete(_headers) + error = Twitter::Error.errors[@parser.status_code] + fail error.new if error end def on_body(data) diff --git a/spec/twitter/streaming/response_spec.rb b/spec/twitter/streaming/response_spec.rb new file mode 100644 index 000000000..efd701e52 --- /dev/null +++ b/spec/twitter/streaming/response_spec.rb @@ -0,0 +1,21 @@ +require 'helper' + +describe Twitter::Streaming::Response do + subject { Twitter::Streaming::Response.new } + + describe '#on_headers_complete' do + it 'should not error if status code is 200' do + expect do + subject << "HTTP/1.1 200 OK\r\nSome-Header: Woo\r\n\r\n" + end.to_not raise_error + end + + Twitter::Error.errors.each do |code, klass| + it "should raise an exception of type #{klass} for status code #{code}" do + expect do + subject << "HTTP/1.1 #{code} NOK\r\nSome-Header: Woo\r\n\r\n" + end.to raise_error(klass) + end + end + end +end