Class: Foobara::CommandConnector::Request

Inherits:
Object
  • Object
show all
Includes:
NestedTransactionable, TruncatedInspect
Defined in:
foobara-0.0.125/projects/command_connectors/src/command_connector/request.rb

Constant Summary

Constants included from TruncatedInspect

TruncatedInspect::MAX_LENGTH

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from NestedTransactionable

#auto_detect_current_transactions, #commit_transaction, #commit_transaction_if_open, #open_transaction, #opened_transactions, relevant_entity_classes_for_type, #relevant_entity_classes_for_type, #rollback_transaction, #transactions, #use_transaction

Methods included from Foobara::Concern

foobara_class_methods_module_for, foobara_concern?, included

Methods included from TruncatedInspect

#inspect, truncating

Constructor Details

#initialize(**opts) ⇒ Request

Returns a new instance of Request.

[View source]

21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'foobara-0.0.125/projects/command_connectors/src/command_connector/request.rb', line 21

def initialize(**opts)
  valid_keys = [:inputs, :full_command_name, :action, :serializers]

  invalid_keys = opts.keys - valid_keys

  unless invalid_keys.empty?
    # :nocov:
    raise ArgumentError, "invalid keys: #{invalid_keys} expected only #{valid_keys}"
    # :nocov:
  end

  self.inputs = opts[:inputs] if opts.key?(:inputs)
  self.action = opts[:action] if opts.key?(:action)
  self.full_command_name = opts[:full_command_name] if opts.key?(:full_command_name)
  self.serializers = Util.array(opts[:serializers]) if opts.key?(:serializers)
end

Instance Attribute Details

#actionObject

TODO: this feels like a smell of some sort…


8
9
10
# File 'foobara-0.0.125/projects/command_connectors/src/command_connector/request.rb', line 8

def action
  @action
end

#authenticated_credentialObject

TODO: this feels like a smell of some sort…


8
9
10
# File 'foobara-0.0.125/projects/command_connectors/src/command_connector/request.rb', line 8

def authenticated_credential
  @authenticated_credential
end

#authenticated_userObject

TODO: this feels like a smell of some sort…


8
9
10
# File 'foobara-0.0.125/projects/command_connectors/src/command_connector/request.rb', line 8

def authenticated_user
  @authenticated_user
end

#commandObject

TODO: this feels like a smell of some sort…


8
9
10
# File 'foobara-0.0.125/projects/command_connectors/src/command_connector/request.rb', line 8

def command
  @command
end

#command_classObject

TODO: this feels like a smell of some sort…


8
9
10
# File 'foobara-0.0.125/projects/command_connectors/src/command_connector/request.rb', line 8

def command_class
  @command_class
end

#command_connectorObject

TODO: this feels like a smell of some sort…


8
9
10
# File 'foobara-0.0.125/projects/command_connectors/src/command_connector/request.rb', line 8

def command_connector
  @command_connector
end

#errorObject

TODO: this feels like a smell of some sort…


8
9
10
# File 'foobara-0.0.125/projects/command_connectors/src/command_connector/request.rb', line 8

def error
  @error
end

#full_command_nameObject

TODO: this feels like a smell of some sort…


8
9
10
# File 'foobara-0.0.125/projects/command_connectors/src/command_connector/request.rb', line 8

def full_command_name
  @full_command_name
end

#inputsObject

TODO: this feels like a smell of some sort…


8
9
10
# File 'foobara-0.0.125/projects/command_connectors/src/command_connector/request.rb', line 8

def inputs
  @inputs
end

#responseObject

TODO: this feels like a smell of some sort…


8
9
10
# File 'foobara-0.0.125/projects/command_connectors/src/command_connector/request.rb', line 8

def response
  @response
end

#serializersObject

TODO: this feels like a smell of some sort…


8
9
10
# File 'foobara-0.0.125/projects/command_connectors/src/command_connector/request.rb', line 8

def serializers
  @serializers
end

Instance Method Details

#authenticateObject

[View source]

47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'foobara-0.0.125/projects/command_connectors/src/command_connector/request.rb', line 47

def authenticate
  return if error
  return unless command_class.respond_to?(:requires_authentication) && command_class.requires_authentication

  authenticated_user, authenticated_credential = authenticator.authenticate(self)

  # TODO: why are these on the command instead of the request??
  self.authenticated_user = authenticated_user
  self.authenticated_credential = authenticated_credential

  unless authenticated_user
    self.error = CommandConnector::UnauthenticatedError.new
  end
end

#authenticatorObject

[View source]

62
63
64
# File 'foobara-0.0.125/projects/command_connectors/src/command_connector/request.rb', line 62

def authenticator
  command_class.authenticator
end

#error_collectionObject

[View source]

112
113
114
# File 'foobara-0.0.125/projects/command_connectors/src/command_connector/request.rb', line 112

def error_collection
  outcome.error_collection
end

#mutate_requestObject

[View source]

38
39
40
41
42
43
44
45
# File 'foobara-0.0.125/projects/command_connectors/src/command_connector/request.rb', line 38

def mutate_request
  return if error

  # TODO: we really need to revisit these interfaces. Something is wrong.
  if command_class.respond_to?(:mutate_request)
    command_class.mutate_request(self)
  end
end

#outcomeObject

[View source]

100
101
102
103
104
105
106
# File 'foobara-0.0.125/projects/command_connectors/src/command_connector/request.rb', line 100

def outcome
  if error
    Outcome.error(error)
  else
    command&.outcome
  end
end

#relevant_entity_classesObject

[View source]

116
117
118
119
120
121
# File 'foobara-0.0.125/projects/command_connectors/src/command_connector/request.rb', line 116

def relevant_entity_classes
  if command_class.is_a?(::Class) && command_class < TransformedCommand
    entity_classes = authenticator&.relevant_entity_classes(self)
    [*entity_classes, *relevant_entity_classes_from_inputs_transformer]
  end || []
end

#response_bodyObject

[View source]

83
84
85
86
87
88
89
90
91
92
93
94
# File 'foobara-0.0.125/projects/command_connectors/src/command_connector/request.rb', line 83

def response_body
  @response_body ||= begin
    # TODO: should we have separate ways to register success and failure serializers?
    body = success? ? result : error_collection

    if serializer
      serializer.process_value!(body)
    else
      body
    end
  end
end

#resultObject

[View source]

108
109
110
# File 'foobara-0.0.125/projects/command_connectors/src/command_connector/request.rb', line 108

def result
  outcome.result
end

#serializerObject

[View source]

66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'foobara-0.0.125/projects/command_connectors/src/command_connector/request.rb', line 66

def serializer
  return @serializer if defined?(@serializer)

  if serializers.nil? || serializers.empty?
    @serializer = nil
    return
  end

  actual_serializers = objects_to_serializers(serializers)

  @serializer = if actual_serializers.size == 1
                  actual_serializers.first
                else
                  Value::Processor::Pipeline.new(processors: actual_serializers)
                end
end

#success?Boolean

Returns:

  • (Boolean)
[View source]

96
97
98
# File 'foobara-0.0.125/projects/command_connectors/src/command_connector/request.rb', line 96

def success?
  outcome.success?
end