Sha256: cd16fb76816b7d22d36af10af0ee2c2ddd563a7c35ee3879ae86f9dc6fd36271

Contents?: true

Size: 1.36 KB

Versions: 6

Compression:

Stored size: 1.36 KB

Contents

require_relative './request_entities/request'
require_relative './handler'

# Routes an incoming request to the correct Handler.
module Ralyxa
  class Skill
    @handlers = {}

    def initialize(request)
      @request = request
    end

    def handle
      handler = self.class.handlers[@request.intent_name]
      handler ? handler.new(@request).handle : warn(handler_not_found)
    end

    class << self
      def intent(intent_name, handler_base_class = Ralyxa::Handler, &intent_block)
        intent_handler = Class.new(handler_base_class)
        intent_handler.send(:define_method, :handle, &intent_block)
        @handlers[intent_name] = intent_handler
      end

      def handle(request, alexa_request_wrapper = Ralyxa::RequestEntities::Request)
        new(alexa_request_wrapper.new(request)).handle
      end

      def handlers
        @handlers.dup
      end
    end

    private

    def handler_not_found
      <<~HEREDOC
        \e[33m
        WARNING: An intent declaration for intent "#{@request.intent_name}" was not found.

        To define it, add an intent declaration inside a directory called 'intents',
        on the same directory level as the runfile for your server application.

        You probably want something like:

        intent "#{@request.intent_name}" do
          respond("Hello World!")
        end
        \e[0m
      HEREDOC
    end
  end
end

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
ralyxa-lambda-1.8.0 lib/ralyxa/skill.rb
ralyxa-1.8.0 lib/ralyxa/skill.rb
ralyxa-1.7.0 lib/ralyxa/skill.rb
ralyxa-1.6.2 lib/ralyxa/skill.rb
ralyxa-1.6.1 lib/ralyxa/skill.rb
ralyxa-1.6.0 lib/ralyxa/skill.rb