Class: Shrine::PresignEndpoint

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

Overview

Rack application that accepts GET request to the root URL, calls `#presign` on the specified storage, and returns that 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) ⇒ PresignEndpoint

Writes given options to instance variables.


67
68
69
70
71
# File 'lib/shrine/plugins/presign_endpoint.rb', line 67

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 GET 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.


79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/shrine/plugins/presign_endpoint.rb', line 79

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

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

    if request.get?
      handle_request(request)
    elsif request.options?
      handle_options_request(request)
    else
      error!(405, "Method Not Allowed")
    end
  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/presign_endpoint.rb', line 99

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