Skip to content

Commit b842040

Browse files
shanempopeTheSmartnik
authored andcommitted
stream_body fragment include http_response feature (#588)
1 parent 43519c4 commit b842040

5 files changed

Lines changed: 46 additions & 3 deletions

File tree

examples/stream_download.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,14 @@
99

1010
File.open(filename, "w") do |file|
1111
response = HTTParty.get(url, stream_body: true) do |fragment|
12-
print "."
13-
file.write(fragment)
12+
if [301, 302].include?(fragment.code)
13+
print "skip writing for redirect"
14+
elsif fragment.code == 200
15+
print "."
16+
file.write(fragment)
17+
else
18+
raise StandardError, "Non-success status code while streaming #{fragment.code}"
19+
end
1420
end
1521
end
1622
puts
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
require 'delegate'
2+
3+
module HTTParty
4+
# Allow access to http_response and code by delegation on fragment
5+
class FragmentWithResponse < SimpleDelegator
6+
extend Forwardable
7+
8+
attr_reader :http_response
9+
10+
def code
11+
@http_response.code.to_i
12+
end
13+
14+
def initialize(fragment, http_response)
15+
@fragment = fragment
16+
@http_response = http_response
17+
super fragment
18+
end
19+
end
20+
end

lib/httparty/request.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
require 'erb'
22
require 'httparty/request/body'
3+
require 'httparty/fragment_with_response'
34

45
module HTTParty
56
class Request #:nodoc:
@@ -148,7 +149,7 @@ def perform(&block)
148149

149150
http_response.read_body do |fragment|
150151
chunks << fragment unless options[:stream_body]
151-
block.call(fragment)
152+
block.call FragmentWithResponse.new(fragment, http_response)
152153
end
153154

154155
chunked_body = chunks.join
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
require File.expand_path(File.join(File.dirname(__FILE__), '../spec_helper'))
2+
3+
RSpec.describe HTTParty::FragmentWithResponse do
4+
it "access to fragment" do
5+
fragment = HTTParty::FragmentWithResponse.new("chunk", nil)
6+
expect(fragment).to eq("chunk")
7+
end
8+
it "has access to delegators" do
9+
response = double(code: '200')
10+
fragment = HTTParty::FragmentWithResponse.new("chunk", response)
11+
expect(fragment.code).to eq(200)
12+
expect(fragment.http_response).to eq response
13+
end
14+
end

spec/httparty_spec.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -831,6 +831,8 @@ def self.inherited(subclass)
831831
expect(
832832
HTTParty.get('http://www.google.com', options) do |fragment|
833833
expect(chunks).to include(fragment)
834+
expect(fragment.code).to eql 200
835+
expect(fragment.http_response).to be
834836
end.parsed_response
835837
).to eq(nil)
836838
end

0 commit comments

Comments
 (0)