GithubHelp home page GithubHelp logo

mfinelli / bankrupt Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 129 KB

A sinatra helper for assets in development and production

Home Page: https://rubygems.org/gems/bankrupt

License: Apache License 2.0

Ruby 100.00%
assets ruby rubygem sinatra

bankrupt's Introduction

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-2021 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.

bankrupt's People

Contributors

mfinelli avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.