Permalink
Browse files

Output HTTP responses incrementally, in order to allow use of the str…

…eaming API
  • Loading branch information...
1 parent bc4a94f commit d8321bec23bce40f20bccd67fefc367091eefbef @j3h committed May 3, 2012
Showing with 26 additions and 20 deletions.
  1. +2 −0 lib/twurl/cli.rb
  2. +16 −15 lib/twurl/oauth_client.rb
  3. +3 −2 lib/twurl/request_controller.rb
  4. +3 −1 test/oauth_client_test.rb
  5. +2 −2 test/request_controller_test.rb
View
@@ -92,10 +92,12 @@ def output
def print(*args, &block)
output.print(*args, &block)
+ output.flush if output.respond_to?(:flush)
end
def puts(*args, &block)
output.puts(*args, &block)
+ output.flush if output.respond_to?(:flush)
end
def prompt_for(label)
View
@@ -62,21 +62,22 @@ def initialize(options = {})
configure_http!
end
- [:get, :post, :put, :delete, :options, :head, :copy].each do |request_method|
- class_eval(<<-EVAL, __FILE__, __LINE__)
- def #{request_method}(url, *options)
- # configure_http!
- access_token.#{request_method}(url, *options)
- end
- EVAL
- end
-
- def perform_request_from_options(options)
- if [:post, :put].include?(options.request_method.to_sym)
- send(options.request_method, options.path, options.data, options.headers)
- else
- send(options.request_method, options.path, options.headers)
- end
+ METHODS = {
+ :post => Net::HTTP::Post,
+ :get => Net::HTTP::Get,
+ :put => Net::HTTP::Put,
+ :delete => Net::HTTP::Delete,
+ :options => Net::HTTP::Options,
+ :head => Net::HTTP::Head,
+ :copy => Net::HTTP::Copy
+ }
+
+ def perform_request_from_options(options, &block)
+ request_class = METHODS.fetch(options.request_method.to_sym)
+ request = request_class.new(options.path, options.headers)
+ request.set_form_data(options.data) if options.data
+ request.oauth!(consumer.http, consumer, access_token)
+ consumer.http.request(request, &block)
end
def exchange_credentials_for_access_token
@@ -10,8 +10,9 @@ def dispatch
end
def perform_request
- response = client.perform_request_from_options(options)
- CLI.print response.body
+ client.perform_request_from_options(options) { |response|
+ response.read_body { |chunk| CLI.print chunk }
+ }
rescue URI::InvalidURIError
CLI.puts NO_URI_MESSAGE
end
@@ -135,7 +135,9 @@ def test_oauth_options_are_passed_through
class Twurl::OAuthClient::PerformingRequestsFromOptionsTest < Twurl::OAuthClient::AbstractOAuthClientTest
def test_request_is_made_using_request_method_and_path_and_data_in_options
client = Twurl::OAuthClient.test_exemplar
- mock(client).get(options.path, options.data)
+ mock(client.consumer.http).request(satisfy { |req|
+ req.is_a?(Net::HTTP::Get) && (req.path == options.path)
+ })
client.perform_request_from_options(options)
end
@@ -41,8 +41,8 @@ class Twurl::RequestController::RequestTest < Twurl::RequestController::Abstract
def test_request_response_is_written_to_output
expected_body = 'this is a fake response body'
response = Object.new
- mock(response).body.times(1) { expected_body }
- mock(client).perform_request_from_options(options).times(1) { response }
+ mock(response).read_body { |block| block.call expected_body }
+ mock(client).perform_request_from_options(options).times(1) { |options, block| block.call(response) }
controller.perform_request

0 comments on commit d8321be

Please sign in to comment.