Module: AutonomaRails::Handler

Defined in:
lib/autonoma_rails/server.rb

Overview

Rails controller mixin for handling Autonoma protocol requests.

Usage in a Rails controller:

class AutonomaController < ApplicationController
  include AutonomaRails::Handler

  skip_before_action :verify_authenticity_token

  def handle
    autonoma_handle(autonoma_config)
  end

  private

  def autonoma_config
    @autonoma_config ||= Autonoma::HandlerConfig.new(
      scope_field: "organizationId",
      shared_secret: ENV["AUTONOMA_SHARED_SECRET"],
      signing_secret: ENV["AUTONOMA_SIGNING_SECRET"],
      auth: ->(user, ctx) { { "token" => "..." } },
      factories: { "User" => Autonoma::Factory.define_factory(...) }
    )
  end
end

Instance Method Summary collapse

Instance Method Details

#autonoma_handle(config) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/autonoma_rails/server.rb', line 33

def autonoma_handle(config)
  enriched = config.dup
  sdk = (enriched.sdk || {}).merge("server" => "rails")
  enriched.sdk = sdk

  body_str = request.raw_post
  headers = request.headers.to_h
    .select { |k, _| k.is_a?(String) }
    .transform_keys(&:downcase)

  req = Autonoma::HandlerRequest.new(body: body_str, headers: headers)
  result = Autonoma::Handler.handle_request(enriched, req)

  render json: Autonoma::Refs.make_json_safe(result.body), status: result.status
end