Module: Syntropy::ControllerExtensions

Defined in:
lib/syntropy/controller_extensions.rb

Overview

Utilities for use in modules

Instance Method Summary collapse

Instance Method Details

#appSyntropy::App

Instantiates a Syntropy app for the given environment hash.

Returns:



64
65
66
# File 'lib/syntropy/controller_extensions.rb', line 64

def app(**)
  Syntropy::App.new(**)
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(ref) ⇒ Array<Hash>

Returns a list of parsed markdown pages at the given path.

Parameters:

  • ref (String)

    directory path

Returns:

  • (Array<Hash>)

    array of page entries



51
52
53
54
55
56
57
58
59
# File 'lib/syntropy/controller_extensions.rb', line 51

def page_list(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