Skip to content

Commit

Permalink
Use the new method to rewind responses created in excon/excon#657
Browse files Browse the repository at this point in the history
  • Loading branch information
maf23 committed Mar 6, 2018
1 parent 9e2458f commit 47d8a61
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
1 change: 1 addition & 0 deletions lib/fog/xml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module Fog
module XML
autoload :SAXParserConnection, File.expand_path("../xml/sax_parser_connection", __FILE__)
autoload :Connection, File.expand_path("../xml/connection", __FILE__)
autoload :Response, File.expand_path("../xml/response", __FILE__)
end

module Parsers
Expand Down
26 changes: 26 additions & 0 deletions lib/fog/xml/response.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module Fog
module XML
class Response
def initialize(parser)
@parser = parser
@data_stream = Nokogiri::XML::SAX::PushParser.new(parser)
@response_string = ""
end

def call(chunk, _remaining, _total)
@response_string << chunk if ENV["DEBUG_RESPONSE"]
@data_stream << chunk
end

def rewind
@parser.reset
@response_string = ""
end

def finish
Fog::Logger.debug "\n#{@response_string}" if ENV["DEBUG_RESPONSE"]
@data_stream.finish
end
end
end
end
11 changes: 2 additions & 9 deletions lib/fog/xml/sax_parser_connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,13 @@ class SAXParserConnection < ::Fog::Core::Connection
def request(parser, params)
reset unless @persistent

# Prepare the SAX parser
data_stream = Nokogiri::XML::SAX::PushParser.new(parser)
response_string = ""
params[:response_block] = lambda do |chunk, _remaining, _total|
response_string << chunk if ENV["DEBUG_RESPONSE"]
data_stream << chunk
end
params[:response_block] = ::Fog::XML::Response.new(parser)

# Make request which read chunks into parser
response = @excon.request(params)
Fog::Logger.debug "\n#{response_string}" if ENV["DEBUG_RESPONSE"]

# Cease parsing and override response.body with parsed data
data_stream.finish
params[:response_block].finish
response.body = parser.response
response
end
Expand Down

0 comments on commit 47d8a61

Please sign in to comment.