Module: Syntropy

Extended by:
Utilities
Defined in:
lib/syntropy.rb,
lib/syntropy/app.rb,
lib/syntropy/test.rb,
lib/syntropy/utils.rb,
lib/syntropy/errors.rb,
lib/syntropy/logger.rb,
lib/syntropy/module.rb,
lib/syntropy/request.rb,
lib/syntropy/version.rb,
lib/syntropy/json_api.rb,
lib/syntropy/markdown.rb,
lib/syntropy/side_run.rb,
lib/syntropy/mime_types.rb,
lib/syntropy/http/client.rb,
lib/syntropy/http/server.rb,
lib/syntropy/http/status.rb,
lib/syntropy/routing_tree.rb,
lib/syntropy/connection_pool.rb,
lib/syntropy/request/response.rb,
lib/syntropy/http/io_extensions.rb,
lib/syntropy/request/validation.rb,
lib/syntropy/request/mock_adapter.rb,
lib/syntropy/request/request_info.rb,
lib/syntropy/http/client_connection.rb,
lib/syntropy/http/server_connection.rb

Defined Under Namespace

Modules: HTTP, MimeTypes, RequestInfoClassMethods, RequestInfoMethods, RequestValidationMethods, ResponseMethods, SideRun, StaticFileCaching, Utilities Classes: App, BadRequestError, ConnectionPool, Error, InvalidRequestContentTypeError, JSONAPI, Logger, MockAdapter, Module, ModuleLoader, ProtocolError, Request, RoutingTree, TestHarness, UnsupportedHTTPVersionError, ValidationError

Constant Summary collapse

VERSION =
'0.31.0'
DATE_REGEXP =
/(\d{4}-\d{2}-\d{2})/
FRONT_MATTER_REGEXP =
/\A(---\s*\n.*?\n?)^((---|\.\.\.)\s*$\n?)/m
YAML_OPTS =
{
  permitted_classes: [Date],
  symbolize_names: true
}

Constants included from Utilities

Utilities::BUILTIN_APPLET_ROOT_DIR

Class Attribute Summary collapse

Class Method Summary collapse

Methods included from Utilities

app, builtin_applet, page_list, route_by_host

Class Attribute Details

.machineObject

Returns the value of attribute machine.



26
27
28
# File 'lib/syntropy.rb', line 26

def machine
  @machine
end

Class Method Details

.env(env = nil, &app) ⇒ Object



59
60
61
62
63
64
# File 'lib/syntropy.rb', line 59

def env(env = nil, &app)
  return @env if !env && !app

  @env = env || {}
  @env[:app] = app if app
end

.parse_markdown_file(path, env) ⇒ Array

Parses the markdown file at the given path.

Parameters:

  • path (String)

    file path

Returns:

  • (Array)

    an tuple containing properties<Hash>, contents<String>



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/syntropy/markdown.rb', line 17

def self.parse_markdown_file(path, env)
  content = IO.read(path) || ''
  atts = {}

  # Parse date from file name
  m = path.match(DATE_REGEXP)
  atts[:date] ||= Date.parse(m[1]) if m

  if (m = content.match(FRONT_MATTER_REGEXP))
    front_matter = m[1]
    content = m.post_match

    yaml = YAML.safe_load(front_matter, **YAML_OPTS)
    atts = atts.merge(yaml)
  end

  if env[:root_dir]
    atts[:url] = path
                 .gsub(/#{env[:root_dir]}/, '')
                 .gsub(/\.md$/, '')
  end

  [atts, content]
end

.run(env = {}, &app) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/syntropy.rb', line 36

def run(env = {}, &app)
  if @in_run
    @env = env
    @env[:app] = app if app
    return
  end

  env ||= @env || {}
  begin
    @in_run = true
    machine = env[:machine] || UM.new

    env[:logger]&.info(message: "Running Syntropy #{Syntropy::VERSION}, UringMachine #{UM::VERSION}, Ruby #{RUBY_VERSION}")

    server = HTTP::Server.new(machine, env, &app)

    setup_signal_handling(machine, Fiber.current)
    server.run
  ensure
    @in_run = false
  end
end

.side_run(&block) ⇒ Object



28
29
30
31
32
# File 'lib/syntropy.rb', line 28

def side_run(&block)
  raise 'Syntropy.machine not set' if !@machine

  SideRun.call(@machine, &block)
end