<commit_before>
module FreeSwitcher
  module Commands
    class Originate
      attr_accessor :originator, :target, :application, :application_arguments, :caller_id_number, :caller_id_name
      attr_reader :fs_socket

      def initialize(fs_socket, args = {})
        # These are options that will precede the target address
        @target_options = args[:target_options] || {:ignore_early_media => true}
        raise "#{@target_options} must be a hash" unless @target_options.kind_of?(Hash)
        
        @fs_socket = fs_socket # This socket must support say and <<
        @target = args[:target] # The target address to call
        # The origination extension
        @originator = args[:originator]

        # or application to attach the target caller to, and arguments for the application
        @application = args[:application]
        @application_arguments = args[:application_arguments]
        # These are options that will precede the application as modifiers
        @application_options = args[:application_options] || {}
        raise "#{@target_options} must be a hash" unless @target_options.kind_of?(Hash)

        @caller_id_number = args[:caller_id_number] || FreeSwitcher::DEFAULT_CALLER_ID_NAME
        @caller_id_name = args[:caller_id_name] || FreeSwitcher::DEFAULT_CALLER_ID_NUMBER
        @wait_for_media = 
        @timeout = args[:timeout] || 15
      end

      def run(api_method = :bgapi, wait_for_media = true)
        orig_command = command_text
        Log.debug "saying #{orig_command}"
        @fs_socket.say(orig_command)
      end
    end

    def command_text
      target_opts = @target_options.map { |k,v| "#{k}=#{v}" }
      if @originator
        orig_command = "originate {#{target_opts.join(',')}}#{@target} #{@originator}"
      elsif @application and @application_arguments
        orig_command = "originate {#{target_opts.join(',')}}#{@target} &#{@application}(#{@application_arguments})"
      else
        raise "Invalid originator or application"
      end
    end

    register(:originate, Originate)
  end
end<commit_msg>* Refactored originate to more closely model FS originate,   from http://wiki.freeswitch.org/wiki/Mod_commands#originate * Set defaults for caller_id_name, number, and timeout in Originate.new * Made command_text its own method, so run only sends the command_text   return string to the fs event socket<commit_after>
module FreeSwitcher
  module Commands
    class Originate
      attr_accessor :originator, :target, :application, :application_arguments
      attr_reader :fs_socket, :target_options, :application_options

      def initialize(fs_socket, args = {})
        # These are options that will precede the target address
        @target_options = args[:target_options] || {:ignore_early_media => true}
        raise "#{@target_options} must be a hash" unless @target_options.kind_of?(Hash)
        
        @fs_socket = fs_socket # This socket must support say and <<
        @target = args[:target] # The target address to call
        # The origination extension
        @originator = args[:originator]

        # or application to attach the target caller to, and arguments for the application
        @application = args[:application]
        @application_arguments = args[:application_arguments]

        # These are options that will precede the application as modifiers
        @application_options = args[:application_options] || {}
        raise "#{@application_options} must be a hash" unless @application_options.kind_of?(Hash)
        
        @target_options[:origination_caller_id_number] = args[:caller_id_number] || FreeSwitcher::DEFAULT_CALLER_ID_NUMBER
        @target_options[:origination_caller_id_name] = args[:caller_id_name] || FreeSwitcher::DEFAULT_CALLER_ID_NAME
        @target_options[:originate_timeout] = args[:timeout] || @target_options[:timeout] || 15
      end

      # Send the command to the event socket, using bgapi by default.
      def run(api_method = :bgapi)
        orig_command = "%s %s" % [api_method, command_text]
        Log.debug "saying #{orig_command}"
        @fs_socket.say(orig_command)
      end

      # This method builds the API command to send to the freeswitch event socket
      def command_text
        target_opts = @target_options.map { |k,v| "%s=%s" % [k, v] }.join(",")
        if @originator
          orig_command = "originate {#{target_opts.join(',')}}#{@target} #{@originator}"
        elsif @application and @application_arguments
          application_opts = @application_options.map { |k,v| "%s=%s" % [k, v] }.join(",")
          orig_command = "originate {#{target_opts}}#{@target} '&#{@application}({#{application_opts}}#{@application_arguments})'"
        else
          raise "Invalid originator or application"
        end
      end
    end

    register(:originate, Originate)
  end
end
{'fraction_non_alphanumeric': 0.10312885961300947, 'fraction_numerical': 0.0008233841086867024, 'mean_word_length': 3.401268115942029, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, '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': '6525262', 'n_tokens_mistral': 1387, 'n_tokens_neox': 1378, 'n_words': 377}