1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00
fog--fog/lib/fog/xml/sax_parser_connection.rb
Paul Thornthwaite 4123be9568 [core/xml] Splits SAX parsing from Connection
This creates a new `Fog::Core::Connection` class that wraps around HTTP
connections/requests but does not presume SAX parsing of the API response.

A new `Fog::XML::SAXParserConnection` is available which implements the
original behaviour with a clearer interface.

`Fog::Connection` subclasses `SAXParserConnection` to be backwards
compatible.

Further testing and deprecation warnings are needed.

Since mock testing occurs at a higher level the changed code is not
exercised by the tests and I do not have access to an XML based API to
debug quickly with.
2013-06-24 17:30:51 +01:00

43 lines
1.6 KiB
Ruby

module Fog
module XML
class SAXParserConnection < Fog::Core::Connection
# Makes a request using the connection using Excon
#
# @param [Hash] params
# @option params [String] :body text to be sent over a socket
# @option params [Hash<Symbol, String>] :headers The default headers to supply in a request
# @option params [String] :host The destination host's reachable DNS name or IP, in the form of a String
# @option params [String] :path appears after 'scheme://host:port/'
# @option params [Fixnum] :port The port on which to connect, to the destination host
# @option params [Hash] :query appended to the 'scheme://host:port/path/' in the form of '?key=value'
# @option params [String] :scheme The protocol; 'https' causes OpenSSL to be used
# @option params [Proc] :response_block
# @option params [Nokogiri::XML::SAX::Document] :parser
#
# @return [Excon::Response]
#
# @raise [Excon::Errors::StubNotFound]
# @raise [Excon::Errors::Timeout]
# @raise [Excon::Errors::SocketError]
#
def request(parser, params)
reset unless @persistent
# Prepare the SAX parser
data_stream = Nokogiri::XML::SAX::PushParser.new(parser)
params[:response_block] = lambda do |chunk, remaining, total|
data_stream << chunk
end
# Make request which read chunks into parser
response = @excon.request(params)
# Cease parsing and override response.body with parsed data
data_stream.finish
response.body = parser.response
response
end
end
end
end