Module: RubyPages

Defined in:
lib/ruby_pages.rb,
lib/ruby_pages/version.rb

Defined Under Namespace

Classes: MissingTemplateError

Constant Summary collapse

ALLOWED_EXTNAMES =
Set.new(%w[.html .htm .css .ico .txt .jpg .jpeg .png .svg .webmanifest]).freeze
FORBIDDEN_PATH_FRAGMENTS =
["", ".", ".."].freeze
TEMPLATE_EXTNAMES =
["", ".html", ".erb", ".html.erb"].freeze
VERSION =
"0.1.1"

Class Method Summary collapse

Class Method Details

.call(env) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/ruby_pages.rb', line 11

def self.call(env)
  web_path_arr = env["PATH_INFO"].split("/").reject do |d|
    FORBIDDEN_PATH_FRAGMENTS.include?(d)
  end
  os_path = File.join(Dir.getwd, *web_path_arr)
  ext = File.extname(env["PATH_INFO"])
  if ext.empty?
    os_path = File.join(os_path, "index.html")
    ext = ".html"
  end
  render_file(env, os_path, ext)
end

.headers(ext) ⇒ Object



43
44
45
46
47
# File 'lib/ruby_pages.rb', line 43

def self.headers(ext)
  return {"content-type" => "text/html"} if ext == ".html"
  return {"content-type" => "application/json"} if ext == ".json"
  {}
end

.render(partial, **options) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/ruby_pages.rb', line 49

def self.render(partial, **options)
  partname = File.join(Dir.getwd, partial)
  TEMPLATE_EXTNAMES.each do |ext|
    filename = partname + ext
    if File.file?(filename)
      bind = binding
      options.each do |key, value|
        bind.local_variable_set(key, value)
      end
      return ERB.new(File.read(filename)).result(bind)
    end
  end
  raise MissingTemplateError, "Template partial #{partial} doesn't exist."
end

.render_404(env) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/ruby_pages.rb', line 35

def self.render_404(env)
  file404 = File.join(Dir.getwd, "404.html")
  bind = binding
  bind.local_variable_set("env", env)
  body = File.file?(file404) ? ERB.new(File.read(file404)).result(bind) : "Page Not Found Error."
  [404, {"content-type" => "text/html"}, [body]]
end

.render_file(env, filename, ext) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/ruby_pages.rb', line 24

def self.render_file(env, filename, ext)
  return render_404(env) unless ALLOWED_EXTNAMES.include?(ext) && File.file?(filename)
  body = File.read(filename)
  if ext == ".html" || ext == ".json"
    bind = binding
    bind.local_variable_set("env", env)
    body = ERB.new(body).result(bind)
  end
  [200, headers(ext), [body]]
end