Class: Adhearsion::Console

Inherits:
Object show all
Includes:
Singleton
Defined in:
lib/adhearsion/console.rb

Defined Under Namespace

Classes: InteractiveController

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConsole

Returns a new instance of Console.



26
27
28
# File 'lib/adhearsion/console.rb', line 26

def initialize
  @input = $stdin
end

Instance Attribute Details

#inputObject

Returns the value of attribute input.



24
25
26
# File 'lib/adhearsion/console.rb', line 24

def input
  @input
end

Class Method Details

.method_missing(method, *args, &block) ⇒ Object



19
20
21
# File 'lib/adhearsion/console.rb', line 19

def method_missing(method, *args, &block)
  instance.send method, *args, &block
end

.mixin(mod) ⇒ Object

Include another external functionality into the console



15
16
17
# File 'lib/adhearsion/console.rb', line 15

def mixin(mod)
  include mod
end

Instance Method Details

#callsObject



65
66
67
# File 'lib/adhearsion/console.rb', line 65

def calls
  Adhearsion.active_calls
end

#cruby_with_readline?Boolean

Returns:

  • (Boolean)


109
110
111
112
113
114
115
116
117
# File 'lib/adhearsion/console.rb', line 109

def cruby_with_readline?
  begin
    # If NotImplemented then this might be libedit
    Readline.emacs_editing_mode
    true
  rescue NotImplementedError
    false
  end
end

#jruby?Boolean

Returns:

  • (Boolean)


119
120
121
# File 'lib/adhearsion/console.rb', line 119

def jruby?
  defined? JRUBY_VERSION
end

#log_level(level = nil) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/adhearsion/console.rb', line 53

def log_level(level = nil)
  if level
    Adhearsion::Logging.level = level
  else
    ::Logging::LEVELS.invert[Adhearsion::Logging.level].to_sym
  end
end

#originate(*args, &block) ⇒ Object



69
70
71
# File 'lib/adhearsion/console.rb', line 69

def originate(*args, &block)
  Adhearsion::OutboundCall.originate(*args, &block)
end

#runObject

Start the Adhearsion console



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/adhearsion/console.rb', line 33

def run
  if jruby? || cruby_with_readline?
    set_prompt
    Pry.config.command_prefix = "%"
    logger.info "Launching Adhearsion Console"
    @pry_thread = Thread.current
    pry
    logger.info "Adhearsion Console exiting"
  else
    logger.error "Unable to launch Adhearsion Console: This version of Ruby is using libedit. You must use readline for the console to work."
  end
end

#shutdown!Object



61
62
63
# File 'lib/adhearsion/console.rb', line 61

def shutdown!
  Process.shutdown!
end

#stopObject



46
47
48
49
50
51
# File 'lib/adhearsion/console.rb', line 46

def stop
  return unless instance_variable_defined?(:@pry_thread) && @pry_thread
  @pry_thread.kill
  @pry_thread = nil
  logger.info "Adhearsion Console shutting down"
end

#take(call = nil) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/adhearsion/console.rb', line 73

def take(call = nil)
  case call
  when Call
    interact_with_call call
  when String
    if call = calls[call]
      interact_with_call call
    else
      logger.error "An active call with that ID does not exist"
    end
  when nil
    case calls.size
    when 0
      logger.warn "No calls active to take"
    when 1
      interact_with_call calls.values.first
    else
      puts "Please choose a call:"
      puts "# (inbound/outbound) details"
      current_calls = calls.values
      current_calls.each_with_index do |active_call, index|
        puts "#{index}: (#{active_call.is_a?(OutboundCall) ? 'o' : 'i' }) #{active_call.id} from #{active_call.from} to #{active_call.to}"
      end
      print "#> "
      index = input.gets.chomp.to_i
      call = current_calls[index]
      interact_with_call call
    end
  else
    raise ArgumentError
  end
ensure
  set_prompt
  pry
end