Mange / roadie
1
# frozen_string_literal: true
2

3 5
module Roadie
4
  # @api public
5
  # This provider acts a bit like a pipeline in normal UNIX parlour by enabling
6
  # you to make changes to the requested path. Some uses of this include:
7
  #
8
  #   * Convert absolute URLs into local filesystem paths.
9
  #   * Convert between external DNS name into internal naming.
10
  #   * Changing path structure of filenames.
11
  #   * Removing digests from filenames.
12
  #   * Handle query string logic.
13
  #   * Skipping known-bad paths.
14
  #
15
  # There might be other useful things you could use it for. The basic premise
16
  # is that a path is sent in to this provider, maybe modified and then passed
17
  # on to the "upstream" provider (or {ProviderList}).
18
  #
19
  # If the block returns {nil} or {false}, the upstream provider will not be
20
  # invoked and it will be treated as "not found". This makes it possible to
21
  # use this provider as a filter only.
22
  #
23
  # @example Simple regex
24
  #   provider = Roadie::PathRewriterProvider.new(other_provider) { |path|
25
  #     path.gsub(/-[a-f0-9]+\.css$/, '.css')
26
  #   }
27
  #
28
  # @example Filtering assets
29
  #   # Only assets containing "email" in the path will be considered by other_provider
30
  #   only_email_provider = Roadie::PathRewriterProvider.new(other_provider) { |path|
31
  #     path =~ /email/ ? path : nil
32
  #   }
33
  #
34
  # @example Handling "external" app assets as local assets
35
  #   document.external_asset_providers = [
36
  #     # Look for assets from "myapp.com" just like if we just specified a local path
37
  #     Roadie::PathRewriterProvider.new(document.asset_providers) { |url|
38
  #       uri = URI.parse(url)
39
  #       uri.path if uri.host == "myapp.com"
40
  #     },
41
  #     # Any other asset should be downloaded like normal
42
  #     Roadie::NetHttpProvider.new
43
  #   ]
44 5
  class PathRewriterProvider
45 5
    attr_reader :provider, :filter
46

47 5
    def initialize(provider, &filter)
48 5
      @provider = provider
49 5
      @filter = filter
50
    end
51

52 5
    def find_stylesheet(path)
53 5
      new_path = filter.call(path)
54 5
      provider.find_stylesheet(new_path) if new_path
55
    end
56

57 5
    def find_stylesheet!(path)
58 5
      new_path = filter.call(path)
59 5
      if new_path
60 5
        provider.find_stylesheet!(new_path)
61
      else
62 5
        raise CssNotFound, "Filter returned #{new_path.inspect}"
63
      end
64
    end
65
  end
66
end

Read our documentation on viewing source code .

Loading