Class: AGIRouter

Inherits:
Object
  • Object
show all
Defined in:
lib/AGIRouter.rb

Overview

The AGIRouter is used to route clients to the proper AGIRoute method. It parses the URI presented to the AGIServer by asterisk in the request channel parameter, and if the AGIRoute object responds to the requested method/route, it calls it.

Constant Summary collapse

@@logger =

Logger for the AGIRouter class

Logger.new(STDERR)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(candidate_uri) ⇒ AGIRouter

Takes the request URI, and parses it into the requested method, a supplied id, and supplied options.



45
46
47
48
49
# File 'lib/AGIRouter.rb', line 45

def initialize(candidate_uri)
  @uri  = URI.parse(candidate_uri)
  @controller, @method, @id = parse_path(@uri.path)
  @options = parse_query(@uri.query)
end

Class Method Details

.logger(logger) ⇒ Object

Can be used to reset the AGIRouter classes logger object.



52
53
54
# File 'lib/AGIRouter.rb', line 52

def AGIRouter.logger(logger)
  @@logger = logger
end

Instance Method Details

#route(agi, params = nil) ⇒ Object

Takes an agi, and an optional parameters hash, and attempts to route to the requested method. If the method does not exist or is private, uses the agi to change the channel extension to invalid and the priority to 1.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/AGIRouter.rb', line 57

def route(agi, params=nil)
  request = { :uri        => @uri, 
              :controller => @controller, 
              :method     => @method,
              :id         => @id,
              :options    => @options
            }
            
  if controller = get_controller(@controller)
    if check_controller(controller)
      if check_route(controller, @method)
        @@logger.info{"AGIRouter Routing Request to #{@controller} #{@method} by #{@uri}"}
        controller.new({:agi => agi, :params => params, :request => request}).method(@method).call()
      else
        @@logger.warn{"AGIRouter was asked to route a call to controller with unroutable method #{@controller} #{@method}"}      
        agi.set_extension('i')
        agi.set_priority('1')
      end
    else
      @@logger.warn{"AGIRouter was asked to route a call to an invalid controller #{@controller}"}      
      agi.set_extension('i')
      agi.set_priority('1')
    end
  else
    @@logger.warn{"AGIRouter was asked to route a call to a nonexistant controller #{@controller}"}      
    agi.set_extension('i')
    agi.set_priority('1')
  end
end