|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +# Released under the MIT License. |
| 4 | +# Copyright, 2020-2025, by Samuel Williams. |
| 5 | + |
| 6 | +require_relative "middleware" |
| 7 | + |
| 8 | +module Utopia |
| 9 | + module Controller |
| 10 | + module Handlers |
| 11 | + module JSON |
| 12 | + APPLICATION_JSON = HTTP::Accept::ContentType.new("application", "json").freeze |
| 13 | + |
| 14 | + def self.split(*arguments) |
| 15 | + APPLICATION_JSON.split(*arguments) |
| 16 | + end |
| 17 | + |
| 18 | + def self.call(context, request, media_range, object, **options) |
| 19 | + if version = media_range.parameters["version"] |
| 20 | + options[:version] = version.to_s |
| 21 | + end |
| 22 | + |
| 23 | + context.succeed! content: object.to_json(options), type: APPLICATION_JSON |
| 24 | + end |
| 25 | + end |
| 26 | + |
| 27 | + module Passthrough |
| 28 | + WILDCARD = HTTP::Accept::MediaTypes::MediaRange.new("*", "*").freeze |
| 29 | + |
| 30 | + def self.split(*arguments) |
| 31 | + WILDCARD.split(*arguments) |
| 32 | + end |
| 33 | + |
| 34 | + def self.call(context, request, media_range, object, **options) |
| 35 | + # Do nothing. |
| 36 | + end |
| 37 | + end |
| 38 | + end |
| 39 | + |
| 40 | + class Responder |
| 41 | + Handler = Struct.new(:content_type, :block) do |
| 42 | + def split(*arguments) |
| 43 | + self.content_type.split(*arguments) |
| 44 | + end |
| 45 | + |
| 46 | + def call(context, request, media_range, *arguments, **options) |
| 47 | + context.instance_exec(media_range, *arguments, **options, &self.block) |
| 48 | + end |
| 49 | + end |
| 50 | + |
| 51 | + Responds = Struct.new(:responder, :context, :request) do |
| 52 | + # @todo Refactor `object` -> `*arguments`... |
| 53 | + def with(object, **options) |
| 54 | + responder.call(context, request, object, **options) |
| 55 | + end |
| 56 | + end |
| 57 | + |
| 58 | + def initialize |
| 59 | + @handlers = HTTP::Accept::MediaTypes::Map.new |
| 60 | + end |
| 61 | + |
| 62 | + attr :handlers |
| 63 | + |
| 64 | + def freeze |
| 65 | + @handlers.freeze |
| 66 | + |
| 67 | + super |
| 68 | + end |
| 69 | + |
| 70 | + def call(context, request, *arguments, **options) |
| 71 | + # Parse the list of browser preferred content types and return ordered by priority: |
| 72 | + media_types = HTTP::Accept::MediaTypes.browser_preferred_media_types(request.env) |
| 73 | + |
| 74 | + handler, media_range = @handlers.for(media_types) |
| 75 | + |
| 76 | + if handler |
| 77 | + handler.call(context, request, media_range, *arguments, **options) |
| 78 | + end |
| 79 | + end |
| 80 | + |
| 81 | + # Add a converter for the specified content type. Call the block with the response content if the request accepts the specified content_type. |
| 82 | + def handle(content_type, &block) |
| 83 | + @handlers << Handler.new(content_type, block) |
| 84 | + end |
| 85 | + |
| 86 | + def respond_to(context, request) |
| 87 | + Responds.new(self, context, request) |
| 88 | + end |
| 89 | + |
| 90 | + def with_json |
| 91 | + @handlers << Handlers::JSON |
| 92 | + end |
| 93 | + |
| 94 | + def with_passthrough |
| 95 | + @handlers << Handlers::Passthrough |
| 96 | + end |
| 97 | + |
| 98 | + def with(content_type, &block) |
| 99 | + handle(content_type, &block) |
| 100 | + end |
| 101 | + end |
| 102 | + end |
| 103 | +end |
0 commit comments