Class: FSR::Cmd::Calls

Inherits:
Command show all
Includes:
Enumerable
Defined in:
lib/fsr/cmd/calls.rb

Constant Summary collapse

TYPES =
[:detailed, :bridged, :detailed_bridged]

Constants inherited from Command

FSR::Cmd::Command::DEFAULT_OPTIONS

Instance Method Summary collapse

Constructor Details

#initialize(fs_socket = nil, type = nil, filter = nil) ⇒ Calls

Returns a new instance of Calls.



15
16
17
18
19
20
21
22
# File 'lib/fsr/cmd/calls.rb', line 15

def initialize(fs_socket = nil, type = nil, filter = nil)
  @type = type
  @filter = filter
  unless @type.nil?
    raise ArgumentError, "Only #{TYPES} are allowed as arguments" unless TYPES.include?(@type)
  end
  @fs_socket = fs_socket # FSR::CommandSocket obj
end

Instance Method Details

#each(&block) ⇒ Object



8
9
10
11
12
13
# File 'lib/fsr/cmd/calls.rb', line 8

def each(&block)
  @calls ||= run
  if @calls
    @calls.each { |call| yield call }
  end
end

#rawObject

This method builds the API command to send to the freeswitch event socket



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/fsr/cmd/calls.rb', line 40

def raw
  base = if @type.nil?
    "show calls"
  else
    "show %s_calls" % @type
  end
  if @filter
    "%s like '%s'" % [base, @filter]
  else
    base
  end
end

#run(api_method = :api) ⇒ Object

Send the command to the event socket, using bgapi by default.



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/fsr/cmd/calls.rb', line 25

def run(api_method = :api)
  orig_command = "%s %s" % [api_method, raw]
  Log.debug "saying #{orig_command}"
  resp = @fs_socket.say(orig_command)
  unless resp["body"] == "0 total."
    call_info, count = resp["body"].split("\n\n")
    require "fsr/model/call"
    require "csv"
    @calls = CSV.parse(call_info)
    return @calls[1 .. -1].map { |c| FSR::Model::Call.new(@calls[0],*c) }
  end
  []
end