Class: RubyAsterisk::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-asterisk/request.rb

Overview

Class responsible of building commands structure

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(action, parameters = {}, action_id: nil) ⇒ Request

Returns a new instance of Request.

Parameters:

  • action (String)

    AMI action name

  • parameters (Hash) (defaults to: {})

    additional AMI headers

  • action_id (String, nil) (defaults to: nil)

    ActionID to use; a generated one when nil. Passing it here (rather than as a parameter) keeps a single ActionID header on the wire, so responses still correlate with their Promise.



19
20
21
22
23
24
# File 'lib/ruby-asterisk/request.rb', line 19

def initialize(action, parameters = {}, action_id: nil)
  @action = action.freeze
  @action_id = (action_id || Request.generate_action_id).to_s.freeze
  @parameters = deep_freeze_hash(parameters)
  freeze
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



9
10
11
# File 'lib/ruby-asterisk/request.rb', line 9

def action
  @action
end

#action_idObject (readonly)

Returns the value of attribute action_id.



9
10
11
# File 'lib/ruby-asterisk/request.rb', line 9

def action_id
  @action_id
end

#parametersObject (readonly)

Returns the value of attribute parameters.



9
10
11
# File 'lib/ruby-asterisk/request.rb', line 9

def parameters
  @parameters
end

Class Method Details

.generate_action_idObject

Monotonic timestamp plus a process-wide atomic counter, so two calls made within the same nanosecond (or across an NTP clock step-back) never collide.



37
38
39
40
# File 'lib/ruby-asterisk/request.rb', line 37

def self.generate_action_id
  count = @id_mutex.synchronize { @id_counter += 1 }
  "#{Process.clock_gettime(Process::CLOCK_MONOTONIC, :nanosecond).to_s(36)}-#{count.to_s(36)}"
end

Instance Method Details

#commandsObject



26
27
28
29
30
31
32
33
# File 'lib/ruby-asterisk/request.rb', line 26

def commands
  command_list = ["Action: #{action}\r\n", "ActionID: #{action_id}\r\n"]
  parameters.each do |key, value|
    command_list << "#{key}: #{value}\r\n" unless value.nil?
  end
  command_list[-1] << "\r\n"
  command_list
end