Class: Scrivito::Binary
- Inherits:
-
Object
- Object
- Scrivito::Binary
- Defined in:
- lib/scrivito/binary.rb
Overview
Instance Method Summary collapse
-
#content_length ⇒ Integer
The length of this binary data, in bytes.
-
#content_type ⇒ String
The content type of this binary data, for example “image/jpeg”.
-
#filename ⇒ String
The filename of this binary data, for example “my_image.jpg”.
-
#original ⇒ Scrivito::Binary
Returns the original version of a transformed binary.
-
#private? ⇒ Boolean
Some Scrivito data is considered private, i.e.
-
#transform(definition) ⇒ Scrivito::Binary
Use this method to transform images, i.e.
-
#transformed? ⇒ Boolean
Returns whether a binary has been transformed.
-
#url ⇒ String
The URL for accessing the binary data and downloading it using an HTTP GET request.
Instance Method Details
#content_length ⇒ Integer
The length of this binary data, in bytes.
74 75 76 |
# File 'lib/scrivito/binary.rb', line 74 def content_length headers[:content_length].to_i end |
#content_type ⇒ String
The content type of this binary data, for example “image/jpeg”.
67 68 69 |
# File 'lib/scrivito/binary.rb', line 67 def content_type headers[:content_type] end |
#filename ⇒ String
The filename of this binary data, for example “my_image.jpg”.
60 61 62 |
# File 'lib/scrivito/binary.rb', line 60 def filename File.basename(URI(url).path) end |
#original ⇒ Scrivito::Binary
Returns the original version of a transformed binary.
If a binary is the result of a transformation, the original version of the binary is returned. Otherwise self.
193 194 195 |
# File 'lib/scrivito/binary.rb', line 193 def original @original || self end |
#private? ⇒ Boolean
Some Scrivito data is considered private, i.e. it is not currently intended for the general public, for example content in a workspace that has not been published yet.
21 22 23 |
# File 'lib/scrivito/binary.rb', line 21 def private? !@is_public end |
#transform(definition) ⇒ Scrivito::Binary
Use this method to transform images, i.e. to scale down large images or to generate thumbnails of images. Only applicable if this Scrivito::Binary is an image.
This method does not change the binary. Instead, it returns a copy of it, transformed using the definition.
If the original binary has already been transformed, the returned binary will be a combination of the transformations. Thus, the transformations can be chained (see examples).
The transformed data is calculated “lazily”, so calling #transform does not trigger any calculation. The calculation is triggered only when data is accessed, for example via #url.
165 166 167 |
# File 'lib/scrivito/binary.rb', line 165 def transform(definition) self.class.new(id, public?, (transformation_definition || {}).merge(definition), original) end |
#transformed? ⇒ Boolean
Returns whether a binary has been transformed.
173 174 175 |
# File 'lib/scrivito/binary.rb', line 173 def transformed? !!transformation_definition end |
#url ⇒ String
The URL is calculated on demand, i.e. if the URL has not been cached yet, this method calls the Scrivito API to retrieve the URL. If you want to link to a Binary, consider using ControllerHelper#scrivito_url instead. This is generally much faster, as it performs the Scrivito API call asynchronously.
URLs for private content have an expiration time in order to protect them. Therefore, the URL should be accessed immediately after it has been returned (i.e. within a couple of minutes). Accessing it after expiration causes an error.
The URL for accessing the binary data and downloading it using an HTTP GET request.
The URLs should not be used for long-term storage since they are no longer accessible hours or days after they have been generated.
45 46 47 |
# File 'lib/scrivito/binary.rb', line 45 def url find_url('get') end |