class SinatraStatic
Attributes
app[RW]
Public Class Methods
new(app)
click to toggle source
# File lib/sinatra_static.rb, line 14 def initialize(app) @app = app end
Public Instance Methods
build!(dir)
click to toggle source
# File lib/sinatra_static.rb, line 18 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/sinatra_static.rb, line 33 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/sinatra_static.rb, line 26 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/sinatra_static.rb, line 54 def dir_exists?(dir) ::File.exists?(dir) && ::File.directory?(dir) end
dir_for_path(path, dir)
click to toggle source
# File lib/sinatra_static.rb, line 58 def dir_for_path(path, dir) file_for_path(path, dir).match(/(.*)\/[^\/]+$/)[1] end
env()
click to toggle source
# File lib/sinatra_static.rb, line 66 def env ENV['RACK_ENV'] end
file_extensions()
click to toggle source
# File lib/sinatra_static.rb, line 62 def file_extensions @@file_extensions end
file_for_path(path, dir)
click to toggle source
# File lib/sinatra_static.rb, line 46 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/sinatra_static.rb, line 40 def get_path(path) self.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_static.rb, line 82 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_static.rb, line 74 def handle_error_dir_not_found!(dir) handle_error!("can't find output directory: #{dir}") end
handle_error_no_each_route!()
click to toggle source
# File lib/sinatra_static.rb, line 70 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/sinatra_static.rb, line 78 def handle_error_non_200!(path) handle_error!("GET #{path} returned non-200 status code...") end