<commit_before>require 'nokogiri'

module Twingly
  module LiveFeed
    class Parser
      # Parse an API response body.
      #
      # @param [String] document containing an API response XML.
      # @raise [Error] which error depends on the API response (see {Error.from_api_response_message}).
      # @return [Result] containing the result.
      def parse(document)
        nokogiri = Nokogiri::XML(document)

        failure = nokogiri.at_xpath('/error')
        handle_failure(failure) if failure

        data_node = nokogiri.at_xpath('/twinglydata')
        handle_non_xml_document(document) unless data_node

        create_result(data_node)
      end

      private

      def create_result(data_node)
        result = Result.new
        result.ts                  = parse_time(data_node.attribute('ts').value)
        result.from                = parse_time(data_node.attribute('from').value)
        result.number_of_posts     = data_node.attribute('noOfPosts').value.to_i
        result.max_number_of_posts = data_node.attribute('maxNumberOfPosts').value.to_i

        unless result.number_of_posts.zero?
          result.first_post = parse_time(data_node.attribute('firstPost').value)
          result.last_post  = parse_time(data_node.attribute('lastPost').value)
        end

        data_node.xpath('//post').each do |post|
          result.posts << parse_post(post)
        end

        result
      end

      def parse_post(element)
        post_params = {}
        element.element_children.each do |child|
          post_params[child.name] =
            if %w(tags links images).include?(child.name)
              parse_array(child)
            elsif child.name == "coordinates"
              parse_coordinates(child)
            else
              child.text
            end
        end

        post = Post.new
        post.set_values(post_params)<commit_msg>Use case instead of if statement<commit_after>require 'nokogiri'

module Twingly
  module LiveFeed
    class Parser
      # Parse an API response body.
      #
      # @param [String] document containing an API response XML.
      # @raise [Error] which error depends on the API response (see {Error.from_api_response_message}).
      # @return [Result] containing the result.
      def parse(document)
        nokogiri = Nokogiri::XML(document)

        failure = nokogiri.at_xpath('/error')
        handle_failure(failure) if failure

        data_node = nokogiri.at_xpath('/twinglydata')
        handle_non_xml_document(document) unless data_node

        create_result(data_node)
      end

      private

      def create_result(data_node)
        result = Result.new
        result.ts                  = parse_time(data_node.attribute('ts').value)
        result.from                = parse_time(data_node.attribute('from').value)
        result.number_of_posts     = data_node.attribute('noOfPosts').value.to_i
        result.max_number_of_posts = data_node.attribute('maxNumberOfPosts').value.to_i

        unless result.number_of_posts.zero?
          result.first_post = parse_time(data_node.attribute('firstPost').value)
          result.last_post  = parse_time(data_node.attribute('lastPost').value)
        end

        data_node.xpath('//post').each do |post|
          result.posts << parse_post(post)
        end

        result
      end

      def parse_post(element)
        post_params = {}
        element.element_children.each do |child|
          post_params[child.name] =
            case child.name
            when *%w(tags links images)
              parse_array(child)
            when "coordinates"
              parse_coordinates(child)
            else
              child.text
            end
        end

        post = Post.new
        post.set_values(post_params)
{'fraction_non_alphanumeric': 0.088841882601798, 'fraction_numerical': 0.0, 'mean_word_length': 2.1736577181208054, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '5500796', 'n_tokens_mistral': 1092, 'n_tokens_neox': 1063, 'n_words': 213}