Class: Guides::App

Inherits:
Object
  • Object
show all
Defined in:
lib/guides/preview.rb

Instance Method Summary collapse

Constructor Details

#initializeApp

Returns a new instance of App.



5
6
7
8
9
# File 'lib/guides/preview.rb', line 5

def initialize
  @local  = Rack::File.new(local_assets)
  @source = Rack::File.new(source_assets)
  @output = Rack::File.new(File.join(Guides.root, "output"))
end

Instance Method Details

#call(env) ⇒ Object



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

def call(env)
  path = env["PATH_INFO"]

  case path
  when "/"
    env["PATH_INFO"] = "/index.html"
    return call(env)
  when /\/(.*).html/
    name = $1
    generator = Guides::Generator.new({})
    source_file = Dir["#{source_templates}/#{name}.{html.erb,textile}"].first

    unless source_file
      return [404, {"Content-Type" => "text/html"}, ["#{name} not found in #{source_templates}: #{Guides.root}"]]
    end

    generator.send(:generate_guide, File.basename(source_file), "#{name}.html")
    return @output.call(env)
  else
    source = @source.call(env)
    return source if source.first == 200
    return @local.call(env)
  end
end

#local_assetsObject



11
12
13
# File 'lib/guides/preview.rb', line 11

def local_assets
  File.expand_path("../templates/assets", __FILE__)
end

#source_assetsObject



15
16
17
# File 'lib/guides/preview.rb', line 15

def source_assets
  File.join(Guides.root, "assets")
end

#source_templatesObject



19
20
21
# File 'lib/guides/preview.rb', line 19

def source_templates
  File.join(Guides.root, "source")
end