Class: EasyHtmlGenerator::RackDispatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/easy_html_generator/rack_dispatcher.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}'>ehg on Github</a></body></html>"

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ RackDispatcher

Returns a new instance of RackDispatcher.



16
17
18
# File 'lib/easy_html_generator/rack_dispatcher.rb', line 16

def initialize(app)
  @app = app
end

Instance Method Details

#build_list_dir_row(file) ⇒ Object



51
52
53
54
55
56
# File 'lib/easy_html_generator/rack_dispatcher.rb', line 51

def 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
31
32
33
34
35
36
# File 'lib/easy_html_generator/rack_dispatcher.rb', line 20

def call(env)
  request = Rack::Request.new(env)
  case request.path_info
  when '/'
    create_directory_listening_index_file
    env['PATH_INFO'] = '/index.html'
  else
    EasyHtmlGenerator.dispatch(request)
  end
  status, headers, body = @app.call(env)

  headers['Connection']    = 'Keep-Alive'
  headers['Charset']       = 'UTF-8'
  headers['Vary']          = 'Accept-Encoding'

  [status, headers, body]
end

#create_directory_listening_index_fileObject



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/easy_html_generator/rack_dispatcher.rb', line 38

def create_directory_listening_index_file
  content = ''
  EasyHtmlGenerator::Workspace.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>")

  File.write("#{EasyHtmlGenerator::DIST_PATH}/index.html", content)
end