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.


73
74
75
# File 'lib/shrine/plugins/derivation_endpoint.rb', line 73

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

#derivation_endpoint(**options) ⇒ Object

Returns a mountable Rack app that handles derivation requests.


41
42
43
# File 'lib/shrine/plugins/derivation_endpoint.rb', line 41

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

#derivation_optionsObject


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

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.


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# 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

#derivationsObject


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

def derivations
  opts[:derivation_endpoint][:derivations]
end