Module: Syntropy::ControllerExtensions
- Defined in:
- lib/syntropy/controller_extensions.rb
Overview
Utilities for use in modules
Constant Summary collapse
- BUILTIN_APPLET_app_root =
File.(File.join(__dir__, 'applets/builtin'))
Instance Method Summary collapse
-
#app ⇒ Syntropy::App
Instantiates a Syntropy app for the given environment hash.
-
#builtin_applet(env, mount_path: '/.syntropy') ⇒ Syntropy::App
Creates a builtin applet with the given environment hash.
-
#dispatch_by_host(dir = nil, map = nil) ⇒ Proc
Returns a request handler that routes request according to the host header.
-
#dispatch_by_http_method ⇒ Proc
Returns a request handler that handles requests by calling the appropriate module method (e.g. get, post, etc.).
-
#page_list(env, ref) ⇒ Array<Hash>
Returns a list of parsed markdown pages at the given path.
-
#tmp_path(prefix = 'syntropy') ⇒ String
Returns a unique temporary path.
Instance Method Details
#app ⇒ Syntropy::App
Instantiates a Syntropy app for the given environment hash.
65 66 67 |
# File 'lib/syntropy/controller_extensions.rb', line 65 def app(**) Syntropy::App.new(**) end |
#builtin_applet(env, mount_path: '/.syntropy') ⇒ Syntropy::App
Creates a builtin applet with the given environment hash. By default the builtin applet is mounted at /.syntropy.
77 78 79 80 81 82 83 84 85 |
# File 'lib/syntropy/controller_extensions.rb', line 77 def builtin_applet(env, mount_path: '/.syntropy') app( machine: env[:machine], app_root: BUILTIN_APPLET_app_root, mount_path: mount_path, builtin_applet_path: nil, watch_files: nil ) end |
#dispatch_by_host(dir = nil, map = nil) ⇒ Proc
Returns a request handler that routes request according to the host header. Looks for site directories (named by host name) in the app’s root directory. A map may be given in order to provide additional hostnames to site directories.
24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/syntropy/controller_extensions.rb', line 24 def dispatch_by_host(dir = nil, map = nil) raise Syntropy::Error, 'Must provide dir and/or map' if !dir && !map site_map = {} setup_directory_sites(dir, site_map) if dir setup_mapped_sites(map, site_map) if map ->(req) do site = site_map[req.host] site ? site.call(req) : req.respond(nil, ':status' => HTTP::BAD_REQUEST) end end |
#dispatch_by_http_method ⇒ Proc
Returns a request handler that handles requests by calling the appropriate module method (e.g. get, post, etc.)
41 42 43 44 45 |
# File 'lib/syntropy/controller_extensions.rb', line 41 def dispatch_by_http_method ->(req) do route_by_http_method(req) end end |
#page_list(env, ref) ⇒ Array<Hash>
Returns a list of parsed markdown pages at the given path.
52 53 54 55 56 57 58 59 60 |
# File 'lib/syntropy/controller_extensions.rb', line 52 def page_list(env, ref) full_path = File.join(env[:app_root], ref) raise 'Not a directory' if !File.directory?(full_path) Dir[File.join(full_path, '*.md')].sort.map { atts, markdown = Syntropy::Markdown.parse(it, env) { atts:, markdown: } } end |
#tmp_path(prefix = 'syntropy') ⇒ String
Returns a unique temporary path
12 13 14 |
# File 'lib/syntropy/controller_extensions.rb', line 12 def tmp_path(prefix = 'syntropy') "/tmp/#{prefix}-#{SecureRandom.hex(16)}" end |