Module: Shrine::Plugins::DerivationEndpoint::ClassMethods

Defined in:
lib/shrine/plugins/derivation_endpoint.rb

Instance Method Summary collapse

Instance Method Details

#derivation(name, &block) ⇒ Object

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



78
79
80
# File 'lib/shrine/plugins/derivation_endpoint.rb', line 78

def derivation(name, &block)
  derivations[name] = block
end

#derivation_endpoint(**options) ⇒ Object

Returns a mountable Rack app that handles derivation requests.



46
47
48
# File 'lib/shrine/plugins/derivation_endpoint.rb', line 46

def derivation_endpoint(**options)
  Shrine::DerivationEndpoint.new(shrine_class: self, options: options)
end

#derivation_optionsObject



86
87
88
# File 'lib/shrine/plugins/derivation_endpoint.rb', line 86

def derivation_options
  opts[:derivation_endpoint_options]
end

#derivation_response(env, **options) ⇒ Object

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.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/shrine/plugins/derivation_endpoint.rb', line 56

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

#derivationsObject



82
83
84
# File 'lib/shrine/plugins/derivation_endpoint.rb', line 82

def derivations
  opts[:derivation_endpoint_derivations]
end