Module: Imglab

Extended by:
Imglab
Included in:
Imglab
Defined in:
lib/imglab/url.rb,
lib/imglab/srcset.rb,
lib/imglab/version.rb

Defined Under Namespace

Modules: Color, Position, Sequence, Signature, Srcset, Url Classes: Source

Constant Summary collapse

FLUID_CLASSES =
[Array, Range].freeze
DEFAULT_DPRS =
[1, 2, 3, 4, 5, 6].freeze
DEFAULT_WIDTHS =
Sequence.sequence(100, 8192).freeze
VERSION =
"0.3.0"

Instance Method Summary collapse

Instance Method Details

#srcset(source, path, params = {}) ⇒ String

Returns a formatted srcset ‘string` with the specified parameters.

Examples:

Creating a srcset with three different device pixel ratios:

Imglab.srcset("assets", "example.jpeg", width: 500, dpr: [1, 2, 3]) #=> "https://assets.imglab-cdn.net/example.jpeg?width=500&dpr=1 1x,\n..."

Creating a srcset with three different device pixel ratios using a range:

Imglab.srcset("assets", "example.jpeg", width: 500, dpr: 1..3) #=> "https://assets.imglab-cdn.net/example.jpeg?width=500&dpr=1 1x,\n..."

Creating a srcset with three different width sizes:

Imglab.srcset("assets", "example.jpeg", width: [400, 800, 1200], format: "webp") #=> "https://assets.imglab-cdn.net/example.jpeg?width=400&format=webp 400w,\n..."

Creating a srcset with a range of width sizes:

Imglab.srcset("assets", "example.jpeg", width: 400..1200, format: "webp") #=> "https://assets.imglab-cdn.net/example.jpeg?width=400&format=webp 400w,\n..."

Parameters:

  • source (String, Imglab::Source)

    the source name or source object

  • path (String)

    the path where the resource is located

  • params (Hash) (defaults to: {})

    the query parameters that we want to use

Returns:

  • (String)

    the srcset value as a list of formatted URLs with the specified arguments

Raises:

  • (ArgumentError)

    when the source name or source parameter has a not expected type or params are using unexpected values



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/imglab/srcset.rb', line 25

def srcset(source, path, params = {})
  params = Srcset::Utils.normalize_params(params)

  width, height, dpr = params.values_at("width", "height", "dpr")

  case
  when is_fluid?(width)
    if is_fluid?(dpr)
      raise ArgumentError, "dpr as #{dpr.class} is not allowed when width is Array or Range"
    end

    srcset_width(source, path, params)
  when width || height
    if is_fluid?(height)
      raise ArgumentError, "height as #{height.class} is not allowed when width is not an Array or Range"
    end

    srcset_dpr(source, path, params.merge("dpr" => dprs(params)))
  else
    if is_fluid?(dpr)
      raise ArgumentError, "dpr as #{dpr.class} is not allowed without specifying width or height"
    end

    srcset_width(source, path, params.merge("width" => DEFAULT_WIDTHS))
  end
end

#url(source, path, params = {}) ⇒ String

Returns a formatted URL ‘string` with the specified arguments.

Examples:

Creating a URL specifying source name as string

Imglab.url("assets", "example.jpeg", width: 500, height: 600) #=> "https://assets.imglab-cdn.net/example.jpeg?width=500&height=600"

Creating a URL specifying a Imglab::Source

Imglab.url(Imglab::Source.new("assets"), "example.jpeg", width: 500, height: 600) #=> "https://assets.imglab-cdn.net/example.jpeg?width=500&height=600"

Parameters:

  • source (String, Imglab::Source)

    the source name or source object

  • path (String)

    the path where the resource is located

  • params (Hash) (defaults to: {})

    the query parameters that we want to use

Returns:

  • (String)

    the formatted URL with the specified arguments

Raises:

  • (ArgumentError)

    when the source name or source parameter has a not expected type



16
17
18
19
20
21
22
23
24
25
# File 'lib/imglab/url.rb', line 16

def url(source, path, params = {})
  case source
  when String
    url_for_source(Source.new(source), path, params)
  when Source
    url_for_source(source, path, params)
  else
    raise ArgumentError, "Invalid source name or source. A string or a #{Source.name} instance is expected"
  end
end