Skip to content

mfinelli/bankrupt

Repository files navigation

Bankrupt

RubyGems Build Status Coverage Status Maintainability Inline docs

A sinatra helper to path assets during development and production.

Usage

Sinatra

Before loading your app set a few constants:

CDN = CONFIG[:cdn_url].to_s.freeze

require 'bankrupt/util'
ASSETS = Bankrupt::Util.parse_manifest(
  File.join(APP_ROOT, 'tmp', 'assets.json')
).freeze

Then include bankrupt as a helper in your app:

require 'bankrupt'

class App < Sinatra::Base
  set :public_folder, File.join(APP_ROOT, 'public')

  helpers Bankrupt

  # TODO: there is a better way to do this
  def initialize
    @_assets = {}
    super
  end
end

Now, in your views you can use the helper methods:

== stylesheet('app.css')

In development mode it will load app.css from your local public directory but in production it will load the CDN URL and include integrity hashes and anonymous crossorigin attributes.

There's also a helper for script tags:

== javascript('app.js')

For images you can optionally pass a hash of options to apply additional attributes to the image:

== image('img.jpg', alt: 'img')

To get the full path of an asset:

a href=raw('file.pdf')
  | Click me!

Rake

You can use the bundled rake task to generate the manifest file in the correct format using any assets found in the public folder. You can also upload the assets to an s3 bucket for use with cloudfront as a CDN.

Make sure to add the extra dependencies to your gemfile:

gem 'aws-sdk-s3'
gem 'mini_mime'

In your rakefile you'll need to define several constants and then include the tasks:

require 'bankrupt/tasks'
require 'logger'

APP_ROOT = __dir__.freeze unless defined?(APP_ROOT)
CDN_BUCKET = 'your-s3-bucket'.freeze unless defined?(CDN_BUCKET)
CDN_PREFIX = 'project'.freeze unless defined?(CDN_PREFIX)

unless defined?(VERSION)
  VERSION = JSON.parse(File.read(File.join(APP_ROOT, 'package.json')),
                       symbolize_names: true).fetch(:version).freeze
end

LOG = Logger.new(STDOUT) unless defined?(LOG)

Finally set your default task:

task default: if ENV['CLOUDBUILD'].to_s.casecmp?('true')
                %i[bankrupt:cdn]
              else
                %i[bankrupt:manifest]
              end

Note that it's possible to upload some files to the CDN without their hash appended. Create a .bankrupt.yml file in the root of your project and provide an array of file globs to store "hashless":

---
hashless:
  - "*.pdf"
  - "image.png"

Rspec

If you're testing your app with rspec or similar you need to stub the CDN and ASSETS constants.

require 'rack/test'

RSpec.describe App do
  include Rack::Test::Methods

  before do
    stub_const('CDN', '')
    stub_const('ASSETS', {})
  end

  let(:app) { described_class }

  describe 'GET /' do
    before { get '/' }

    it 'returns a 200' do
      expect(last_response).to be_ok
    end
  end
end

License

Copyright 2018-2020 Mario Finelli

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.