Class: Guides::App

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

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ App

Returns a new instance of App.



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

def initialize(options = {})
  @production = !!options[:production]
  @local  = Rack::File.new(local_assets)
  @source = Rack::File.new(source_assets)
  @output = Rack::File.new(File.join(Guides.root, @production ? "output" : "staging"))
end

Instance Method Details

#call(env) ⇒ Object



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
50
51
52
53
54
# File 'lib/guides/preview.rb', line 24

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({ :production => @production })

    source_file = Dir["#{source_templates}/#{name}.{#{Guides::Generator::EXTENSIONS.join(",")}}"].first

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

    source_base = File.basename(source_file)

    if generator.construction?(source_base) && @production
      return [404, {"Content-Type" => "text/html"}, ["#{name} is under construction and not available in production"]]
    end

    generator.send(:generate_guide, source_base, "#{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



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

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

#source_assetsObject



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

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

#source_templatesObject



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

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