# frozen_string_literal: true require 'media_types/serialization/base' module MediaTypes module Serialization module Serializers # The serializer used when no serializer has been configured. class FallbackNotAcceptableSerializer < MediaTypes::Serialization::Base unvalidated 'text/html' output_raw do |obj, version, context| available_types = [] begin original_uri = URI.parse(context.request.original_url) stripped_original = original_uri.dup query_parts = stripped_original.query&.split('&') || [] query_parts = query_parts.select { |q| !q.start_with? 'api_viewer=' } available_types = obj[:registrations].registrations.keys.map do |identifier| stripped_original.query = (query_parts + ["api_viewer=#{identifier}"]).join('&') { identifier: identifier, url: stripped_original.to_s, } end rescue URI::InvalidURIError available_types = obj[:registrations].registrations.keys.map do |identifier| { identifier: identifier, url: context.request.original_url, } end end input = OpenStruct.new( media_types: available_types, has_viewer: obj[:has_viewer], css: CommonCSS.css, acceptable_types: obj[:request].headers["Accept"] || "", ) template = ERB.new <<-TEMPLATE Unable to provide requested media types

Not acceptable

<% media_types.each do |m| %>
  • <%= CGI::escapeHTML(m[:identifier]) %>
  • <% end %>
    TEMPLATE template.result(input.instance_eval { binding }) end end end end end