Class: Sitepress::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/sitepress/server.rb

Overview

Run a Sitepress site as a rack app.

Instance Method Summary collapse

Constructor Details

#initialize(site:) ⇒ Server

Returns a new instance of Server.



4
5
6
7
8
9
# File 'lib/sitepress/server.rb', line 4

def initialize(site: )
  @site = site
  # TODO: This is in the wrong place. Needs to be configurable by
  # Sitepress::Site.
  @helper_paths = Dir.glob(@site.root_path.join("helpers/**.rb"))
end

Instance Method Details

#call(env) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/sitepress/server.rb', line 11

def call(env)
  req = Rack::Request.new(env)
  resource = @site.get req.path

  if resource
    # TODO: Lets slim this down a bit.
    helpers = HelperLoader.new paths: @helper_paths
    context = helpers.context locals: {
      current_page: resource, site: @site }
    renderer = ResourceRenderer.new resource: resource

    mime_type = resource.mime_type.to_s
    body = renderer.render context: context

    [ 200, {"Content-Type" => mime_type}, Array(body) ]
  else
    [ 404, {"Content-Type" => "text/plain"}, ["Not Found"]]
  end
end