module Shrine::Plugins::DerivationEndpoint::ClassMethods

Public Instance Methods

derivation(name, &block) click to toggle source

Registers a derivation block, which is called when the corresponding derivation URL is requested.

# File lib/shrine/plugins/derivation_endpoint.rb, line 73
def derivation(name, &block)
  derivations[name.to_sym] = block
end
derivation_endpoint(**options) click to toggle source

Returns a mountable Rack app that handles derivation requests.

# File lib/shrine/plugins/derivation_endpoint.rb, line 41
def derivation_endpoint(**options)
  Shrine::DerivationEndpoint.new(shrine_class: self, options: options)
end
derivation_options() click to toggle source
# File lib/shrine/plugins/derivation_endpoint.rb, line 81
def derivation_options
  opts[:derivation_endpoint][:options]
end
derivation_response(env, **options) click to toggle source

Calls the derivation endpoint passing the request information, and returns the Rack response triple.

It uses a trick where it removes the derivation path prefix from the path info before calling the Rack app, which is what web framework routers do before they’re calling a mounted Rack app.

# File lib/shrine/plugins/derivation_endpoint.rb, line 51
def derivation_response(env, **options)
  script_name = env["SCRIPT_NAME"]
  path_info   = env["PATH_INFO"]

  prefix = derivation_options[:prefix]
  match  = path_info.match(/^\/#{prefix}/)

  fail Error, "request path must start with \"/#{prefix}\", but is \"#{path_info}\"" unless match

  begin
    env["SCRIPT_NAME"] += match.to_s
    env["PATH_INFO"]    = match.post_match

    derivation_endpoint(**options).call(env)
  ensure
    env["SCRIPT_NAME"] = script_name
    env["PATH_INFO"]   = path_info
  end
end
derivations() click to toggle source
# File lib/shrine/plugins/derivation_endpoint.rb, line 77
def derivations
  opts[:derivation_endpoint][:derivations]
end