Class: Shrine::Derivation

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

Defined Under Namespace

Classes: Command, Delete, Generate, NotFound, Opened, Processed, Response, Retrieve, SourceNotFound, Upload, Url

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, args:, source:, options:) ⇒ Derivation

Returns a new instance of Derivation.



124
125
126
127
128
129
# File 'lib/shrine/plugins/derivation_endpoint.rb', line 124

def initialize(name:, args:, source:, options:)
  @name    = name.to_sym
  @args    = args
  @source  = source
  @options = options
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



122
123
124
# File 'lib/shrine/plugins/derivation_endpoint.rb', line 122

def args
  @args
end

#nameObject (readonly)

Returns the value of attribute name.



122
123
124
# File 'lib/shrine/plugins/derivation_endpoint.rb', line 122

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



122
123
124
# File 'lib/shrine/plugins/derivation_endpoint.rb', line 122

def options
  @options
end

#sourceObject (readonly)

Returns the value of attribute source.



122
123
124
# File 'lib/shrine/plugins/derivation_endpoint.rb', line 122

def source
  @source
end

Class Method Details

.option(name, default: nil, result: nil) ⇒ Object



186
187
188
# File 'lib/shrine/plugins/derivation_endpoint.rb', line 186

def self.option(name, default: nil, result: nil)
  options[name] = { default: default, result: result }
end

.optionsObject



182
183
184
# File 'lib/shrine/plugins/derivation_endpoint.rb', line 182

def self.options
  @options ||= {}
end

Instance Method Details

#deleteObject

Deletes the derivation result from the storage.



178
179
180
# File 'lib/shrine/plugins/derivation_endpoint.rb', line 178

def delete
  Derivation::Delete.new(self).call
end

#generate(file = nil) ⇒ Object

Calls the derivation block and returns the direct result.



155
156
157
# File 'lib/shrine/plugins/derivation_endpoint.rb', line 155

def generate(file = nil)
  Derivation::Generate.new(self).call(file)
end

#openedObject

Returns opened Shrine::UploadedFile object pointing to the uploaded derivative if it exists.



173
174
175
# File 'lib/shrine/plugins/derivation_endpoint.rb', line 173

def opened
  Derivation::Opened.new(self).call
end

#option(name) ⇒ Object

Retrieves the value of a derivation option.

  • If specified as a raw value, returns that value

  • If specified as a block, evaluates that it and returns the result

  • If unspecified, returns the default value



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/shrine/plugins/derivation_endpoint.rb', line 215

def option(name)
  option_definition = self.class.options.fetch(name)

  value = options.fetch(name) { shrine_class.derivation_options[name] }
  value = instance_exec(&value) if value.is_a?(Proc)

  if value.nil?
    default = option_definition[:default]
    value   = instance_exec(&default) if default
  end

  result = option_definition[:result]
  value  = instance_exec(value, &result) if result

  value
end

#processedObject

Returns the derivation result as a File/Tempfile or a Shrine::UploadedFile object.



150
151
152
# File 'lib/shrine/plugins/derivation_endpoint.rb', line 150

def processed
  Derivation::Processed.new(self).call
end

#response(env) ⇒ Object

Returns the derivation result in form of a Rack response triple.



144
145
146
# File 'lib/shrine/plugins/derivation_endpoint.rb', line 144

def response(env)
  Derivation::Response.new(self).call(env)
end

#retrieveObject

Returns a Shrine::UploadedFile object pointing to the uploaded derivative if it exists.



167
168
169
# File 'lib/shrine/plugins/derivation_endpoint.rb', line 167

def retrieve
  Derivation::Retrieve.new(self).call
end

#shrine_classObject



232
233
234
# File 'lib/shrine/plugins/derivation_endpoint.rb', line 232

def shrine_class
  source.shrine_class
end

#upload(file = nil, **options) ⇒ Object

Uploads the derivation result to a dedicated destination on the specified Shrine storage.



161
162
163
# File 'lib/shrine/plugins/derivation_endpoint.rb', line 161

def upload(file = nil, **options)
  Derivation::Upload.new(self).call(file, **options)
end

#url(**options) ⇒ Object

Returns an URL to the derivation.



132
133
134
135
136
137
138
139
140
141
# File 'lib/shrine/plugins/derivation_endpoint.rb', line 132

def url(**options)
  Derivation::Url.new(self).call(
    host:       option(:host),
    prefix:     option(:prefix),
    expires_in: option(:expires_in),
    version:    option(:version),
    metadata:   option(:metadata),
    **options,
  )
end