Class: ServiceWorker::Route

Inherits:
Object
  • Object
show all
Defined in:
lib/serviceworker/route.rb

Defined Under Namespace

Classes: AssetResolver, RouteMatch

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path_pattern, asset_pattern = nil, options = {}) ⇒ Route

Returns a new instance of Route.



17
18
19
20
21
22
23
24
25
26
# File 'lib/serviceworker/route.rb', line 17

def initialize(path_pattern, asset_pattern = nil, options = {})
  if asset_pattern.is_a?(Hash)
    options = asset_pattern
    asset_pattern = nil
  end

  @path_pattern = path_pattern
  @asset_pattern = asset_pattern || options.fetch(:asset, path_pattern)
  @options = options
end

Instance Attribute Details

#asset_patternObject (readonly)

Returns the value of attribute asset_pattern.



5
6
7
# File 'lib/serviceworker/route.rb', line 5

def asset_pattern
  @asset_pattern
end

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/serviceworker/route.rb', line 5

def options
  @options
end

#path_patternObject (readonly)

Returns the value of attribute path_pattern.



5
6
7
# File 'lib/serviceworker/route.rb', line 5

def path_pattern
  @path_pattern
end

Class Method Details

.sprockets?(options) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/serviceworker/route.rb', line 13

def self.sprockets?(options)
  options.key?(:asset)
end

Instance Method Details

#headersObject



36
37
38
# File 'lib/serviceworker/route.rb', line 36

def headers
  @options.fetch(:headers, {})
end

#match(path) ⇒ Object

Raises:

  • (ArgumentError)


28
29
30
31
32
33
34
# File 'lib/serviceworker/route.rb', line 28

def match(path)
  raise ArgumentError, "path is required" if path.to_s.strip.empty?

  (asset = resolver.call(path)) || (return nil)

  RouteMatch.new(path, asset, headers, options)
end