class Sharpie::Builder

Attributes

app[RW]

Public Class Methods

new(sinatra_application) click to toggle source
# File lib/sharpie.rb, line 24
def initialize(sinatra_application)
  @app = sinatra_application
end

Public Instance Methods

build!(dir) click to toggle source
# File lib/sharpie.rb, line 28
def build!(dir)
  handle_error_no_each_route! unless @app.respond_to?(:each_route)
  #handle_error_dir_not_found!(dir) unless dir_exists?(dir)
  build_routes(dir)
end

Private Instance Methods

build_path(path, dir) click to toggle source
# File lib/sharpie.rb, line 43
def build_path(path, dir)
  FileUtils.mkdir_p(dir_for_path(path, dir))
  File.open(file_for_path(path, dir), "w+") do |f|
    f.write(get_path(path).body)
  end
end
build_routes(dir) click to toggle source
# File lib/sharpie.rb, line 36
def build_routes(dir)
  @app.each_route do |route|     
    next unless route.verb == "GET"     
    build_path(route.path, dir)
  end 
end
dir_exists?(dir) click to toggle source
# File lib/sharpie.rb, line 74
def dir_exists?(dir)
  File.exists?(dir) && ::File.directory?(dir)
end
dir_for_path(path, dir) click to toggle source
# File lib/sharpie.rb, line 70
def dir_for_path(path, dir)
  file_for_path(path, dir).match(/(.*)\/[^\/]+$/)[1]  
end
file_extensions() click to toggle source
# File lib/sharpie.rb, line 58
def file_extensions
  @@file_extensions
end
file_for_path(path, dir) click to toggle source
# File lib/sharpie.rb, line 62
def file_for_path(path, dir)
  if path.match(/[^\/\.]+.(#{file_extensions.join("|")})$/)
    File.join(dir, path)
  else
    File.join(dir, path, "index.html")
  end
end
get_path(path) click to toggle source
# File lib/sharpie.rb, line 50
def get_path(path)
  self.get(path).tap do |resp|      
    unless resp.status == 200
      handle_error_non_200!(path)
    end        
  end
end
handle_error!(desc) click to toggle source
# File lib/sharpie.rb, line 86
def handle_error!(desc)    
  raise "Sharpie error: #{desc}"
end
handle_error_no_each_route!() click to toggle source
# File lib/sharpie.rb, line 82
def handle_error_no_each_route!
  handle_error!("can't call app.each_route, did you include sinatra-advanced-routes?")
end
handle_error_non_200!(path) click to toggle source
# File lib/sharpie.rb, line 78
def handle_error_non_200!(path)
  handle_error!("GET #{path} returned non-200 status code...")
end