File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 99
1010File . 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
1521end
1622puts
Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 11require 'erb'
22require 'httparty/request/body'
3+ require 'httparty/fragment_with_response'
34
45module 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
Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments