Class: Shrine::UploadEndpoint
- Inherits:
-
Object
- Object
- Shrine::UploadEndpoint
- 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
-
#call(env) ⇒ Object
Accepts a Rack env hash, routes POST requests to the root URL, and returns a Rack response triple.
-
#initialize(options) ⇒ UploadEndpoint
constructor
Writes given options to instance variables.
- #inspect ⇒ Object (also: #to_s)
Constructor Details
#initialize(options) ⇒ UploadEndpoint
Writes given options to instance variables.
74 75 76 77 78 |
# File 'lib/shrine/plugins/upload_endpoint.rb', line 74 def initialize() .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.
86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/shrine/plugins/upload_endpoint.rb', line 86 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 |
#inspect ⇒ Object Also known as: to_s
101 102 103 |
# File 'lib/shrine/plugins/upload_endpoint.rb', line 101 def inspect "#<#{@shrine_class}::UploadEndpoint(:#{@storage_key})>" end |