Sha256: 9061c482368b924d5662aafc8dd5420138f2dfb61734ee97f310ab5393565633

Contents?: true

Size: 1.59 KB

Versions: 1

Compression:

Stored size: 1.59 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 manifest]
        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

        attach_to_rails_application_config!
        install_controller!
        install_routes!
      end

      def attach_to_rails_application_config!
        Rails.application.config.foobara_command_connector = self
      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.2 src/rails_command_connector.rb