Module: Syntropy::ControllerExtensions

Defined in:
lib/syntropy/controller_extensions.rb

Overview

Utilities for use in modules

Constant Summary collapse

BUILTIN_APPLET_app_root =
File.expand_path(File.join(__dir__, 'applets/builtin'))

Instance Method Summary collapse

Instance Method Details

#appSyntropy::App

Instantiates a Syntropy app for the given environment hash.

Returns:



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.

Parameters:

  • env (Hash)

    app environment

  • mount_path (String) (defaults to: '/.syntropy')

    mount path for the builtin applet

Returns:



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.

Parameters:

  • dir (String, nil) (defaults to: nil)

    relative directory path for host sites

  • map (Hash, nil) (defaults to: nil)

    hash mapping host names to relative site directory

Returns:

  • (Proc)

    router proc

Raises:



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_methodProc

Returns a request handler that handles requests by calling the appropriate module method (e.g. get, post, etc.)

Returns:

  • (Proc)


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.

Parameters:

  • env (Hash)

    app environment hash

  • ref (String)

    directory path

Returns:

  • (Array<Hash>)

    array of page entries



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

Parameters:

  • prefix (String) (defaults to: 'syntropy')

    temp file prefix

Returns:

  • (String)


12
13
14
# File 'lib/syntropy/controller_extensions.rb', line 12

def tmp_path(prefix = 'syntropy')
  "/tmp/#{prefix}-#{SecureRandom.hex(16)}"
end