Module: Syntropy::Utilities

Included in:
Syntropy
Defined in:
lib/syntropy/utils.rb

Overview

Utilities for use in modules

Instance Method Summary collapse

Instance Method Details

#app(**env) ⇒ Object



38
39
40
# File 'lib/syntropy/utils.rb', line 38

def app(**env)
  Syntropy::App.new(**env)
end

#page_list(env, ref) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/syntropy/utils.rb', line 28

def page_list(env, ref)
  full_path = File.join(env[:root_dir], ref)
  raise 'Not a directory' if !File.directory?(full_path)

  Dir[File.join(full_path, '*.md')].sort.map {
    atts, markdown = Syntropy.parse_markdown_file(it, env)
    { atts:, markdown: }
  }
end

#route_by_host(env, map = nil) ⇒ Object

Returns a request handler that routes request according to



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/syntropy/utils.rb', line 7

def route_by_host(env, map = nil)
  root = env[:root_dir]
  sites = Dir[File.join(root, '*')]
          .reject { File.basename(it) =~ /^_/ }
          .select { File.directory?(it) }
          .each_with_object({}) { |fn, h|
            name = File.basename(fn)
            opts = opts.merge(root_dir: fn)
            h[name] = Syntropy::App.new(**opts)
          }

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

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