Class: HtmlMockup::Rack::HtmlMockup

Inherits:
Object
  • Object
show all
Defined in:
lib/html_mockup/rack/html_mockup.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project) ⇒ HtmlMockup

Returns a new instance of HtmlMockup.



14
15
16
17
18
19
20
# File 'lib/html_mockup/rack/html_mockup.rb', line 14

def initialize(project)
  @project = project
  @docroot = project.html_path
  
  @resolver = Resolver.new(@docroot)
  @file_server = ::Rack::File.new(@docroot)
end

Instance Attribute Details

#projectObject (readonly)

Returns the value of attribute project.



12
13
14
# File 'lib/html_mockup/rack/html_mockup.rb', line 12

def project
  @project
end

Instance Method Details

#call(env) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/html_mockup/rack/html_mockup.rb', line 22

def call(env)
  url = env["PATH_INFO"]
  env["MOCKUP_PROJECT"] = project
  
  if template_path = @resolver.url_to_path(url)
    env["rack.errors"].puts "Rendering template #{template_path.inspect} (#{url.inspect})"
    # begin
      templ = ::HtmlMockup::Template.open(template_path, :partials_path => @project.partials_path, :layouts_path => @project.layouts_path)
      mime = ::Rack::Mime.mime_type(File.extname(template_path), 'text/html')
      resp = ::Rack::Response.new do |res|
        res.headers["Content-Type"] = mime if mime
        res.status = 200
        res.write templ.render(env)
      end
      resp.finish
    # rescue StandardError => e
    #   env["rack.errors"].puts "#{e.message}\n #{e.backtrace.join("\n")}\n\n"
    #   resp = ::Rack::Response.new do |res|
    #     res.status = 500
    #     res.write "An error occurred"
    #   end
    #   resp.finish
    # end
  else
    env["rack.errors"].puts "Invoking file handler for #{url.inspect}"
    @file_server.call(env)
  end
end