<commit_before># frozen_string_literal: true

module RubyRabbitmqJanus
  module Generators
    # Create an class for generate a custom configuration file
    class CreateRequestGenerator < Rails::Generators::Base
      desc 'Create an request to json format for RubyRabbitmqJanus transaction.'
      argument :type_name, type: :string, default: ''
      argument :janus_type, type: :string, default: ''
      argument :content, type: :string, default: ''

      def create_request
        File.open(file_json, 'w') do |file|
          file.write(write_json)
        end
      end

      private

      def file_json
        base_file = type_name.empty? ? 'config/requests' : create_folder?(type_name)
        "#{base_file}/#{janus_type}.json"
      end

      def write_json
        hash = {}
        hash['janus'] = janus_type
        hash.merge!(to_hash(content))
        JSON.pretty_generate(hash)
      end

      def to_hash(body, arr_sep = ',', key_sep = ':')
        hash = {}
        body.split(arr_sep).each do |couple_hash|
          key_value = couple_hash.split(key_sep)
          hash[key_value[0]] = test_string_hash(key_value, arr_sep, key_sep)
        end
        hash
      end

      # :reek:DuplicateMethodCall and :reek:FeatureEnvy
      def test_string_hash(value, arr_sep, key_sep)
        if value[1].include? '{'
          to_hash(value.drop(1).join(':').sub('{', '').sub('}', ''), arr_sep, key_sep)
        else<commit_msg>Update generator for using rails method<commit_after># frozen_string_literal: true

module RubyRabbitmqJanus
  module Generators
    # Create an class for generate a custom configuration file
    class CreateRequestGenerator < Rails::Generators::Base
      desc 'Create an request to json format for RubyRabbitmqJanus transaction.'
      argument :type_name, type: :string, default: ''
      argument :janus_type, type: :string, default: ''
      argument :content, type: :string, default: ''

      def create_request
        create_file file_json, write_json
      end

      private

      def file_json
        base_file = type_name.empty? ? 'config/requests' : create_folder?(type_name)
        "#{base_file}/#{janus_type}.json"
      end

      def write_json
        hash = {}
        hash['janus'] = janus_type
        hash.merge!(to_hash(content))
        JSON.pretty_generate(hash)
      end

      def to_hash(body, arr_sep = ',', key_sep = ':')
        hash = {}
        body.split(arr_sep).each do |couple_hash|
          key_value = couple_hash.split(key_sep)
          hash[key_value[0]] = test_string_hash(key_value, arr_sep, key_sep)
        end
        hash
      end

      # :reek:DuplicateMethodCall and :reek:FeatureEnvy
      def test_string_hash(value, arr_sep, key_sep)
        if value[1].include? '{'
          to_hash(value.drop(1).join(':').sub('{', '').sub('}', ''), arr_sep, key_sep)
        else
{'fraction_non_alphanumeric': 0.11339130434782609, 'fraction_numerical': 0.0020869565217391303, 'mean_word_length': 2.6919127086007704, 'pattern_counts': {'":': 0, '<': 5, '<?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': '6719287', 'n_tokens_mistral': 903, 'n_tokens_neox': 887, 'n_words': 203}