Class: Spree::AgentTools::GetResource

Inherits:
Spree::AgentTool show all
Defined in:
app/tools/spree/agent_tools/get_resource.rb

Overview

Fetches one record in full, through the Admin API's own show operation, for when the summary from a search is not enough to answer the question.

Constant Summary

Constants inherited from Spree::AgentTool

Spree::AgentTool::PARENT_ID_DESCRIPTION

Instance Attribute Summary

Attributes inherited from Spree::AgentTool

#context

Instance Method Summary collapse

Methods inherited from Spree::AgentTool

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

Constructor Details

This class inherits a constructor from Spree::AgentTool

Instance Method Details

#call(resource:, id:, expand: nil, parent_id: nil) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/tools/spree/agent_tools/get_resource.rb', line 19

def call(resource:, id:, expand: nil, parent_id: nil)
  entry = ResourceMap.find(resource)
  return unknown_resource(resource) if entry.nil?
  return { error: "You do not have permission to read #{entry.key}." } unless
    entry.readable_by?(context)

  return { error: "#{entry.key} cannot be fetched one at a time." } if entry.api_path(:show).blank?

  path = entry.path_for(:show, id: id, parent_id: parent_id)
  return nested_resource_error(entry, 'fetched', :show) if path.nil?

  requested, refusal = expansions(entry, expand)
  return refusal if refusal

  response = dispatch.call(method: :get, path: path,
                           params: requested.any? ? { expand: requested.join(',') } : {})
  return { error: not_found(entry, id, response) } unless response.success?

  attributes = sanitize(response.data, entry)
  # An empty hash would be reported as a successful lookup of a record
  # with no fields, which is worse than saying the read failed.
  return { error: "Could not read this #{entry.key.singularize}." } if attributes.blank?

  {
    resource: entry.key,
    # The full record for the model to answer from, plus the same
    # compact row `search_resources` returns so the panel can show it
    # as something the merchant can click through to.
    record: attributes,
    records: [RecordSummary.from_payload(entry: entry, payload: attributes)],
    count: 1,
    total: 1
  }
end

#expansions(entry, expand) ⇒ Object

An association the serializer does not offer is named back rather than ignored, so a model that guessed learns the real list instead of reading a record that silently lacks what it asked for.



57
58
59
60
61
62
63
64
65
66
67
# File 'app/tools/spree/agent_tools/get_resource.rb', line 57

def expansions(entry, expand)
  requested = Array(expand).map(&:to_s).reject(&:blank?)
  return [[], nil] if requested.empty?

  offered = entry.expandable_associations
  unknown = requested - offered
  return [requested, nil] if unknown.empty?

  [nil, { error: "#{entry.key} cannot expand #{unknown.to_sentence}.",
          expandable: offered }]
end

#summary(arguments) ⇒ Object

Named, not identified: "Look up Rotary Shaver 9000" is what the merchant is watching happen. A prefixed id is our plumbing.



71
72
73
# File 'app/tools/spree/agent_tools/get_resource.rb', line 71

def summary(arguments)
  "Look up #{arguments[:id]}"
end