Class: Spree::AgentTools::OperationTool

Inherits:
Spree::AgentTool show all
Defined in:
lib/spree/agent_tools/operation_tool.rb

Overview

One agent tool per Admin API operation in OperationCatalog.

Everything about the tool is the operation's: its name, its prose, its arguments and the request it sends. The arguments are the path segments and the request body under the names the document gives them, so a call is a request with no translation step between what the model was shown and what the endpoint receives. The endpoint runs its own routing, authentication, scope check, record-level ability and workflow.

Generated classes are cached per entry, so a catalog rebuilt after a code reload produces new tools rather than serving stale ones.

See docs/plans/6.0-agent-tools-from-openapi.md.

Constant Summary

Constants inherited from Spree::AgentTool

Spree::AgentTool::PARENT_ID_DESCRIPTION

Instance Attribute Summary

Attributes inherited from Spree::AgentTool

#context

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Spree::AgentTool

additive!, audience, description, destructive?, #initialize, mutating!, mutating?, param, params, permission, #permitted?, tool_name, #unknown_resource

Constructor Details

This class inherits a constructor from Spree::AgentTool

Class Method Details

.clear_cache! ⇒ void

This method returns an undefined value.



41
42
43
# File 'lib/spree/agent_tools/operation_tool.rb', line 41

def clear_cache!
  @cache = {}
end

.for(entry) ⇒ Class<OperationTool>

Parameters:

Returns:



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/spree/agent_tools/operation_tool.rb', line 22

def for(entry)
  # By identity: an entry is rebuilt, as a new object, whenever the
  # catalog is, and hashing one walks its whole request schema.
  cache[entry] ||= Class.new(self) do
    self.operation = entry

    tool_name entry.name
    description entry.description
    permission entry.permission
    mutating! if entry.mutating
  end
end

.input_schema ⇒ Hash

Returns JSON Schema for the arguments, read by the adapters.

Returns:

  • (Hash) —

    JSON Schema for the arguments, read by the adapters



36
37
38
# File 'lib/spree/agent_tools/operation_tool.rb', line 36

def input_schema
  @input_schema ||= operation.input_schema
end

Instance Method Details

#call(**arguments) ⇒ Hash

Returns the result handed back to the model.

Returns:

  • (Hash) —

    the result handed back to the model



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/spree/agent_tools/operation_tool.rb', line 55

def call(**arguments)
  arguments, notes = map_legacy_arguments(arguments.transform_keys(&:to_s))

  segments = operation.path_params.index_with { |segment| arguments[segment] }
  derive_parents(segments)
  path = operation.path_for(segments)
  # Only what the path needs is checked here. What the body requires is
  # the endpoint's to refuse, in its own words.
  if path.nil?
    unfilled = segments.select { |_segment, value| value.blank? }.keys
    return { error: "#{tool_name} needs #{unfilled.to_sentence}.", deprecations: notes.presence }.compact
  end

  values, refusal = body_for(arguments)
  return refusal.merge(deprecations: notes.presence).compact if refusal

  response = dispatch(path, values)
  return { error: response.error_message, deprecations: notes.presence }.compact unless response.success?

  # A read opted in to the catalog answers with what it read, filtered
  # like every other record that leaves for a model.
  return { summary: operation.summary, data: sanitized(response.data) } if operation.read?

  record = payload_summary(response.data)
  named = record&.dig(:title).presence || record&.dig(:id).presence || record&.dig('id').presence
  { summary: named ? "#{operation.summary} #{named}" : summary(arguments), record: record,
    deprecations: notes.presence }.compact
end

#summary(arguments) ⇒ String

The line a client shows before the call runs: the operation's own summary and the records it names.

Parameters:

  • arguments (Hash)

Returns:

  • (String)


89
90
91
92
93
# File 'lib/spree/agent_tools/operation_tool.rb', line 89

def summary(arguments)
  arguments, = map_legacy_arguments(arguments.transform_keys(&:to_s))
  named = operation.path_params.filter_map { |segment| arguments[segment] }
  [operation.summary, named.reverse.join(' ').presence].compact.join(' ')
end