class Synthetic::Bot

Attributes

routes[RW]

Public Class Methods

new(options={}) click to toggle source
Calls superclass method
# File lib/synthetic/bot.rb, line 11
def initialize(options={})
        super()

        # Override Cinch's bot configuration with our own extended one
        @config = Synthetic::Configuration::Bot.new

        # Configure any known bot options that were passed, ignoring unknown keys
        options.select do |key, value|
                Cinch::Configuration::Bot::KnownOptions.include?(key)
        end.each do |key, value|
                @config[key] = value
        end

        # Enable Wit for Cinch
        @config.plugins.plugins << Cinch::Plugins::Wit
        @config.plugins.options[Cinch::Plugins::Wit][:access_token] = options[:wit_token]

        # Load bot app
        load_root!

        # Catch intent events and route to their designated controller
        on :intent do |msg, wit|
                if msg.bot.routes.has_key?(wit['intent'])
                        controller = msg.bot.routes[wit['intent']].new
                        controller.send(wit['intent'], msg, wit)
                end
        end
end

Private Instance Methods

load_root!() click to toggle source
# File lib/synthetic/bot.rb, line 44
def load_root!
        # Add the bot root directory to load path
        if File.directory?(@config[:root]) && !$LOAD_PATH.include?(@config[:root])
                $LOAD_PATH.unshift(@config[:root])
        end

        # Require source files in the bot root directory
        matcher = /\A#{Regexp.escape(@config[:root])}\/(.*)\.rb\Z/
        Dir.glob("#{@config[:root]}/app/**/*.rb").sort.each do |file|
                debug "Requiring '#{file.sub(matcher, '\1')}'"
                require file.sub(matcher, '\1')
        end

        # Load routes config file
        @routes = Hash.new
        routes_config = YAML.load_file "#{@config[:root]}/config/routes.yml"
        routes_config.each do |route|
                intents = ([route['intent']] || []) + (route['intents'] || [])
                intents.each do |intent|
                        @routes[intent] = Object.const_get(route['to'].capitalize + 'Controller')
                end
        end
end