# frozen_string_literal: true # Copyright (C) 2015, 2016, 2017 Szymon Kopciewski # # This file is part of SinatraAssetsLinker. # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . require "sinatra/base" module Sinatra module AssetsLinker def css_uri(file_name, absolute = true, add_script_name = true) css_dir = get_settings(:project_css_dir) generate_uri(css_dir, file_name, absolute, add_script_name) end def js_uri(file_name, absolute = true, add_script_name = true) javascript_dir = get_settings(:project_javascripts_dir) js_dir = get_settings(:project_js_compressed_dir) choosen_dir = get_settings(:project_assets_verbose) ? javascript_dir : js_dir generate_uri(choosen_dir, file_name, absolute, add_script_name) end def img_uri(file_name, absolute = true, add_script_name = true) img_dir = get_settings(:project_images_dir) generate_uri(img_dir, file_name, absolute, add_script_name) end private def get_settings(key) settings.assets_linker_config.fetch(key.to_s) end def generate_uri(assets_dir, file_name, absolute, add_script_name) cdn_url = get_settings(:project_cdn_url) relative_url = File.join(assets_dir, file_name) if cdn_url.nil? || cdn_url == "" uri(relative_url, absolute, add_script_name) else uri(relative_url, false, add_script_name) File.join(cdn_url, relative_url) end end end end