Module: Shrine::Plugins::UploadEndpoint::ClassMethods

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

Instance Method Summary collapse

Instance Method Details

#upload_endpoint(storage_key, **options) ⇒ Object

Returns a Rack application (object that responds to ‘#call`) which accepts multipart POST requests to the root URL, uploads given file to the specified storage, and returns that information in JSON format.

The ‘storage_key` needs to be one of the registered Shrine storages. Additional options can be given to override the options given on plugin initialization.



31
32
33
34
35
36
37
38
# File 'lib/shrine/plugins/upload_endpoint.rb', line 31

def upload_endpoint(storage_key, **options)
  Shrine::UploadEndpoint.new(
    shrine_class: self,
    storage_key:  storage_key,
    **opts[:upload_endpoint],
    **options,
  )
end

#upload_response(storage_key, env, **options) ⇒ Object

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

It performs the same mounting logic that Rack and other web frameworks use, and is meant for cases where statically mounting the endpoint in the router isn’t enough.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/shrine/plugins/upload_endpoint.rb', line 46

def upload_response(storage_key, env, **options)
  script_name = env["SCRIPT_NAME"]
  path_info   = env["PATH_INFO"]

  begin
    env["SCRIPT_NAME"] += path_info
    env["PATH_INFO"]    = ""

    upload_endpoint(storage_key, **options).call(env)
  ensure
    env["SCRIPT_NAME"] = script_name
    env["PATH_INFO"]   = path_info
  end
end