<commit_before>require "russianpost/operation"

module RussianPost
  module OperationsFactory
    class << self
      def build(operations_hash)
        if operations_hash
          operations_hash.map { |o| build_operation(o) }
        else
          []
        end
      end

      def build_operation(operation_hash)
        operation = RussianPost::Operation.new
        ungroup_parameters(operation_hash).each do |key, value|
          operation[key] = process_param(key, value)
        end

        operation
      end


      private

      # Initially all parameters are grouped (address parameters, finance
      # parameters, etc.). This method flattens the structure a bit      
      def ungroup_parameters(operation_hash)
        operation_hash.values.compact.reduce(Hash.new){ |acc, el| acc.merge(el) }
      end

      # Methods below convert certain parameters into proper data structures
      def process_param(key, value)
        process_fixnum(key, value) ||
        process_address(key, value) ||
        process_country(key, value) ||
        process_generic_param(key, value) ||
        value
      end

      def process_fixnum(key, value)
        if [:payment, :value, :mass_rate, :insr_rate, :air_rate, :rate, :mass, :max_mass_ru, :max_mass_en].include? key
          value.to_i
        end
      end

      def process_address(key, value)
        if [:destination_address, :operation_address].include? key
          RussianPost::Address.new(
            value[:index],
            value[:description])
        end
      end

      def process_country(key, value)
        if [:mail_direct, :country_from, :country_oper].include? key
          RussianPost::Country.new(
            value[:id] ? value[:id].to_i : nil,
            value[:code_2a],
            value[:code_3a],
            value[:name_ru],
            value[:name_en])
        end
      end

      def process_generic_param(key, value)
        if value.kind_of? Hash
          RussianPost::GenericOperationParameter.new(
            value[:id] ? value[:id].to_i : nil,
            value[:name])
        end
      end
    end
  end
end
<commit_msg>Added a check whether raw ops are a Hash or Array<commit_after>require "russianpost/operation"

module RussianPost
  module OperationsFactory
    class << self
      def build(operations_hash)
        if operations_hash
          if operations_hash.is_a?(Array)
            operations_hash.map { |o| build_operation(o) }
          else
            build_operation(operations_hash)
          end
        else
          []
        end
      end

      def build_operation(operation_hash)
        operation = RussianPost::Operation.new
        ungroup_parameters(operation_hash).each do |key, value|
          operation[key] = process_param(key, value)
        end

        operation
      end


      private

      # Initially all parameters are grouped (address parameters, finance
      # parameters, etc.). This method flattens the structure a bit      
      def ungroup_parameters(operation_hash)
        operation_hash.values.compact.reduce(Hash.new){ |acc, el| acc.merge(el) }
      end

      # Methods below convert certain parameters into proper data structures
      def process_param(key, value)
        process_fixnum(key, value) ||
        process_address(key, value) ||
        process_country(key, value) ||
        process_generic_param(key, value) ||
        value
      end

      def process_fixnum(key, value)
        if [:payment, :value, :mass_rate, :insr_rate, :air_rate, :rate, :mass, :max_mass_ru, :max_mass_en].include? key
          value.to_i
        end
      end

      def process_address(key, value)
        if [:destination_address, :operation_address].include? key
          RussianPost::Address.new(
            value[:index],
            value[:description])
        end
      end

      def process_country(key, value)
        if [:mail_direct, :country_from, :country_oper].include? key
          RussianPost::Country.new(
            value[:id] ? value[:id].to_i : nil,
            value[:code_2a],
            value[:code_3a],
            value[:name_ru],
            value[:name_en])
        end
      end

      def process_generic_param(key, value)
        if value.kind_of? Hash
          RussianPost::GenericOperationParameter.new(
            value[:id] ? value[:id].to_i : nil,
            value[:name])
        end
      end
    end
  end
end

{'fraction_non_alphanumeric': 0.09033423667570009, 'fraction_numerical': 0.0009033423667570009, 'mean_word_length': 2.1344656758669496, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, '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': '3853131', 'n_tokens_mistral': 1250, 'n_tokens_neox': 1222, 'n_words': 299}