Class: Shrine::Storage::Imgix

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/shrine/storage/imgix.rb

Constant Summary collapse

PURGE_URL =
"https://api.imgix.com/v2/image/purger"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(storage:, **options) ⇒ Imgix

We initialize the Imgix client, and save the storage. We additionally save the token as well, because ‘Imgix::Client` doesn’t provide a reader for the token.



16
17
18
19
20
# File 'lib/shrine/storage/imgix.rb', line 16

def initialize(storage:, **options)
  @client = ::Imgix::Client.new(options)
  @token = options[:token]
  @storage = storage
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



11
12
13
# File 'lib/shrine/storage/imgix.rb', line 11

def client
  @client
end

#storageObject (readonly)

Returns the value of attribute storage.



11
12
13
# File 'lib/shrine/storage/imgix.rb', line 11

def storage
  @storage
end

Instance Method Details

#delete(id) ⇒ Object

Purges the deleted file.



46
47
48
49
# File 'lib/shrine/storage/imgix.rb', line 46

def delete(id)
  @storage.delete(id)
  purge(id)
end

#movable?(io, id) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/shrine/storage/imgix.rb', line 32

def movable?(io, id)
  @storage.movable?(io, id) if @storage.respond_to?(:movable?)
end

#move(io, id, **options) ⇒ Object

Purges the file from the source storage after moving it.



27
28
29
30
# File 'lib/shrine/storage/imgix.rb', line 27

def move(io, id, **options)
  @storage.move(io, id, **options)
  io.storage.purge(io.id) if io.storage.is_a?(Storage::Imgix)
end

#multi_delete(ids) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/shrine/storage/imgix.rb', line 36

def multi_delete(ids)
  if @storage.respond_to?(:multi_delete)
    @storage.multi_delete(ids)
    ids.each { |id| purge(id) }
  else
    ids.each { |id| delete(id) }
  end
end

#purge(id) ⇒ Object

Removes the file from Imgix, along with the generated versions.



52
53
54
55
56
57
# File 'lib/shrine/storage/imgix.rb', line 52

def purge(id)
  uri = URI.parse(PURGE_URL)
  uri.user = @token

  post(uri, "url" => url(id))
end

#url(id, **options) ⇒ Object

Generates an Imgix URL to the file. All options passed in will be transformed into URL parameters, check out the [reference] for all available query parameters.

[reference]: www.imgix.com/docs/reference



64
65
66
# File 'lib/shrine/storage/imgix.rb', line 64

def url(id, **options)
  client.path(id).to_url(**options)
end