Module: Syntropy::Utilities

Included in:
Syntropy
Defined in:
lib/syntropy/utils.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:



50
51
52
# File 'lib/syntropy/utils.rb', line 50

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:



62
63
64
65
66
67
68
69
70
# File 'lib/syntropy/utils.rb', line 62

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

#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



37
38
39
40
41
42
43
44
45
# File 'lib/syntropy/utils.rb', line 37

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

#route_by_host(env, 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:

  • env (Hash)

    app environment hash

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

    additional hostname map

Returns:

  • (Proc)

    router proc



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

def route_by_host(env, map = nil)
  sites = find_hostname_sites(env)

  # add map refs
  map&.each { |k, v| sites[k] = sites[v] }

  lambda { |req|
    site = sites[req.host]
    site ? site.call(req) : req.respond(nil, ':status' => HTTP::BAD_REQUEST)
  }
end

#tmp_path(prefix = 'syntropy') ⇒ Object



8
9
10
# File 'lib/syntropy/utils.rb', line 8

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