class Sinatra::Export::Builder

Public Class Methods

new(app) click to toggle source
# File lib/sinatra/export.rb, line 30
def initialize(app)
  @app = app
end

Public Instance Methods

app() click to toggle source
# File lib/sinatra/export.rb, line 34
def app
  @app
end
build!() click to toggle source
# File lib/sinatra/export.rb, line 38
def build!
  dir = app.public_folder
  handle_error_dir_not_found!(dir) unless dir_exists?(dir)
  app.each_route do |route|
    next if route.verb != 'GET' or not route.path.is_a? String
    build_path(route.path, dir)
  end
end

Private Instance Methods

build_path(path, dir) click to toggle source
# File lib/sinatra/export.rb, line 49
def build_path(path, dir)
  response = get_path(path)
  body = response.body
  mtime = response.headers.key?("Last-Modified") ?
    Time.httpdate(response.headers["Last-Modified"]) : Time.now
  file_path = file_for_path(path, dir)
  dir_path = dir_for_path(path, dir)

  ::FileUtils.mkdir_p(dir_path)
  ::File.open(file_path, 'w+') do |f|
    f.write(body)
  end
  ::FileUtils.touch(file_path, :mtime => mtime)
end
dir_exists?(dir) click to toggle source
# File lib/sinatra/export.rb, line 78
def dir_exists?(dir)
  ::File.exists?(dir) && ::File.directory?(dir)
end
dir_for_path(path, dir) click to toggle source
# File lib/sinatra/export.rb, line 82
def dir_for_path(path, dir)
  file_for_path(path, dir).match(/(.*)\/[^\/]+$/)[1]
end
file_for_path(path, dir) click to toggle source
# File lib/sinatra/export.rb, line 70
def file_for_path(path, dir)
  if path.match(/[^\/\.]+.(#{app.settings.export_extensions.join("|")})$/)
    ::File.join(dir, path)
  else
    ::File.join(dir, path, 'index.html')
  end
end
get_path( path) click to toggle source
# File lib/sinatra/export.rb, line 64
def get_path( path)
  get(path).tap do |resp|
    handle_error_non_200!(path) unless resp.status == 200
  end
end
handle_error!(desc) click to toggle source
# File lib/sinatra/export.rb, line 94
def handle_error!(desc)
  puts ColorString.new("failed: #{desc}").red; exit!
end
handle_error_dir_not_found!(dir) click to toggle source
# File lib/sinatra/export.rb, line 86
def handle_error_dir_not_found!(dir)
  handle_error!("can't find output directory: #{dir}")
end
handle_error_non_200!(path) click to toggle source
# File lib/sinatra/export.rb, line 90
def handle_error_non_200!(path)
  handle_error!("GET #{path} returned non-200 status code...")
end