Class: Foobara::CommandConnectors::Http

Inherits:
Foobara::CommandConnector show all
Includes:
TruncatedInspect
Defined in:
foobara-http-command-connector-0.0.23/src/http.rb,
foobara-http-command-connector-0.0.23/src/http/cookie.rb,
foobara-http-command-connector-0.0.23/src/http/request.rb,
foobara-http-command-connector-0.0.23/src/http/response.rb,
foobara-http-command-connector-0.0.23/src/http/commands/help.rb,
foobara-http-command-connector-0.0.23/src/http/commands/describe.rb,
foobara-http-command-connector-0.0.23/src/http/commands/get_options.rb,
foobara-http-command-connector-0.0.23/src/http/commands/help/presenter.rb,
foobara-http-command-connector-0.0.23/src/http/commands/help/presenter/root.rb,
foobara-http-command-connector-0.0.23/src/http/commands/help/presenter/type.rb,
foobara-http-command-connector-0.0.23/src/http/response_mutators/set_header.rb,
foobara-http-command-connector-0.0.23/src/http/commands/help/presenter/error.rb,
foobara-http-command-connector-0.0.23/src/http/commands/help/presenter/model.rb,
foobara-http-command-connector-0.0.23/src/http/commands/help/presenter/domain.rb,
foobara-http-command-connector-0.0.23/src/http/commands/help/presenter/entity.rb,
foobara-http-command-connector-0.0.23/src/http/commands/help/presenter/command.rb,
foobara-http-command-connector-0.0.23/src/http/commands/help/result_serializer.rb,
foobara-http-command-connector-0.0.23/src/http/commands/help/presenter/processor.rb,
foobara-http-command-connector-0.0.23/src/http/commands/help/presenter/organization.rb,
foobara-http-command-connector-0.0.23/src/http/commands/help/presenter/request_failed.rb,
foobara-http-command-connector-0.0.23/src/http/request_mutators/set_input_from_cookie.rb,
foobara-http-command-connector-0.0.23/src/http/request_mutators/set_input_from_header.rb,
foobara-http-command-connector-0.0.23/src/http/commands/help/presenter/processor_class.rb,
foobara-http-command-connector-0.0.23/src/http/response_mutators/move_attribute_to_cookie.rb,
foobara-http-command-connector-0.0.23/src/http/response_mutators/move_attribute_to_header.rb,
foobara-rack-connector-0.0.10/lib/foobara/command_connectors/http/rack.rb,
foobara-rack-connector-0.0.10/lib/foobara/command_connectors/http/puma_runner.rb,
foobara-rack-connector-0.0.10/lib/foobara/command_connectors/http/rack/request.rb

Direct Known Subclasses

Rack

Defined Under Namespace

Modules: Commands Classes: Cookie, MoveAttributeToCookie, MoveAttributeToHeader, Rack, Request, Response, SetHeader, SetInputFromCookie, SetInputFromHeader

Constant Summary

Constants included from TruncatedInspect

TruncatedInspect::MAX_LENGTH

Class Attribute Summary collapse

Instance Attribute Summary collapse

Attributes inherited from Foobara::CommandConnector

#authenticator, #capture_unknown_error, #command_registry

Instance Method Summary collapse

Methods included from TruncatedInspect

#inspect, truncating

Methods inherited from Foobara::CommandConnector

#all_exposed_commands, allowed_rules_to_register, #authenticate, authenticator_registry, #build_command, #build_request, #build_response, #connect, #connect_delayed, #delayed_connections, find_builtin_command_class, #find_builtin_command_class, #foobara_manifest, #lookup_command, #normalize_manifest, #patch_up_broken_parents_for_errors_with_missing_command_parents, #process_delayed_connections, register_allowed_rule, register_authenticator, #request_to_response, #run_command, #run_request, #serialize_response_body, #set_response_body, to_authenticator, #transform_command_class, #type_from_name

Constructor Details

#initialize(prefix: nil, default_serializers: self.class.default_serializers) ⇒ Http

Returns a new instance of Http.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'foobara-http-command-connector-0.0.23/src/http.rb', line 33

def initialize(
  prefix: nil,
  default_serializers: self.class.default_serializers,
  **
)
  if prefix
    if prefix.is_a?(::Array)
      prefix = prefix.join("/")
    end

    if prefix.end_with?("/")
      prefix = prefix[0..-2]
    end

    unless prefix.start_with?("/")
      prefix = "/#{prefix}"
    end

    self.prefix = prefix
  end

  super(default_serializers:, **)
end

Class Attribute Details

.default_serializersObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'foobara-http-command-connector-0.0.23/src/http.rb', line 9

def default_serializers
  return @default_serializers if @default_serializers

  superklass = superclass
  serializers = nil

  while superklass.respond_to?(:default_serializers)
    serializers = superclass.instance_variable_get(:@default_serializers)

    return serializers if serializers

    superklass = superklass.superclass
  end

  @default_serializers = [
    Foobara::CommandConnectors::Serializers::ErrorsSerializer,
    Foobara::CommandConnectors::Serializers::AtomicSerializer,
    Foobara::CommandConnectors::Serializers::JsonSerializer
  ]
end

Instance Attribute Details

#prefixObject

Returns the value of attribute prefix.



31
32
33
# File 'foobara-http-command-connector-0.0.23/src/http.rb', line 31

def prefix
  @prefix
end

Instance Method Details

#headers_for(request) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'foobara-http-command-connector-0.0.23/src/http.rb', line 115

def headers_for(request)
  response_headers = request.response_headers

  if response_headers.nil? || !response_headers.key?("content-type")
    if request.command.respond_to?(:serialize_result)
      # TODO: we should ask the request this not the command.
      if request.command.serializers.include?(Serializers::JsonSerializer)
        response_headers = (response_headers || {}).merge("content-type" => "application/json")
      end
    end
  end

  if response_headers
    static_headers.merge(response_headers)
  else
    static_headers.dup
  end
end

#mutate_response(response) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
# File 'foobara-http-command-connector-0.0.23/src/http.rb', line 103

def mutate_response(response)
  super

  headers = headers_for(response.request)

  if headers&.any?
    response.headers = (response.headers || {}).merge(headers)
  else
    response.headers ||= {}
  end
end

#request_to_command(request) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'foobara-http-command-connector-0.0.23/src/http.rb', line 61

def request_to_command(request)
  if request.method == "OPTIONS"
    return Foobara::CommandConnectors::Http::Commands::GetOptions.new(request:)
  end

  command = super

  if request.action == "help"
    request.set_response_header("content-type", "text/html")
    command.class.serializers = [Commands::Help::ResultSerializer]
  end

  command
end

#runObject



57
58
59
# File 'foobara-http-command-connector-0.0.23/src/http.rb', line 57

def run(*, **)
  super(*, prefix:, **)
end

#set_response_status(response) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'foobara-http-command-connector-0.0.23/src/http.rb', line 76

def set_response_status(response)
  command = response.command
  outcome = command.outcome

  response.status = if outcome.success?
                      200
                    else
                      errors = outcome.errors

                      if errors.size == 1
                        error = errors.first

                        case error
                        when CommandConnector::UnknownError
                          500
                        when CommandConnector::NotFoundError, Foobara::Entity::NotFoundError
                          # TODO: we should not be coupled to Entities here...
                          404
                        when CommandConnector::UnauthenticatedError
                          401
                        when CommandConnector::NotAllowedError
                          403
                        end
                      end || 422
                    end
end