ultra-lite-hub 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/roda-3.105.0/MIT-LICENSE +21 -0
- data/roda-3.105.0/lib/roda/cache.rb +42 -0
- data/roda-3.105.0/lib/roda/plugins/Integer_matcher_max.rb +55 -0
- data/roda-3.105.0/lib/roda/plugins/_after_hook.rb +11 -0
- data/roda-3.105.0/lib/roda/plugins/_base64.rb +34 -0
- data/roda-3.105.0/lib/roda/plugins/_before_hook.rb +41 -0
- data/roda-3.105.0/lib/roda/plugins/_optimized_matching.rb +227 -0
- data/roda-3.105.0/lib/roda/plugins/_symbol_class_matchers.rb +108 -0
- data/roda-3.105.0/lib/roda/plugins/_symbol_regexp_matchers.rb +24 -0
- data/roda-3.105.0/lib/roda/plugins/additional_render_engines.rb +61 -0
- data/roda-3.105.0/lib/roda/plugins/additional_view_directories.rb +66 -0
- data/roda-3.105.0/lib/roda/plugins/all_verbs.rb +50 -0
- data/roda-3.105.0/lib/roda/plugins/assets.rb +902 -0
- data/roda-3.105.0/lib/roda/plugins/assets_preloading.rb +89 -0
- data/roda-3.105.0/lib/roda/plugins/assume_ssl.rb +28 -0
- data/roda-3.105.0/lib/roda/plugins/autoload_hash_branches.rb +79 -0
- data/roda-3.105.0/lib/roda/plugins/autoload_named_routes.rb +65 -0
- data/roda-3.105.0/lib/roda/plugins/backtracking_array.rb +88 -0
- data/roda-3.105.0/lib/roda/plugins/bearer_token.rb +28 -0
- data/roda-3.105.0/lib/roda/plugins/branch_locals.rb +74 -0
- data/roda-3.105.0/lib/roda/plugins/break.rb +43 -0
- data/roda-3.105.0/lib/roda/plugins/caching.rb +206 -0
- data/roda-3.105.0/lib/roda/plugins/capture_erb.rb +94 -0
- data/roda-3.105.0/lib/roda/plugins/chunked.rb +331 -0
- data/roda-3.105.0/lib/roda/plugins/class_level_routing.rb +111 -0
- data/roda-3.105.0/lib/roda/plugins/class_matchers.rb +155 -0
- data/roda-3.105.0/lib/roda/plugins/common_logger.rb +80 -0
- data/roda-3.105.0/lib/roda/plugins/conditional_sessions.rb +67 -0
- data/roda-3.105.0/lib/roda/plugins/content_for.rb +103 -0
- data/roda-3.105.0/lib/roda/plugins/content_security_policy.rb +329 -0
- data/roda-3.105.0/lib/roda/plugins/cookie_flags.rb +157 -0
- data/roda-3.105.0/lib/roda/plugins/cookies.rb +51 -0
- data/roda-3.105.0/lib/roda/plugins/csrf.rb +81 -0
- data/roda-3.105.0/lib/roda/plugins/custom_block_results.rb +85 -0
- data/roda-3.105.0/lib/roda/plugins/custom_matchers.rb +89 -0
- data/roda-3.105.0/lib/roda/plugins/default_headers.rb +64 -0
- data/roda-3.105.0/lib/roda/plugins/default_status.rb +35 -0
- data/roda-3.105.0/lib/roda/plugins/delay_build.rb +18 -0
- data/roda-3.105.0/lib/roda/plugins/delegate.rb +77 -0
- data/roda-3.105.0/lib/roda/plugins/delete_empty_headers.rb +36 -0
- data/roda-3.105.0/lib/roda/plugins/direct_call.rb +38 -0
- data/roda-3.105.0/lib/roda/plugins/disallow_file_uploads.rb +38 -0
- data/roda-3.105.0/lib/roda/plugins/drop_body.rb +43 -0
- data/roda-3.105.0/lib/roda/plugins/each_part.rb +87 -0
- data/roda-3.105.0/lib/roda/plugins/early_hints.rb +25 -0
- data/roda-3.105.0/lib/roda/plugins/empty_root.rb +49 -0
- data/roda-3.105.0/lib/roda/plugins/environments.rb +79 -0
- data/roda-3.105.0/lib/roda/plugins/erb_h.rb +43 -0
- data/roda-3.105.0/lib/roda/plugins/error_email.rb +152 -0
- data/roda-3.105.0/lib/roda/plugins/error_handler.rb +133 -0
- data/roda-3.105.0/lib/roda/plugins/error_mail.rb +149 -0
- data/roda-3.105.0/lib/roda/plugins/exception_page.rb +445 -0
- data/roda-3.105.0/lib/roda/plugins/filter_common_logger.rb +46 -0
- data/roda-3.105.0/lib/roda/plugins/flash.rb +117 -0
- data/roda-3.105.0/lib/roda/plugins/h.rb +59 -0
- data/roda-3.105.0/lib/roda/plugins/halt.rb +108 -0
- data/roda-3.105.0/lib/roda/plugins/hash_branch_view_subdir.rb +76 -0
- data/roda-3.105.0/lib/roda/plugins/hash_branches.rb +145 -0
- data/roda-3.105.0/lib/roda/plugins/hash_matcher.rb +39 -0
- data/roda-3.105.0/lib/roda/plugins/hash_paths.rb +128 -0
- data/roda-3.105.0/lib/roda/plugins/hash_public.rb +125 -0
- data/roda-3.105.0/lib/roda/plugins/hash_public_cache.rb +91 -0
- data/roda-3.105.0/lib/roda/plugins/hash_routes.rb +304 -0
- data/roda-3.105.0/lib/roda/plugins/head.rb +99 -0
- data/roda-3.105.0/lib/roda/plugins/header_matchers.rb +88 -0
- data/roda-3.105.0/lib/roda/plugins/heartbeat.rb +41 -0
- data/roda-3.105.0/lib/roda/plugins/hmac_paths.rb +413 -0
- data/roda-3.105.0/lib/roda/plugins/hooks.rb +92 -0
- data/roda-3.105.0/lib/roda/plugins/host_authorization.rb +155 -0
- data/roda-3.105.0/lib/roda/plugins/host_routing.rb +190 -0
- data/roda-3.105.0/lib/roda/plugins/hsts.rb +35 -0
- data/roda-3.105.0/lib/roda/plugins/indifferent_params.rb +124 -0
- data/roda-3.105.0/lib/roda/plugins/inject_erb.rb +33 -0
- data/roda-3.105.0/lib/roda/plugins/invalid_request_body.rb +107 -0
- data/roda-3.105.0/lib/roda/plugins/ip_from_header.rb +34 -0
- data/roda-3.105.0/lib/roda/plugins/json.rb +119 -0
- data/roda-3.105.0/lib/roda/plugins/json_parser.rb +107 -0
- data/roda-3.105.0/lib/roda/plugins/link_to.rb +83 -0
- data/roda-3.105.0/lib/roda/plugins/mail_processor.rb +629 -0
- data/roda-3.105.0/lib/roda/plugins/mailer.rb +283 -0
- data/roda-3.105.0/lib/roda/plugins/map_matcher.rb +48 -0
- data/roda-3.105.0/lib/roda/plugins/match_affix.rb +64 -0
- data/roda-3.105.0/lib/roda/plugins/match_hook.rb +34 -0
- data/roda-3.105.0/lib/roda/plugins/match_hook_args.rb +93 -0
- data/roda-3.105.0/lib/roda/plugins/middleware.rb +225 -0
- data/roda-3.105.0/lib/roda/plugins/middleware_stack.rb +101 -0
- data/roda-3.105.0/lib/roda/plugins/module_include.rb +96 -0
- data/roda-3.105.0/lib/roda/plugins/multi_public.rb +95 -0
- data/roda-3.105.0/lib/roda/plugins/multi_route.rb +156 -0
- data/roda-3.105.0/lib/roda/plugins/multi_run.rb +144 -0
- data/roda-3.105.0/lib/roda/plugins/multi_view.rb +83 -0
- data/roda-3.105.0/lib/roda/plugins/multibyte_string_matcher.rb +57 -0
- data/roda-3.105.0/lib/roda/plugins/named_routes.rb +167 -0
- data/roda-3.105.0/lib/roda/plugins/named_templates.rb +102 -0
- data/roda-3.105.0/lib/roda/plugins/not_allowed.rb +149 -0
- data/roda-3.105.0/lib/roda/plugins/not_found.rb +55 -0
- data/roda-3.105.0/lib/roda/plugins/optimized_segment_matchers.rb +53 -0
- data/roda-3.105.0/lib/roda/plugins/optimized_string_matchers.rb +50 -0
- data/roda-3.105.0/lib/roda/plugins/padrino_render.rb +42 -0
- data/roda-3.105.0/lib/roda/plugins/param_matchers.rb +83 -0
- data/roda-3.105.0/lib/roda/plugins/params_capturing.rb +116 -0
- data/roda-3.105.0/lib/roda/plugins/part.rb +76 -0
- data/roda-3.105.0/lib/roda/plugins/partials.rb +74 -0
- data/roda-3.105.0/lib/roda/plugins/pass.rb +47 -0
- data/roda-3.105.0/lib/roda/plugins/path.rb +255 -0
- data/roda-3.105.0/lib/roda/plugins/path_matchers.rb +54 -0
- data/roda-3.105.0/lib/roda/plugins/path_rewriter.rb +111 -0
- data/roda-3.105.0/lib/roda/plugins/permissions_policy.rb +336 -0
- data/roda-3.105.0/lib/roda/plugins/placeholder_string_matchers.rb +48 -0
- data/roda-3.105.0/lib/roda/plugins/plain_hash_response_headers.rb +32 -0
- data/roda-3.105.0/lib/roda/plugins/precompile_templates.rb +156 -0
- data/roda-3.105.0/lib/roda/plugins/public.rb +182 -0
- data/roda-3.105.0/lib/roda/plugins/r.rb +35 -0
- data/roda-3.105.0/lib/roda/plugins/recheck_precompiled_assets.rb +107 -0
- data/roda-3.105.0/lib/roda/plugins/redirect_http_to_https.rb +99 -0
- data/roda-3.105.0/lib/roda/plugins/redirect_path.rb +59 -0
- data/roda-3.105.0/lib/roda/plugins/relative_path.rb +74 -0
- data/roda-3.105.0/lib/roda/plugins/render.rb +998 -0
- data/roda-3.105.0/lib/roda/plugins/render_coverage.rb +105 -0
- data/roda-3.105.0/lib/roda/plugins/render_each.rb +224 -0
- data/roda-3.105.0/lib/roda/plugins/render_locals.rb +102 -0
- data/roda-3.105.0/lib/roda/plugins/request_aref.rb +77 -0
- data/roda-3.105.0/lib/roda/plugins/request_headers.rb +81 -0
- data/roda-3.105.0/lib/roda/plugins/response_attachment.rb +94 -0
- data/roda-3.105.0/lib/roda/plugins/response_content_type.rb +79 -0
- data/roda-3.105.0/lib/roda/plugins/response_request.rb +29 -0
- data/roda-3.105.0/lib/roda/plugins/route_block_args.rb +50 -0
- data/roda-3.105.0/lib/roda/plugins/route_csrf.rb +388 -0
- data/roda-3.105.0/lib/roda/plugins/run_append_slash.rb +51 -0
- data/roda-3.105.0/lib/roda/plugins/run_handler.rb +51 -0
- data/roda-3.105.0/lib/roda/plugins/run_require_slash.rb +46 -0
- data/roda-3.105.0/lib/roda/plugins/sec_fetch_site_csrf.rb +131 -0
- data/roda-3.105.0/lib/roda/plugins/send_file.rb +127 -0
- data/roda-3.105.0/lib/roda/plugins/sessions.rb +537 -0
- data/roda-3.105.0/lib/roda/plugins/shared_vars.rb +86 -0
- data/roda-3.105.0/lib/roda/plugins/sinatra_helpers.rb +443 -0
- data/roda-3.105.0/lib/roda/plugins/slash_path_empty.rb +26 -0
- data/roda-3.105.0/lib/roda/plugins/static.rb +42 -0
- data/roda-3.105.0/lib/roda/plugins/static_routing.rb +89 -0
- data/roda-3.105.0/lib/roda/plugins/status_303.rb +34 -0
- data/roda-3.105.0/lib/roda/plugins/status_handler.rb +86 -0
- data/roda-3.105.0/lib/roda/plugins/streaming.rb +170 -0
- data/roda-3.105.0/lib/roda/plugins/strip_path_prefix.rb +33 -0
- data/roda-3.105.0/lib/roda/plugins/symbol_matchers.rb +188 -0
- data/roda-3.105.0/lib/roda/plugins/symbol_status.rb +32 -0
- data/roda-3.105.0/lib/roda/plugins/symbol_views.rb +37 -0
- data/roda-3.105.0/lib/roda/plugins/timestamp_public.rb +78 -0
- data/roda-3.105.0/lib/roda/plugins/type_routing.rb +215 -0
- data/roda-3.105.0/lib/roda/plugins/typecast_params.rb +1183 -0
- data/roda-3.105.0/lib/roda/plugins/typecast_params_sized_integers.rb +108 -0
- data/roda-3.105.0/lib/roda/plugins/unescape_path.rb +41 -0
- data/roda-3.105.0/lib/roda/plugins/view_options.rb +201 -0
- data/roda-3.105.0/lib/roda/plugins/view_subdir_leading_slash.rb +49 -0
- data/roda-3.105.0/lib/roda/plugins.rb +68 -0
- data/roda-3.105.0/lib/roda/request.rb +722 -0
- data/roda-3.105.0/lib/roda/response.rb +221 -0
- data/roda-3.105.0/lib/roda/session_middleware.rb +183 -0
- data/roda-3.105.0/lib/roda/version.rb +18 -0
- data/roda-3.105.0/lib/roda.rb +586 -0
- data/ultra-lite-hub.gemspec +12 -0
- metadata +201 -0
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
# frozen-string-literal: true
|
|
2
|
+
|
|
3
|
+
#
|
|
4
|
+
class Roda
|
|
5
|
+
module RodaPlugins
|
|
6
|
+
# The assets_preloading plugin generates html tags or a header value
|
|
7
|
+
# to facilitate browser preloading of your assets. This allows
|
|
8
|
+
# compatible browsers to fetch assets before they are required,
|
|
9
|
+
# streamlining page rendering.
|
|
10
|
+
#
|
|
11
|
+
# For a list of compatible browsers, see
|
|
12
|
+
# http://caniuse.com/#search=link-rel-preload
|
|
13
|
+
#
|
|
14
|
+
# The plugin provides two functions - preload_assets_link_header and
|
|
15
|
+
# preload_assets_link_tags. The resulting preloading should be
|
|
16
|
+
# identical, it is up to you which system you prefer.
|
|
17
|
+
#
|
|
18
|
+
# preload_assets_link_header returns a string suitable for populating
|
|
19
|
+
# the response Link header:
|
|
20
|
+
#
|
|
21
|
+
# response.headers['Link'] = preload_assets_link_header(:css)
|
|
22
|
+
# # Link header will now contain something like:
|
|
23
|
+
# # </assets/app.css>;rel="preload";as="style"
|
|
24
|
+
#
|
|
25
|
+
# preload_assets_link_tags returns a string to drop into your
|
|
26
|
+
# templates containing link tags:
|
|
27
|
+
#
|
|
28
|
+
# preload_assets_link_tags(:css)
|
|
29
|
+
# # returns <link href="/assets/app.css" rel="preload" as="style">
|
|
30
|
+
#
|
|
31
|
+
# Note that these link tags are different to the usual asset
|
|
32
|
+
# declarations in markup; this will only instruct a compatible browser
|
|
33
|
+
# to fetch the file and cache it for later; the browser will not parse
|
|
34
|
+
# the asset until it encounters a traditional link or script tag.
|
|
35
|
+
#
|
|
36
|
+
# You must still setup and link to your assets as you did previously.
|
|
37
|
+
#
|
|
38
|
+
# Both functions can be passed any combination of asset types or
|
|
39
|
+
# asset groups, as multiple arguments:
|
|
40
|
+
#
|
|
41
|
+
# # generate tags for css assets and the app js asset group
|
|
42
|
+
# preload_assets_link_tags(:css, [:js, :app], [:js, :bar])
|
|
43
|
+
#
|
|
44
|
+
# # generate Link header for css assets and js asset groups app and bar
|
|
45
|
+
# preload_assets_link_header(:css, [:js, :app])
|
|
46
|
+
#
|
|
47
|
+
module AssetsPreloading
|
|
48
|
+
TYPE_AS = {
|
|
49
|
+
:css => 'style'.freeze,
|
|
50
|
+
:js => 'script'.freeze,
|
|
51
|
+
}.freeze
|
|
52
|
+
|
|
53
|
+
# Depend on the assets plugin, as we'll be calling some functions in it.
|
|
54
|
+
def self.load_dependencies(app)
|
|
55
|
+
app.plugin :assets
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
module InstanceMethods
|
|
59
|
+
# Return a string of <link> tags for the given asset
|
|
60
|
+
# types/groups.
|
|
61
|
+
def preload_assets_link_tags(*args)
|
|
62
|
+
_preload_assets_array(args).map{|path, as| "<link href=\"#{h(path)}\" rel=\"preload\" as=\"#{as}\">"}.join("\n")
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
# Return a string suitable for a Link header for the
|
|
66
|
+
# given asset types/groups.
|
|
67
|
+
def preload_assets_link_header(*args)
|
|
68
|
+
_preload_assets_array(args).map{|path, as| "<#{path}>;rel=preload;as=#{as}"}.join(",")
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
private
|
|
72
|
+
|
|
73
|
+
# Return an array of paths/as pairs for the given asset
|
|
74
|
+
# types and/or groups.
|
|
75
|
+
def _preload_assets_array(assets)
|
|
76
|
+
assets.flat_map do |type|
|
|
77
|
+
paths = assets_paths(type)
|
|
78
|
+
type = type[0] if type.is_a?(Array)
|
|
79
|
+
as = TYPE_AS[type]
|
|
80
|
+
|
|
81
|
+
paths.map{|path| [path, as]}
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
register_plugin(:assets_preloading, AssetsPreloading)
|
|
88
|
+
end
|
|
89
|
+
end
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# frozen-string-literal: true
|
|
2
|
+
|
|
3
|
+
#
|
|
4
|
+
class Roda
|
|
5
|
+
module RodaPlugins
|
|
6
|
+
# The assume_ssl plugin makes the request ssl? method always return
|
|
7
|
+
# true. This is useful when using an SSL-terminating reverse proxy
|
|
8
|
+
# that doesn't set the X-Forwarded-Proto or similar header to notify
|
|
9
|
+
# Rack that it is forwarding an SSL request.
|
|
10
|
+
#
|
|
11
|
+
# The sessions and sinatra_helpers plugins that ship with Roda both
|
|
12
|
+
# use the ssl? method internally and can be affected by use of the
|
|
13
|
+
# plugin. It's recommended that you use this plugin if you are
|
|
14
|
+
# using either plugin and an SSL-terminating proxy as described above.
|
|
15
|
+
#
|
|
16
|
+
# plugin :assume_ssl
|
|
17
|
+
module AssumeSSL
|
|
18
|
+
module RequestMethods
|
|
19
|
+
# Assume all requests are protected by SSL.
|
|
20
|
+
def ssl?
|
|
21
|
+
true
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
register_plugin(:assume_ssl, AssumeSSL)
|
|
27
|
+
end
|
|
28
|
+
end
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
# frozen-string-literal: true
|
|
2
|
+
|
|
3
|
+
#
|
|
4
|
+
class Roda
|
|
5
|
+
module RodaPlugins
|
|
6
|
+
# The autoload_hash_branches plugin builds on the hash_branches plugin and allows for
|
|
7
|
+
# delaying loading of a file containing a hash branch for an application until there
|
|
8
|
+
# is a request that uses the hash branch. This can be useful in development
|
|
9
|
+
# to improvement startup time by not loading all branches up front. It can also be
|
|
10
|
+
# useful in testing subsets of an application by only loading the hash branches being
|
|
11
|
+
# tested.
|
|
12
|
+
#
|
|
13
|
+
# You can specify a single hash branch for autoloading:
|
|
14
|
+
#
|
|
15
|
+
# plugin :autoload_hash_branches
|
|
16
|
+
# autoload_hash_branch('branch_name', '/absolute/path/to/file')
|
|
17
|
+
# autoload_hash_branch('namespace', 'branch_name', 'relative/path/to/file')
|
|
18
|
+
#
|
|
19
|
+
# You can also set the plugin to autoload load all hash branch files in a given directory.
|
|
20
|
+
# This will look at each .rb file in the directory, and add an autoload for it, using the
|
|
21
|
+
# filename without the .rb as the branch name:
|
|
22
|
+
#
|
|
23
|
+
# autoload_hash_branch_dir('/path/to/dir')
|
|
24
|
+
# autoload_hash_branch_dir('namespace', '/path/to/dir')
|
|
25
|
+
#
|
|
26
|
+
# In both cases, when the autoloaded file is required, it should redefine the same
|
|
27
|
+
# hash branch. If it does not, requests to the hash branch will result in a 404 error.
|
|
28
|
+
#
|
|
29
|
+
# When freezing an application, all hash branches are automatically loaded, because
|
|
30
|
+
# autoloading hash branches does not work for frozen applications.
|
|
31
|
+
module AutoloadHashBranches
|
|
32
|
+
def self.load_dependencies(app)
|
|
33
|
+
app.plugin :hash_branches
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def self.configure(app)
|
|
37
|
+
app.opts[:autoload_hash_branch_files] ||= []
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
module ClassMethods
|
|
41
|
+
# Autoload the given file when there is request for the hash branch.
|
|
42
|
+
# The given file should configure the hash branch specified.
|
|
43
|
+
def autoload_hash_branch(namespace='', segment, file)
|
|
44
|
+
segment = "/#{segment}"
|
|
45
|
+
file = File.expand_path(file)
|
|
46
|
+
opts[:autoload_hash_branch_files] << file
|
|
47
|
+
routes = opts[:hash_branches][namespace] ||= {}
|
|
48
|
+
meth = routes[segment] = define_roda_method(routes[segment] || "hash_branch_#{namespace}_#{segment}", 1) do |r|
|
|
49
|
+
loc = method(routes[segment]).source_location
|
|
50
|
+
require file
|
|
51
|
+
# Avoid infinite loop in case method is not overridden
|
|
52
|
+
if method(meth).source_location != loc
|
|
53
|
+
send(meth, r)
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
nil
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
# For each .rb file in the given directory, add an autoloaded hash branch
|
|
60
|
+
# based on the file name.
|
|
61
|
+
def autoload_hash_branch_dir(namespace='', dir)
|
|
62
|
+
Dir.new(dir).entries.each do |file|
|
|
63
|
+
if file =~ /\.rb\z/i
|
|
64
|
+
autoload_hash_branch(namespace, file.sub(/\.rb\z/i, ''), File.join(dir, file))
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
# Eagerly load all hash branches when freezing the application.
|
|
70
|
+
def freeze
|
|
71
|
+
opts.delete(:autoload_hash_branch_files).each{|file| require file} unless opts.frozen?
|
|
72
|
+
super
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
register_plugin(:autoload_hash_branches, AutoloadHashBranches)
|
|
78
|
+
end
|
|
79
|
+
end
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
# frozen-string-literal: true
|
|
2
|
+
|
|
3
|
+
#
|
|
4
|
+
class Roda
|
|
5
|
+
module RodaPlugins
|
|
6
|
+
# The autoload_named_routes plugin builds on the named_routes plugin and allows for
|
|
7
|
+
# delaying loading of a file containing a named route for an application until there
|
|
8
|
+
# is a request that uses the named route. This can be useful in development
|
|
9
|
+
# to improvement startup time by not loading all named routes up front. It can also be
|
|
10
|
+
# useful in testing subsets of an application by only loading the named routes being
|
|
11
|
+
# tested.
|
|
12
|
+
#
|
|
13
|
+
# You can specify a single hash branch for autoloading:
|
|
14
|
+
#
|
|
15
|
+
# plugin :autoload_named_route
|
|
16
|
+
# autoload_named_route(:route_name, '/absolute/path/to/file')
|
|
17
|
+
# autoload_named_route(:namespace, :route_name, 'relative/path/to/file')
|
|
18
|
+
#
|
|
19
|
+
# Note that unlike the +route+ method defined by the named_routes plugin, when providing
|
|
20
|
+
# a namespace, the namespace comes before the route name and not after.
|
|
21
|
+
#
|
|
22
|
+
# When the autoloaded file is required, it should redefine the same
|
|
23
|
+
# named route. If it does not, requests to the named route will be ignored (as if the
|
|
24
|
+
# related named route block was empty).
|
|
25
|
+
#
|
|
26
|
+
# When freezing an application, all named routes are automatically loaded, because
|
|
27
|
+
# autoloading named routes does not work for frozen applications.
|
|
28
|
+
module AutoloadNamedRoutes
|
|
29
|
+
def self.load_dependencies(app)
|
|
30
|
+
app.plugin :named_routes
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def self.configure(app)
|
|
34
|
+
app.opts[:autoload_named_route_files] ||= []
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
module ClassMethods
|
|
38
|
+
# Autoload the given file when there is request for the named route.
|
|
39
|
+
# The given file should configure the named route specified.
|
|
40
|
+
def autoload_named_route(namespace=nil, name, file)
|
|
41
|
+
file = File.expand_path(file)
|
|
42
|
+
opts[:autoload_named_route_files] << file
|
|
43
|
+
routes = opts[:namespaced_routes][namespace] ||= {}
|
|
44
|
+
meth = routes[name] = define_roda_method(routes[name] || "named_routes_#{namespace}_#{name}", 1) do |r|
|
|
45
|
+
loc = method(routes[name]).source_location
|
|
46
|
+
require file
|
|
47
|
+
# Avoid infinite loop in case method is not overridden
|
|
48
|
+
if method(meth).source_location != loc
|
|
49
|
+
send(meth, r)
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
nil
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
# Eagerly load all autoloaded named routes when freezing the application.
|
|
56
|
+
def freeze
|
|
57
|
+
opts.delete(:autoload_named_route_files).each{|file| require file} unless opts.frozen?
|
|
58
|
+
super
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
register_plugin(:autoload_named_routes, AutoloadNamedRoutes)
|
|
64
|
+
end
|
|
65
|
+
end
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
# frozen-string-literal: true
|
|
2
|
+
|
|
3
|
+
#
|
|
4
|
+
class Roda
|
|
5
|
+
module RodaPlugins
|
|
6
|
+
# The backtracking_array plugin changes the handling of array
|
|
7
|
+
# matchers such that if one of the array entries matches, but
|
|
8
|
+
# a later match argument fails, it will backtrack and try the
|
|
9
|
+
# next entry in the array. For example, the following match
|
|
10
|
+
# block does not match +/a/b+ by default:
|
|
11
|
+
#
|
|
12
|
+
# r.is ['a', 'a/b'] do |path|
|
|
13
|
+
# # ...
|
|
14
|
+
# end
|
|
15
|
+
#
|
|
16
|
+
# This is because the <tt>'a'</tt> entry in the array matches, which
|
|
17
|
+
# makes the array match. However, the next matcher is the
|
|
18
|
+
# terminal matcher (since +r.is+ was used), and since the
|
|
19
|
+
# path is not terminal as it still contains +/b+ after
|
|
20
|
+
# matching <tt>'a'</tt>.
|
|
21
|
+
#
|
|
22
|
+
# With the backtracking_array plugin, when the terminal matcher
|
|
23
|
+
# fails, matching will go on to the next entry in the array,
|
|
24
|
+
# <tt>'a/b'</tt>, which will also match. Since <tt>'a/b'</tt>
|
|
25
|
+
# matches the path fully, the terminal matcher also matches,
|
|
26
|
+
# and the match block yields.
|
|
27
|
+
module BacktrackingArray
|
|
28
|
+
module RequestMethods
|
|
29
|
+
private
|
|
30
|
+
|
|
31
|
+
# When matching for a single array, after a successful
|
|
32
|
+
# array element match, attempt to match all remaining
|
|
33
|
+
# elements. If the remaining elements could not be
|
|
34
|
+
# matched, reset the state and continue to the next
|
|
35
|
+
# entry in the array.
|
|
36
|
+
def _match_array(arg, rest)
|
|
37
|
+
path = @remaining_path
|
|
38
|
+
captures = @captures
|
|
39
|
+
caps = captures.dup
|
|
40
|
+
arg.each do |v|
|
|
41
|
+
if match(v, rest)
|
|
42
|
+
if v.is_a?(String)
|
|
43
|
+
captures.push(v)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
if match_all(rest)
|
|
47
|
+
return true
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
# Matching all remaining elements failed, reset state
|
|
51
|
+
captures.replace(caps)
|
|
52
|
+
@remaining_path = path
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
false
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
# If any of the args are an array, handle backtracking such
|
|
59
|
+
# that if a later matcher fails, we roll back to the current
|
|
60
|
+
# matcher and proceed to the next entry in the array.
|
|
61
|
+
def match_all(args)
|
|
62
|
+
args = args.dup
|
|
63
|
+
until args.empty?
|
|
64
|
+
arg = args.shift
|
|
65
|
+
if match(arg, args)
|
|
66
|
+
return true if arg.is_a?(Array)
|
|
67
|
+
else
|
|
68
|
+
return
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
true
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
# When matching an array, include the remaining arguments,
|
|
75
|
+
# otherwise, just match the single argument.
|
|
76
|
+
def match(v, rest = nil)
|
|
77
|
+
if v.is_a?(Array)
|
|
78
|
+
_match_array(v, rest)
|
|
79
|
+
else
|
|
80
|
+
super(v)
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
register_plugin(:backtracking_array, BacktrackingArray)
|
|
87
|
+
end
|
|
88
|
+
end
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# frozen-string-literal: true
|
|
2
|
+
|
|
3
|
+
#
|
|
4
|
+
class Roda
|
|
5
|
+
module RodaPlugins
|
|
6
|
+
# The bearer_token plugin adds an +r.bearer_token+ method for retrieving
|
|
7
|
+
# a bearer token from the +Authorization+ HTTP header. Bearer tokens will
|
|
8
|
+
# in the authorization header will be recognized as long as they start
|
|
9
|
+
# with the case insensitive string "bearer ".
|
|
10
|
+
module BearerToken
|
|
11
|
+
# :nocov:
|
|
12
|
+
METHOD = RUBY_VERSION >= "2.4" ? :match? : :match
|
|
13
|
+
# :nocov:
|
|
14
|
+
|
|
15
|
+
module RequestMethods
|
|
16
|
+
# Return the bearer token for the request if there is one in the
|
|
17
|
+
# authorization HTTP header.
|
|
18
|
+
def bearer_token
|
|
19
|
+
if (auth = @env["HTTP_AUTHORIZATION"]) && auth.send(METHOD, /\Abearer /i)
|
|
20
|
+
auth[7, 100000000]
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
register_plugin(:bearer_token, BearerToken)
|
|
27
|
+
end
|
|
28
|
+
end
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# frozen-string-literal: true
|
|
2
|
+
|
|
3
|
+
#
|
|
4
|
+
class Roda
|
|
5
|
+
module RodaPlugins
|
|
6
|
+
# The branch_locals plugin allows you to override view and layout
|
|
7
|
+
# locals for specific branches and routes.
|
|
8
|
+
#
|
|
9
|
+
# plugin :render
|
|
10
|
+
# plugin :render_locals, render: {footer: 'Default'}, layout: {title: 'Main'}
|
|
11
|
+
# plugin :branch_locals
|
|
12
|
+
#
|
|
13
|
+
# route do |r|
|
|
14
|
+
# r.on "users" do
|
|
15
|
+
# set_layout_locals title: 'Users'
|
|
16
|
+
# set_view_locals footer: '(c) Roda'
|
|
17
|
+
# end
|
|
18
|
+
# end
|
|
19
|
+
#
|
|
20
|
+
# The locals you specify in the set_layout_locals and set_view_locals methods
|
|
21
|
+
# have higher precedence than the render_locals plugin options, but lower precedence
|
|
22
|
+
# than options you directly pass to the view/render methods.
|
|
23
|
+
module BranchLocals
|
|
24
|
+
# Load the render_locals plugin before this plugin, since this plugin
|
|
25
|
+
# works by overriding methods in the render_locals plugin.
|
|
26
|
+
def self.load_dependencies(app)
|
|
27
|
+
app.plugin :render_locals
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
module InstanceMethods
|
|
31
|
+
# Update the default layout locals to use in this branch.
|
|
32
|
+
def set_layout_locals(opts)
|
|
33
|
+
if locals = @_layout_locals
|
|
34
|
+
@_layout_locals = locals.merge(opts)
|
|
35
|
+
else
|
|
36
|
+
@_layout_locals = opts
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# Update the default view locals to use in this branch.
|
|
41
|
+
def set_view_locals(opts)
|
|
42
|
+
if locals = @_view_locals
|
|
43
|
+
@_view_locals = locals.merge(opts)
|
|
44
|
+
else
|
|
45
|
+
@_view_locals = opts
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
private
|
|
50
|
+
|
|
51
|
+
# Make branch specific view locals override render_locals plugin defaults.
|
|
52
|
+
def render_locals
|
|
53
|
+
locals = super
|
|
54
|
+
if @_view_locals
|
|
55
|
+
locals = Hash[locals].merge!(@_view_locals)
|
|
56
|
+
end
|
|
57
|
+
locals
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# Make branch specific layout locals override render_locals plugin defaults.
|
|
61
|
+
def layout_locals
|
|
62
|
+
locals = super
|
|
63
|
+
if @_layout_locals
|
|
64
|
+
locals = Hash[locals].merge!(@_layout_locals)
|
|
65
|
+
end
|
|
66
|
+
locals
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
register_plugin(:branch_locals, BranchLocals)
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# frozen-string-literal: true
|
|
2
|
+
|
|
3
|
+
#
|
|
4
|
+
class Roda
|
|
5
|
+
module RodaPlugins
|
|
6
|
+
# The break plugin supports calling break inside a match block, to
|
|
7
|
+
# return from the block and continue in the routing tree, restoring
|
|
8
|
+
# the remaining path so that future matchers operating on the path
|
|
9
|
+
# operate as expected.
|
|
10
|
+
#
|
|
11
|
+
# plugin :break
|
|
12
|
+
#
|
|
13
|
+
# route do |r|
|
|
14
|
+
# r.on "foo", :bar do |bar|
|
|
15
|
+
# break if bar == 'baz'
|
|
16
|
+
# "/foo/#{bar} (not baz)"
|
|
17
|
+
# end
|
|
18
|
+
#
|
|
19
|
+
# r.on "foo/baz" do
|
|
20
|
+
# "/foo/baz"
|
|
21
|
+
# end
|
|
22
|
+
# end
|
|
23
|
+
#
|
|
24
|
+
# This provides the same basic feature as the pass plugin, but
|
|
25
|
+
# uses Ruby's standard control flow primative instead of a
|
|
26
|
+
# separate method.
|
|
27
|
+
module Break
|
|
28
|
+
module RequestMethods
|
|
29
|
+
private
|
|
30
|
+
|
|
31
|
+
# Handle break inside match blocks, restoring remaining path.
|
|
32
|
+
def if_match(_)
|
|
33
|
+
rp = @remaining_path
|
|
34
|
+
super
|
|
35
|
+
ensure
|
|
36
|
+
@remaining_path = rp
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
register_plugin(:break, Break)
|
|
42
|
+
end
|
|
43
|
+
end
|