Sha256: 8ceb35fe9c9e4c01f03c740e31289d34e5af0488a3db636b41a7b72bbcda7939

Contents?: true

Size: 1.42 KB

Versions: 1

Compression:

Stored size: 1.42 KB

Contents

require "foobara/rack_connector"
require_relative "rails_command_connector/request"

module Foobara
  module CommandConnectors
    class RailsCommandConnector < Foobara::CommandConnectors::Http::Rack
      class << self
        # TODO: push this up the stack
        def supported_actions
          %i[run help describe list]
        end
      end

      attr_accessor :supported_actions

      # TODO: push these default serializers up into the base class
      def initialize(*, supported_actions: self.class.supported_actions, **opts, &)
        self.supported_actions = supported_actions

        opts[:default_serializers] ||= [
          Foobara::CommandConnectors::Serializers::ErrorsSerializer,
          Foobara::CommandConnectors::Serializers::JsonSerializer
        ]

        super(*, **opts, &)

        install!
      end

      def install!
        return if @installed

        @installed = true

        install_controller!
        install_routes!
      end

      def install_routes!
        prefix = self.prefix
        connector = self

        Rails.application.routes.draw do
          connector.supported_actions.each do |action|
            match "#{prefix}/#{action}/*args", to: "foobara/rails##{action}",
                                               via: %i[get post patch put delete]
          end
        end
      end

      def install_controller!
        require "foobara/rails/controller"
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
foobara-rails-command-connector-0.0.1 src/rails_command_connector.rb