Class: Imgproxy::UrlAdapters

Inherits:
Object
  • Object
show all
Defined in:
lib/imgproxy/url_adapters.rb,
lib/imgproxy/url_adapters/shrine.rb,
lib/imgproxy/url_adapters/active_storage.rb

Overview

URL adapters config. Allows to use this gem with ActiveStorage, Shrine, etc.

Imgproxy.configure do |config|
  config.url_adapters.add Imgproxy::UrlAdapters::ActiveStorage.new
end

Imgproxy.url_for(user.avatar)

Defined Under Namespace

Classes: ActiveStorage, NotConfigured, NotFound, Shrine

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeUrlAdapters

Returns a new instance of UrlAdapters.



19
20
21
# File 'lib/imgproxy/url_adapters.rb', line 19

def initialize
  @adapters = []
end

Instance Attribute Details

#adaptersArray (readonly)

Returns Currently added adapters.

Returns:

  • (Array)

    Currently added adapters



17
18
19
# File 'lib/imgproxy/url_adapters.rb', line 17

def adapters
  @adapters
end

Instance Method Details

#add(adapter) ⇒ Array

Add adapter to the end of the list

Returns:

  • (Array)


25
26
27
# File 'lib/imgproxy/url_adapters.rb', line 25

def add(adapter)
  adapters << adapter
end

#clear!Array

Remove all adapters from the list

Returns:

  • (Array)


37
38
39
# File 'lib/imgproxy/url_adapters.rb', line 37

def clear!
  @adapters = []
end

#prependArray

Add adapter to the beginning of the list

Returns:

  • (Array)


31
32
33
# File 'lib/imgproxy/url_adapters.rb', line 31

def prepend
  adapters.unshift(adapter)
end

#url_of(image) ⇒ String

Get URL for the provided image

Returns:

  • (String)

Raises:



43
44
45
46
47
48
49
50
51
52
# File 'lib/imgproxy/url_adapters.rb', line 43

def url_of(image)
  return image if image.is_a? String
  return image.to_s if image.is_a? URI

  adapter = adapters.find { |a| a.applicable?(image) }

  return adapter.url(image) if adapter

  raise NotFound, "Can't found URL adapter for #{image.inspect}"
end