<commit_before>module Savon

  # Savon::WSDL
  #
  # Represents a WSDL document.
  class WSDL

    # Expects a Savon::Request object.
    def initialize(request)
      @request = request
    end

    # Returns the namespace URI from the WSDL.
    def namespace_uri
      @namespace_uri ||= document.root.attributes["targetNamespace"] || ""
    end

    # Returns a Hash of available SOAP actions mapped to snake_case (keys)
    # and their original names and inputs in another Hash (values).
    def soap_actions
      @soap_actions ||= parse_soap_operations.inject({}) do |hash, (input, action)|
        hash.merge input.snakecase.to_sym => { :name => action, :input => input }
      end
    end

    # Returns +true+ for available methods and SOAP actions.
    def respond_to?(method)
      return true if soap_actions.keys.include? method
      super
    end

    # Returns the WSDL document.
    def to_s
      wsdl_response.body
    end

  private

    # Retrieves and returns the WSDL response. Raises an ArgumentError in
    # case the WSDL seems to be invalid. 
    def wsdl_response
      unless @wsdl_response
        @wsdl_response ||= @request.wsdl
        raise ArgumentError, "Invalid WSDL: #{@request.endpoint}" if soap_actions.empty?
      end
      @wsdl_response
    end

    # Returns a REXML::Document of the WSDL.
    def document
      @document ||= REXML::Document.new wsdl_response.body
    end

    # Parses the WSDL for available SOAP actions and inputs. Returns a Hash
    # containing the SOAP action inputs and corresponding SOAP actions.
    def parse_soap_operations
      wsdl_binding = document.elements["wsdl:definitions/wsdl:binding"]
      return {} unless wsdl_binding

      wsdl_binding.elements.inject("wsdl:operation", {}) do |hash, operation|
        action = operation.elements["*:operation"].attributes["soapAction"] || ""
        action = operation.attributes["name"] if action.empty?

        hash.merge action.split("/").last => action
      end
    end

  end
end<commit_msg>replaced document with stream parsing<commit_after>module Savon

  # Savon::WSDL
  #
  # Represents the WSDL document.
  class WSDL

    # Expects a Savon::Request object.
    def initialize(request)
      @request = request
    end

    # Returns the namespace URI from the WSDL.
    def namespace_uri
      @namespace_uri ||= stream.namespace_uri
    end

    # Returns a Hash of available SOAP actions mapped to snake_case (keys)
    # and their original names and inputs in another Hash (values).
    def soap_actions
      @soap_actions ||= stream.soap_actions
    end

    # Returns +true+ for available methods and SOAP actions.
    def respond_to?(method)
      return true if soap_actions.keys.include? method
      super
    end

    # Returns the WSDL document.
    def to_s
      @document ||= @request.wsdl.body
    end

  private

    def stream
      unless @stream
        @stream = WSDLStream.new
        REXML::Document.parse_stream to_s, @stream
      end
      @stream
    end

  end

  # Savon::WSDLStream
  #
  # Stream listener parsing the WSDL document.
  class WSDLStream

    # Sets the initial state.
    def initialize
      @namespace_uri = ""
      @soap_actions = {}
      @wsdl_binding = false
    end

    attr_reader :namespace_uri

    attr_reader :soap_actions
 
    def tag_start(name, attrs)
      @namespace_uri = attrs["targetNamespace"] if name == "wsdl:definitions"
      @wsdl_binding = true if name == "wsdl:binding"
      soap_action(name, attrs) if @wsdl_binding && /.+:operation/ === name
    end

    def soap_action(name, attrs)
      if name == "wsdl:operation"
        @action = attrs["name"]
      elsif /.+:operation/ === name
        @action = attrs["soapAction"] unless attrs["soapAction"].empty?
        input = @action.split("/").last
        @soap_actions[input.snakecase.to_sym] = { :name => @action, :input => input }
      end
    end

    def method_missing(method, *args)
    end

  end
end
{'fraction_non_alphanumeric': 0.09129778672032193, 'fraction_numerical': 0.0, 'mean_word_length': 2.689239332096475, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3577287', 'n_tokens_mistral': 1223, 'n_tokens_neox': 1155, 'n_words': 356}