Sha256: cbbe62bfc7b7e86a1321f7f85b2ba0538379bcdd6d8ae70e1672fd302ff3afc0

Contents?: true

Size: 1.38 KB

Versions: 47

Compression:

Stored size: 1.38 KB

Contents

# Adds a route for JavaScript files.
#
# == Usage
#
#   require 'sinatra/support/jssupport'
#
#   class Main < Sinatra::Base
#     register Sinatra::JsSupport
#     serve_js '/js', from: './app/js'
#   end
#
# You'll be able to access files via +/js+:
#
#   # This will serve /app/js/jquery.js. (or .coffee)
#   $ curl "http://localhost:4567/js/jquery.js"
#
# == CoffeeScript support
#
# This plugin supports CoffeeScript. To use it, simply
# add a CoffeeScript file in the JS file path.
#
#   # Will first try app/js/application.coffee,
#   # then move onto app/js/application.js if it's not found.
#
#   $ curl "http://localhost:4567/js/application.js"
#
# To use CoffeeScript, install the +coffee_script+ gem.
# If you're using Bundler, that is:
#
#   # Gemfile
#   gem "coffee-script", require: "coffee_script"
#
module Sinatra::JsSupport
  def self.registered(app)
    app.set :js_max_age, app.development? ? 0 : 86400*30
  end

  def serve_js(url_prefix, options={})
    path   = File.join(url_prefix, '*.js')
    prefix = options[:from]

    get path do |name|
      fname = Dir[File.join(prefix, "#{name}.{js,coffee}")].first  or pass

      content_type :js
      last_modified File.mtime(fname)
      cache_control :public, :must_revalidate, :max_age => settings.js_max_age

      if fname =~ /\.coffee$/
        coffee File.read(fname)
      else
        send_file fname
      end
    end
  end
end

Version data entries

47 entries across 47 versions & 2 rubygems

Version Path
classiccms-0.7.5 vendor/bundle/gems/sinatra-support-1.2.2/lib/sinatra/support/jssupport.rb
classiccms-0.7.4 vendor/bundle/gems/sinatra-support-1.2.2/lib/sinatra/support/jssupport.rb
classiccms-0.7.3 vendor/bundle/gems/sinatra-support-1.2.2/lib/sinatra/support/jssupport.rb
classiccms-0.7.2 vendor/bundle/gems/sinatra-support-1.2.2/lib/sinatra/support/jssupport.rb
classiccms-0.7.1 vendor/bundle/gems/sinatra-support-1.2.2/lib/sinatra/support/jssupport.rb
classiccms-0.7.0 vendor/bundle/gems/sinatra-support-1.2.2/lib/sinatra/support/jssupport.rb
classiccms-0.6.9 vendor/bundle/gems/sinatra-support-1.2.2/lib/sinatra/support/jssupport.rb
classiccms-0.6.8 vendor/bundle/gems/sinatra-support-1.2.2/lib/sinatra/support/jssupport.rb
classiccms-0.6.7 vendor/bundle/gems/sinatra-support-1.2.2/lib/sinatra/support/jssupport.rb
classiccms-0.6.6 vendor/bundle/gems/sinatra-support-1.2.2/lib/sinatra/support/jssupport.rb
classiccms-0.6.5 vendor/bundle/gems/sinatra-support-1.2.2/lib/sinatra/support/jssupport.rb
classiccms-0.6.4 vendor/bundle/gems/sinatra-support-1.2.2/lib/sinatra/support/jssupport.rb
classiccms-0.6.3 vendor/bundle/gems/sinatra-support-1.2.2/lib/sinatra/support/jssupport.rb
classiccms-0.6.2 vendor/bundle/gems/sinatra-support-1.2.2/lib/sinatra/support/jssupport.rb
classiccms-0.6.1 vendor/bundle/gems/sinatra-support-1.2.2/lib/sinatra/support/jssupport.rb
classiccms-0.6.0 vendor/bundle/gems/sinatra-support-1.2.2/lib/sinatra/support/jssupport.rb
classiccms-0.5.17 vendor/bundle/gems/sinatra-support-1.2.2/lib/sinatra/support/jssupport.rb
classiccms-0.5.16 vendor/bundle/gems/sinatra-support-1.2.2/lib/sinatra/support/jssupport.rb
classiccms-0.5.15 vendor/bundle/gems/sinatra-support-1.2.2/lib/sinatra/support/jssupport.rb
classiccms-0.5.14 vendor/bundle/gems/sinatra-support-1.2.2/lib/sinatra/support/jssupport.rb