Class: Foobara::CommandConnectors::Http
- Inherits:
-
Foobara::CommandConnector
- Object
- Foobara::CommandConnector
- Foobara::CommandConnectors::Http
- Includes:
- TruncatedInspect
- Defined in:
- foobara-http-command-connector-1.1.5/src/http.rb,
foobara-http-command-connector-1.1.5/src/http/cookie.rb,
foobara-http-command-connector-1.1.5/src/http/request.rb,
foobara-http-command-connector-1.1.5/src/http/response.rb,
foobara-http-command-connector-1.1.5/src/http/commands/help.rb,
foobara-http-command-connector-1.1.5/src/http/commands/describe.rb,
foobara-http-command-connector-1.1.5/src/http/commands/get_options.rb,
foobara-http-command-connector-1.1.5/src/http/commands/help/presenter.rb,
foobara-http-command-connector-1.1.5/src/http/desugarizers/default_inputs.rb,
foobara-http-command-connector-1.1.5/src/http/commands/help/presenter/root.rb,
foobara-http-command-connector-1.1.5/src/http/commands/help/presenter/type.rb,
foobara-http-command-connector-1.1.5/src/http/response_mutators/set_header.rb,
foobara-http-command-connector-1.1.5/src/http/commands/help/presenter/error.rb,
foobara-http-command-connector-1.1.5/src/http/commands/help/presenter/model.rb,
foobara-http-command-connector-1.1.5/src/http/commands/help/presenter/domain.rb,
foobara-http-command-connector-1.1.5/src/http/commands/help/presenter/entity.rb,
foobara-http-command-connector-1.1.5/src/http/commands/help/presenter/command.rb,
foobara-http-command-connector-1.1.5/src/http/commands/help/result_serializer.rb,
foobara-http-command-connector-1.1.5/src/http/commands/help/presenter/processor.rb,
foobara-http-command-connector-1.1.5/src/http/commands/help/presenter/organization.rb,
foobara-http-command-connector-1.1.5/src/http/desugarizers/set_input_to_proc_result.rb,
foobara-http-command-connector-1.1.5/src/http/commands/help/presenter/request_failed.rb,
foobara-http-command-connector-1.1.5/src/http/request_mutators/set_input_from_cookie.rb,
foobara-http-command-connector-1.1.5/src/http/request_mutators/set_input_from_header.rb,
foobara-http-command-connector-1.1.5/src/http/commands/help/presenter/processor_class.rb,
foobara-http-command-connector-1.1.5/src/http/request_mutators/set_input_to_proc_result.rb,
foobara-http-command-connector-1.1.5/src/http/response_mutators/move_attribute_to_cookie.rb,
foobara-http-command-connector-1.1.5/src/http/response_mutators/move_attribute_to_header.rb,
foobara-http-command-connector-1.1.5/src/http/request_mutators/default_inputs_request_mutator.rb,
foobara-rack-connector-0.1.1/lib/foobara/command_connectors/http/rack.rb,
foobara-rack-connector-0.1.1/lib/foobara/command_connectors/http/puma_runner.rb,
foobara-rack-connector-0.1.1/lib/foobara/command_connectors/http/rack/request.rb,
foobara-rack-connector-0.1.1/lib/foobara/command_connectors/http/rack/response.rb
Direct Known Subclasses
Defined Under Namespace
Modules: Commands, Desugarizers Classes: Cookie, DefaultInputsRequestMutator, MoveAttributeToCookie, MoveAttributeToHeader, Rack, Request, Response, SetHeader, SetInputFromCookie, SetInputFromHeader, SetInputToProcResult
Constant Summary
Constants included from TruncatedInspect
Class Attribute Summary collapse
Instance Attribute Summary collapse
-
#prefix ⇒ Object
Returns the value of attribute prefix.
Attributes inherited from Foobara::CommandConnector
#auth_map, #authenticator, #capture_unknown_error, #command_registry, #name, #requires_allowed_rule
Instance Method Summary collapse
- #headers_for(request) ⇒ Object
-
#initialize(prefix: nil, default_serializers: self.class.default_serializers) ⇒ Http
constructor
A new instance of Http.
- #mutate_response(response) ⇒ Object
- #request_to_command_class(request) ⇒ Object
- #request_to_command_inputs(request) ⇒ Object
- #run ⇒ Object
- #set_response_status(response) ⇒ Object
Methods included from TruncatedInspect
Methods inherited from Foobara::CommandConnector
#add_default_errors_transformer, #add_default_inputs_transformer, #add_default_pre_commit_transformer, #add_default_response_mutator, #add_default_result_transformer, #add_default_serializer, #all_exposed_command_names, #all_exposed_commands, #all_transformed_command_classes, #allow, #allowed_rule, #allowed_rules, allowed_rules_to_register, always_allowed_rule, authenticator_registry, build_auth_mapper, #command, #command_connected?, #connect, #each_transformed_command_class, find_builtin_command_class, #find_builtin_command_class, #lookup_command, register_allowed_rule, #register_allowed_rule, register_authenticator, to_auth_user_mapper, to_authenticator, #transformed_command_from_name, #type_from_name
Methods included from Foobara::CommandConnector::Concerns::Reflection
#all_exposed_type_names, #foobara_manifest
Methods included from Foobara::Concern
foobara_class_methods_module_for, foobara_concern?, included
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 '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_serializers ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File '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
#prefix ⇒ Object
Returns the value of attribute prefix.
31 32 33 |
# File 'src/http.rb', line 31 def prefix @prefix end |
Instance Method Details
#headers_for(request) ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'src/http.rb', line 119 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 request.action == "help" response_headers = (response_headers || {}).merge("content-type" => "text/html") end if response_headers static_headers.merge(response_headers) else static_headers.dup end end |
#mutate_response(response) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 |
# File 'src/http.rb', line 107 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_class(request) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 |
# File 'src/http.rb', line 61 def request_to_command_class(request) if request.method == "OPTIONS" Foobara::CommandConnectors::Http::Commands::GetOptions else super.tap do |command_class| if request.action == "help" command_class.serializers = [Commands::Help::ResultSerializer] end end end end |
#request_to_command_inputs(request) ⇒ Object
73 74 75 76 77 78 79 |
# File 'src/http.rb', line 73 def request_to_command_inputs(request) if request.method == "OPTIONS" { request: } else super end end |
#run ⇒ Object
57 58 59 |
# File 'src/http.rb', line 57 def run(*, **) super(*, prefix:, **) end |
#set_response_status(response) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'src/http.rb', line 81 def set_response_status(response) outcome = response.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 |