Sha256: d1a5038eb1387fae8e0dd656c8a2b3a91eb1f2e3a999fca248e0099cbdd61495

Contents?: true

Size: 1.87 KB

Versions: 5

Compression:

Stored size: 1.87 KB

Contents

require "rack"

module Guides
  class App
    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

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

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

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

    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
  end

  class Preview < Rack::Server
    def self.start(options = {})
      super options.merge(:host => '0.0.0.0', :Port => 9292, :server => "thin")
    end

    def initialize(options = {})
      @production = !!options[:production]
      super(options)
    end

    def app
      @app ||= App.new(:production => @production)
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
guides-0.7.0 lib/guides/preview.rb
guides-0.6.10 lib/guides/preview.rb
guides-0.6.9 lib/guides/preview.rb
guides-0.6.8.1 lib/guides/preview.rb
guides-0.6.8 lib/guides/preview.rb