<commit_before>require "active_support/xml_mini/nokogiri"

module Sucker #:nodoc:

  # A Nokogiri-driven wrapper around the cURL response
  class Response

    # The response body
    attr_accessor :body

    # The HTTP status code of the response
    attr_accessor :code

    # Transaction time in seconds for request
    attr_accessor :time

    # The request URI
    attr_accessor :uri

    def initialize(curl)
      self.body = curl.body_str
      self.code = curl.response_code
      self.time = curl.total_time
      self.uri  = curl.url
    end

    # A shorthand that yields each match to a block
    #
    #   worker.get.each("Item") { |item| process(item) }
    #
    def each(path)
      find(path).each { |e| yield e }
    end

    # Queries an xpath and returns an array of matching nodes
    #
    #   response = worker.get
    #   response.find("Item").each { |item| ... }
    #
    def find(path)
      xml.xpath("//xmlns:#{path}").map { |e| strip_content(e.to_hash[path]) }
    end

    # A shorthand that yields matches to a block and collects returned values
    #
    #   descriptions = worker.get.map("Item") { |item| build_description(item) }
    #
    def map(path)
      find(path).map { |e| yield e }
    end

    def node(path) # :nodoc:
      warn "[DEPRECATION] `node` is deprecated.  Use `find` instead."
      find(path)
    end

    # Parses the response into a simple hash
    #
    #   response = worker.get
    #   response.to_hash
    #
    def to_hash
      strip_content(xml.to_hash)
    end

    # Checks if the HTTP response is OK
    #
    #    response = worker.get
    #    p response.valid?
    #    => true
    #
    def valid?
      code == 200
    end

    # The XML document
    #
    #    response = worker.get
    #    response.xml
    def xml
      @xml ||= Nokogiri::XML(body)
    end

    private

    # Let's massage that hash
    def strip_content(node)
      case node
      when Array 
        node.map { |child| strip_content(child) }
      when Hash
        if node.keys.size == 1 && node["__content__"]
          node["__content__"]
        else
          node.inject({}) do |attributes, key_value|
            attributes.merge({ key_value.first => strip_content(key_value.last) })
          end
        end
      else
        node
      end
    end
  end

  class ResponseError < StandardError; end
end<commit_msg>Obsolete error class<commit_after>require "active_support/xml_mini/nokogiri"

module Sucker #:nodoc:

  # A Nokogiri-driven wrapper around the cURL response
  class Response

    # The response body
    attr_accessor :body

    # The HTTP status code of the response
    attr_accessor :code

    # Transaction time in seconds for request
    attr_accessor :time

    # The request URI
    attr_accessor :uri

    def initialize(curl)
      self.body = curl.body_str
      self.code = curl.response_code
      self.time = curl.total_time
      self.uri  = curl.url
    end

    # A shorthand that yields each match to a block
    #
    #   worker.get.each("Item") { |item| process(item) }
    #
    def each(path)
      find(path).each { |e| yield e }
    end

    # Queries an xpath and returns an array of matching nodes
    #
    #   response = worker.get
    #   response.find("Item").each { |item| ... }
    #
    def find(path)
      xml.xpath("//xmlns:#{path}").map { |e| strip_content(e.to_hash[path]) }
    end

    # A shorthand that yields matches to a block and collects returned values
    #
    #   descriptions = worker.get.map("Item") { |item| build_description(item) }
    #
    def map(path)
      find(path).map { |e| yield e }
    end

    def node(path) # :nodoc:
      warn "[DEPRECATION] `node` is deprecated.  Use `find` instead."
      find(path)
    end

    # Parses the response into a simple hash
    #
    #   response = worker.get
    #   response.to_hash
    #
    def to_hash
      strip_content(xml.to_hash)
    end

    # Checks if the HTTP response is OK
    #
    #    response = worker.get
    #    p response.valid?
    #    => true
    #
    def valid?
      code == 200
    end

    # The XML document
    #
    #    response = worker.get
    #    response.xml
    def xml
      @xml ||= Nokogiri::XML(body)
    end

    private

    # Let's massage that hash
    def strip_content(node)
      case node
      when Array 
        node.map { |child| strip_content(child) }
      when Hash
        if node.keys.size == 1 && node["__content__"]
          node["__content__"]
        else
          node.inject({}) do |attributes, key_value|
            attributes.merge({ key_value.first => strip_content(key_value.last) })
          end
        end
      else
        node
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.098026734563972, 'fraction_numerical': 0.001697432633142372, 'mean_word_length': 2.019859064702114, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 14}, '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': '7183627', 'n_tokens_mistral': 1531, 'n_tokens_neox': 1477, 'n_words': 417}