Class: Wurk::Web::Extension::App

Inherits:
Object
  • Object
show all
Defined in:
lib/wurk/web/extension.rb

Overview

Captures the routes + helpers an extension declares in registered. Quacks like the slice of Sidekiq::Web::Application extensions touch.

Constant Summary collapse

HTTP_METHODS =
%i[get post put patch delete head].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize ⇒ App

Returns a new instance of App.



37
38
39
40
41
# File 'lib/wurk/web/extension.rb', line 37

def initialize
  @routes = [] # [[method_string, Route, block], …]
  @helper_modules = []
  @helper_blocks = []
end

Instance Attribute Details

#helper_blocks ⇒ Object (readonly)

Returns the value of attribute helper_blocks.



33
34
35
# File 'lib/wurk/web/extension.rb', line 33

def helper_blocks
  @helper_blocks
end

#helper_modules ⇒ Object (readonly)

Returns the value of attribute helper_modules.



33
34
35
# File 'lib/wurk/web/extension.rb', line 33

def helper_modules
  @helper_modules
end

#routes ⇒ Object (readonly)

Returns the value of attribute routes.



33
34
35
# File 'lib/wurk/web/extension.rb', line 33

def routes
  @routes
end

Instance Method Details

#configure ⇒ Object



84
# File 'lib/wurk/web/extension.rb', line 84

def configure(*) = (yield self if block_given?)

#helpers(*mods, &block) ⇒ Object

Sidekiq extensions add helpers via app.helpers(Mod) and/or a block.



75
76
77
78
79
# File 'lib/wurk/web/extension.rb', line 75

def helpers(*mods, &block)
  @helper_modules.concat(mods)
  @helper_blocks << block if block
  self
end

#match(env) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/wurk/web/extension.rb', line 54

def match(env)
  require 'cgi'
  @routes.each do |method, route, block|
    next unless method == env['REQUEST_METHOD']

    params = route.match(env['PATH_INFO'])
    next unless params

    context = env.fetch('wurk.ext.context', {})
    ext = context.fetch(:ext, {}).merge(helpers: { modules: helper_modules, blocks: helper_blocks })
    return Action.new(env: env, route_params: params, block: block, ext: ext,
                      mount: context[:mount], embed: context.fetch(:embed, true))
  end
  nil
end

#route(*methods, path, &block) ⇒ Object



49
50
51
52
# File 'lib/wurk/web/extension.rb', line 49

def route(*methods, path, &block)
  matcher = Route.new(path)
  methods.each { |method| @routes << [method.to_s.upcase, matcher, block] }
end

#route?(method, path) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/wurk/web/extension.rb', line 70

def route?(method, path)
  @routes.any? { |verb, route, _block| verb == method && route.match(path) }
end

#set ⇒ Object

Sinatra settings calls some extensions make — accepted no-op.



82
# File 'lib/wurk/web/extension.rb', line 82

def set(*); end

#settings ⇒ Object



83
# File 'lib/wurk/web/extension.rb', line 83

def settings = self