Module: Bankrupt

Defined in:
lib/bankrupt.rb,
lib/bankrupt/util.rb,
lib/bankrupt/version.rb

Overview

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.

Defined Under Namespace

Classes: Util

Constant Summary collapse

ASSET =
Struct.new(:path, :sri).freeze
IMAGE_CDN =
<<~SLIM
  img crossorigin='anonymous' src=path
SLIM
IMAGE_LOCAL =
<<~SLIM
  img src=path
SLIM
JAVASCRIPT_CDN =
<<~SLIM
  script crossorigin='anonymous' integrity=sri src=path
SLIM
JAVASCRIPT_LOCAL =
<<~SLIM
  script src=path
SLIM
STYLESHEET_CDN =
<<~SLIM
  link crossorigin='anonymous' href=path integrity=sri rel='stylesheet'
SLIM
STYLESHEET_LOCAL =
<<~SLIM
  link href=path rel='stylesheet'
SLIM
VERSION =
'3.0.0'

Instance Method Summary collapse

Instance Method Details

#image(path, options = {}) ⇒ String

TODO:

we compute the options on every call, we should do the lookup first and short circuit

Return an image html tag for the asset.

Parameters:

  • path (String)

    relative (from public) path to the img

  • options (Hash) (defaults to: {})

    additional attributes to add to the img tag

Returns:

  • (String)

    image html element



56
57
58
59
60
61
# File 'lib/bankrupt.rb', line 56

def image(path, options = {})
  o = Hash(options).map { |k, v| "#{k}='#{v}'" }.join(' ')

  asset_html(path, [IMAGE_CDN.chomp, o].join(' '),
             [IMAGE_LOCAL.chomp, o].join(' '), options)
end

#javascript(path) ⇒ String

Return a javascript html tag for the asset.

Parameters:

  • path (String)

    relative (from public) path to the js

Returns:

  • (String)

    script html element



67
68
69
# File 'lib/bankrupt.rb', line 67

def javascript(path)
  asset_html(path, JAVASCRIPT_CDN, JAVASCRIPT_LOCAL)
end

#raw(path) ⇒ String

Get the full path to the asset for use in e.g. a tags.

Parameters:

  • path (String)

    relative (from public) path to the asset

Returns:

  • (String)

    full path to the asset



83
84
85
86
87
88
# File 'lib/bankrupt.rb', line 83

def raw(path)
  details = ASSETS.fetch(path)
  create_fullpath(path, details[:md5], hashless: details[:hashless])
rescue KeyError
  "/#{path}"
end

#stylesheet(path) ⇒ String

Return a stylesheet html tag for the asset.

Parameters:

  • path (String)

    relative (from public) path to the css

Returns:

  • (String)

    stylesheet html element



75
76
77
# File 'lib/bankrupt.rb', line 75

def stylesheet(path)
  asset_html(path, STYLESHEET_CDN, STYLESHEET_LOCAL)
end