Class: EasyHtmlGenerator::Rackapp

Inherits:
Object
  • Object
show all
Includes:
Rack::RespondTo
Defined in:
lib/easy_html_generator/rackapp.rb

Overview

this is the rack application for ehg

Constant Summary collapse

EHG_URL =
'https://github.com/creative-workflow/easy-html-generator'
LIST_BODY =
"<!DOCTYPE html><html><head>
  <style>*{ font-size: 20px;
        font-family: Verdana, 'Lucida Sans Unicode', sans-serif;}
     a,a:hover{text-decoration: none; color: #818181;}
  </style></head>
  <body>
{CONTENT}
<a target='_blank' href='#{EHG_URL}'>ehc on Github</a></body></html>"

Class Method Summary collapse

Class Method Details

.build_list_dir_row(file) ⇒ Object



61
62
63
64
65
66
# File 'lib/easy_html_generator/rackapp.rb', line 61

def self.build_list_dir_row(file)
  f = File.path(file)
  f_name = File.basename(f)
  f_path = File.dirname(f)
  "<li><a href='#{f_path}/#{f_name}'><b>#{f_path}</b>/#{f_name}</a></li>"
end

.call(env) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/easy_html_generator/rackapp.rb', line 20

def self.call(env)
  Rack::RespondTo.env = env
  request = Rack::Request.new(env)

  case request.path_info
  when '/'
    list_dir
  else
    dispatch(request)
  end
end

.dispatch(request) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'lib/easy_html_generator/rackapp.rb', line 38

def self.dispatch(request)
  file = EasyHtmlGenerator.dispatch(request)

  if File.exist? file
    respond(File.read(file))
  else
    respond("404 not found #{file}", 404)
  end
end

.list_dirObject



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/easy_html_generator/rackapp.rb', line 48

def self.list_dir
  content = ''
  EasyHtmlGenerator.projects.each do |_name, project|
    content += "<h3>#{project.name}</h3>"
    project.list_dir.each do |file|
      content += build_list_dir_row(file)
    end
  end
  content = LIST_BODY.sub('{CONTENT}', "<ul>#{content}</ul>")

  respond(content)
end

.respond(body, status_code = 200) ⇒ Object



32
33
34
35
36
# File 'lib/easy_html_generator/rackapp.rb', line 32

def self.respond(body, status_code = 200)
  content_type = Rack::RespondTo.selected_media_type || 'text/html'

  [status_code, { 'Content-Type' => "#{content_type}; charset=UTF-8" }, [body || '']]
end