Class: FSR::Cmd::Channels

Inherits:
Command
  • Object
show all
Includes:
Enumerable
Defined in:
lib/fsr/cmd/channels.rb

Constant Summary

Constants inherited from Command

FSR::Cmd::Command::DEFAULT_OPTIONS

Instance Method Summary collapse

Constructor Details

#initialize(fs_socket = nil, filter = nil) ⇒ Channels

Returns a new instance of Channels.



14
15
16
17
18
# File 'lib/fsr/cmd/channels.rb', line 14

def initialize(fs_socket = nil, filter = nil)
  @filter = filter
  @filter = nil if @filter === false
  @fs_socket = fs_socket # FSR::CommandSocket obj
end

Instance Method Details

#each(&block) ⇒ Object



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

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

#rawObject

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



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/fsr/cmd/channels.rb', line 43

def raw
  if @filter === true
    'show distinct_channels'
  elsif @filter.nil?
    'show channels'
  elsif @filter.is_a?(Fixnum)
    'show channels %d' % @filter
  elsif @filter.is_a?(String)
    "show channels like '%s'" % @filter
  else
    'show channels'
  end
end

#run(api_method = :api) ⇒ Object

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



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/fsr/cmd/channels.rb', line 21

def run(api_method = :api)
  orig_command = "%s %s" % [api_method, raw]
  Log.debug "saying #{orig_command}"
  resp = @fs_socket.say(orig_command)
  if resp["body"] =~ /USAGE/
    Log.warn "This server does not support #{raw}, trying Calls"
    return Calls.new(@fs_socket, :detailed).run
  else
    unless resp["body"] == "0 total."
      call_info, count = resp["body"].split("\n\n")
      require "fsr/model/channel"
      require "csv"
      channels = CSV.parse(call_info) 
      headers = channels[0]
      @channels = channels[1 .. -1].map { |c| FSR::Model::Channel.new(headers ,*c) }
      return @channels
    end
    []
  end
end