Sha256: 37d80a0bc71e1f579e3fbf216f836e857710117a0972b7954feffddd92545372

Contents?: true

Size: 1.33 KB

Versions: 1

Compression:

Stored size: 1.33 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

      def initialize(*, supported_actions: self.class.supported_actions, **, &)
        self.supported_actions = supported_actions

        super(*, **, &)

        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.3 src/rails_command_connector.rb