Class: PagePrint::RailsResourceFetcher

Inherits:
Object
  • Object
show all
Defined in:
lib/page_print/rails_resource_fetcher.rb

Constant Summary collapse

DEFAULT_ASSET_PREFIX =
'/assets'

Instance Method Summary collapse

Constructor Details

#initialize(rails: nil, asset_prefix: DEFAULT_ASSET_PREFIX) ⇒ RailsResourceFetcher

Returns a new instance of RailsResourceFetcher.

Raises:

  • (ArgumentError)


8
9
10
11
12
13
# File 'lib/page_print/rails_resource_fetcher.rb', line 8

def initialize(rails: nil, asset_prefix: DEFAULT_ASSET_PREFIX)
  @rails = rails || rails_constant
  @asset_prefix = normalize_asset_prefix(asset_prefix)

  raise ArgumentError, 'Rails is not available' unless @rails
end

Instance Method Details

#call(url) ⇒ Object



15
16
17
18
19
20
# File 'lib/page_print/rails_resource_fetcher.rb', line 15

def call(url)
  path = path_from_url(url)
  return unless path&.start_with?("#{@asset_prefix}/")

  read_public_asset(path) || read_resolved_asset(path)
end