Class: ServiceWorker::Router

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

Constant Summary collapse

PATH_INFO =
"PATH_INFO"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Router

standard:disable Style/ArgumentsForwarding



14
15
16
17
18
# File 'lib/serviceworker/router.rb', line 14

def initialize(&block)
  @routes = []

  draw(&block)
end

Instance Attribute Details

#routesObject (readonly)

Returns the value of attribute routes.



11
12
13
# File 'lib/serviceworker/router.rb', line 11

def routes
  @routes
end

Class Method Details

.defaultObject



7
8
9
# File 'lib/serviceworker/router.rb', line 7

def self.default
  new.draw_default
end

Instance Method Details

#any?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/serviceworker/router.rb', line 50

def any?
  @routes.any?
end

#draw(&block) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/serviceworker/router.rb', line 20

def draw(&block)
  return self unless block

  if block.arity == 1
    yield(self)
  else
    instance_eval(&block)
  end

  self
end

#draw_defaultObject

standard:enable Style/ArgumentsForwarding



33
34
35
# File 'lib/serviceworker/router.rb', line 33

def draw_default
  draw { get "/serviceworker.js" }
end

#match(path, *args) ⇒ Object Also known as: get



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/serviceworker/router.rb', line 37

def match(path, *args)
  if path.is_a?(Hash)
    opts = path.to_a
    path, asset = opts.shift
    args = [asset, opts.to_h]
  end

  Route.new(path, *args).tap do |route|
    @routes << route
  end
end

#match_route(env) ⇒ Object



54
55
56
57
# File 'lib/serviceworker/router.rb', line 54

def match_route(env)
  path = env[PATH_INFO]
  @routes.lazy.map { |route| route.match(path) }.detect(&:itself)
end