mirror of
https://github.com/jnunemaker/httparty
synced 2023-03-27 23:23:07 -04:00
stream_body fragment include http_response feature (#588)
This commit is contained in:
parent
43519c4000
commit
b842040866
5 changed files with 46 additions and 3 deletions
|
@ -9,8 +9,14 @@ url = "https://cdn.kernel.org/pub/linux/kernel/v4.x/#{filename}"
|
|||
|
||||
File.open(filename, "w") do |file|
|
||||
response = HTTParty.get(url, stream_body: true) do |fragment|
|
||||
print "."
|
||||
file.write(fragment)
|
||||
if [301, 302].include?(fragment.code)
|
||||
print "skip writing for redirect"
|
||||
elsif fragment.code == 200
|
||||
print "."
|
||||
file.write(fragment)
|
||||
else
|
||||
raise StandardError, "Non-success status code while streaming #{fragment.code}"
|
||||
end
|
||||
end
|
||||
end
|
||||
puts
|
||||
|
|
20
lib/httparty/fragment_with_response.rb
Normal file
20
lib/httparty/fragment_with_response.rb
Normal file
|
@ -0,0 +1,20 @@
|
|||
require 'delegate'
|
||||
|
||||
module HTTParty
|
||||
# Allow access to http_response and code by delegation on fragment
|
||||
class FragmentWithResponse < SimpleDelegator
|
||||
extend Forwardable
|
||||
|
||||
attr_reader :http_response
|
||||
|
||||
def code
|
||||
@http_response.code.to_i
|
||||
end
|
||||
|
||||
def initialize(fragment, http_response)
|
||||
@fragment = fragment
|
||||
@http_response = http_response
|
||||
super fragment
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,5 +1,6 @@
|
|||
require 'erb'
|
||||
require 'httparty/request/body'
|
||||
require 'httparty/fragment_with_response'
|
||||
|
||||
module HTTParty
|
||||
class Request #:nodoc:
|
||||
|
@ -148,7 +149,7 @@ module HTTParty
|
|||
|
||||
http_response.read_body do |fragment|
|
||||
chunks << fragment unless options[:stream_body]
|
||||
block.call(fragment)
|
||||
block.call FragmentWithResponse.new(fragment, http_response)
|
||||
end
|
||||
|
||||
chunked_body = chunks.join
|
||||
|
|
14
spec/httparty/fragment_with_response_spec.rb
Normal file
14
spec/httparty/fragment_with_response_spec.rb
Normal file
|
@ -0,0 +1,14 @@
|
|||
require File.expand_path(File.join(File.dirname(__FILE__), '../spec_helper'))
|
||||
|
||||
RSpec.describe HTTParty::FragmentWithResponse do
|
||||
it "access to fragment" do
|
||||
fragment = HTTParty::FragmentWithResponse.new("chunk", nil)
|
||||
expect(fragment).to eq("chunk")
|
||||
end
|
||||
it "has access to delegators" do
|
||||
response = double(code: '200')
|
||||
fragment = HTTParty::FragmentWithResponse.new("chunk", response)
|
||||
expect(fragment.code).to eq(200)
|
||||
expect(fragment.http_response).to eq response
|
||||
end
|
||||
end
|
|
@ -831,6 +831,8 @@ RSpec.describe HTTParty do
|
|||
expect(
|
||||
HTTParty.get('http://www.google.com', options) do |fragment|
|
||||
expect(chunks).to include(fragment)
|
||||
expect(fragment.code).to eql 200
|
||||
expect(fragment.http_response).to be
|
||||
end.parsed_response
|
||||
).to eq(nil)
|
||||
end
|
||||
|
|
Loading…
Add table
Reference in a new issue