class Slideoff::SlidesAPI

Public Class Methods

new(presentation) click to toggle source
# File lib/slideoff/slides_api.rb, line 9
def initialize(presentation)
  @presentation = presentation
end

Public Instance Methods

response(env) click to toggle source
# File lib/slideoff/slides_api.rb, line 13
def response(env)
  path_info = Rack::Utils.unescape(env["PATH_INFO"])
  if path_info == "/"
    serve_slides
  elsif path_info.include? ".."
    unauthorized_access
  else
    serve_asset(path_info)
  end
end

Protected Instance Methods

page_not_found(path_info) click to toggle source
# File lib/slideoff/slides_api.rb, line 45
def page_not_found(path_info)
  [404, {
    "Content-Type"   => "text/plain",
    "Content-Length" => "0"
  }, ["File not found: #{path_info}\n"] ]
end
serve_asset(path_info) click to toggle source
# File lib/slideoff/slides_api.rb, line 34
def serve_asset(path_info)
  path = @presentation.path_for_asset(path_info)
  return page_not_found(path_info) unless path && File.readable?(path)
  body = File.read(path)
  [200, {
    "Last-Modified"  => File.mtime(path).httpdate,
    "Content-Length" => Rack::Utils.bytesize(body).to_s,
    "Content-Type"   => Rack::Mime.mime_type(File.extname(path), 'text/plain'),
  }, [body] ]
end
serve_slides() click to toggle source
# File lib/slideoff/slides_api.rb, line 26
def serve_slides
  body = @presentation.html
  [200, {
    "Content-Type"   => "text/html; charset=utf-8",
    "Content-Length" => Rack::Utils.bytesize(body).to_s
  }, [body] ]
end
unauthorized_access() click to toggle source
# File lib/slideoff/slides_api.rb, line 52
def unauthorized_access
  [403, {
    "Content-Type"   => "text/plain",
    "Content-Length" => "0"
  }, ["Forbidden\n"] ]
end