Class: Roadie::PathRewriterProvider

Inherits:
Object
  • Object
show all
Defined in:
lib/roadie/path_rewriter_provider.rb

Overview

This provider acts a bit like a pipeline in normal UNIX parlour by enabling you to make changes to the requested path. Some uses of this include:

* Convert absolute URLs into local filesystem paths.
* Convert between external DNS name into internal naming.
* Changing path structure of filenames.
* Removing digests from filenames.
* Handle query string logic.
* Skipping known-bad paths.

There might be other useful things you could use it for. The basic premise is that a path is sent in to this provider, maybe modified and then passed on to the “upstream” provider (or ProviderList).

If the block returns nil or false, the upstream provider will not be invoked and it will be treated as “not found”. This makes it possible to use this provider as a filter only.

Examples:

Simple regex

provider = Roadie::PathRewriterProvider.new(other_provider) { |path|
  path.gsub(/-[a-f0-9]+\.css$/, '.css')
}

Filtering assets

# Only assets containing "email" in the path will be considered by other_provider
only_email_provider = Roadie::PathRewriterProvider.new(other_provider) { |path|
  path =~ /email/ ? path : nil
}

Handling “external” app assets as local assets

document.external_asset_providers = [
  # Look for assets from "myapp.com" just like if we just specified a local path
  Roadie::PathRewriterProvider.new(document.asset_providers) { |url|
    uri = URI.parse(url)
    uri.path if uri.host == "myapp.com"
  },
  # Any other asset should be downloaded like normal
  Roadie::NetHttpProvider.new
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(provider, &filter) ⇒ PathRewriterProvider

Returns a new instance of PathRewriterProvider.



45
46
47
48
# File 'lib/roadie/path_rewriter_provider.rb', line 45

def initialize(provider, &filter)
  @provider = provider
  @filter = filter
end

Instance Attribute Details

#filterObject (readonly)



43
44
45
# File 'lib/roadie/path_rewriter_provider.rb', line 43

def filter
  @filter
end

#providerObject (readonly)



43
44
45
# File 'lib/roadie/path_rewriter_provider.rb', line 43

def provider
  @provider
end

Instance Method Details

#find_stylesheet(path) ⇒ Object



50
51
52
53
# File 'lib/roadie/path_rewriter_provider.rb', line 50

def find_stylesheet(path)
  new_path = filter.call(path)
  provider.find_stylesheet(new_path) if new_path
end

#find_stylesheet!(path) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/roadie/path_rewriter_provider.rb', line 55

def find_stylesheet!(path)
  new_path = filter.call(path)
  if new_path
    provider.find_stylesheet!(new_path)
  else
    raise CssNotFound, "Filter returned #{new_path.inspect}"
  end
end