Class: Shrine::UploadEndpoint

Inherits:
Object
  • Object
show all
Defined in:
lib/shrine/plugins/upload_endpoint.rb

Overview

Rack application that accepts multipart POST request to the root URL, calls ‘#upload` with the uploaded file, and returns the uploaded file information in JSON format.

Constant Summary collapse

CONTENT_TYPE_JSON =
"application/json; charset=utf-8"
CONTENT_TYPE_TEXT =
"text/plain"

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ UploadEndpoint

Writes given options to instance variables.



72
73
74
75
76
# File 'lib/shrine/plugins/upload_endpoint.rb', line 72

def initialize(options)
  options.each do |name, value|
    instance_variable_set("@#{name}", value)
  end
end

Instance Method Details

#call(env) ⇒ Object

Accepts a Rack env hash, routes POST requests to the root URL, and returns a Rack response triple.

If request isn’t to the root URL, a ‘404 Not Found` response is returned. If request verb isn’t GET, a ‘405 Method Not Allowed` response is returned.



84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/shrine/plugins/upload_endpoint.rb', line 84

def call(env)
  request = Rack::Request.new(env)

  status, headers, body = catch(:halt) do
    error!(404, "Not Found") unless ["", "/"].include?(request.path_info)
    error!(405, "Method Not Allowed") unless request.post?

    handle_request(request)
  end

  headers["Content-Length"] ||= body.map(&:bytesize).inject(0, :+).to_s

  [status, headers, body]
end

#inspectObject Also known as: to_s



99
100
101
# File 'lib/shrine/plugins/upload_endpoint.rb', line 99

def inspect
  "#<#{@shrine_class}::UploadEndpoint(:#{@storage_key})>"
end