class RemoteLoader

data is loaded once time. If we can found data in cache, just use the cache instead of making lot of remote call.

Constants

CONTENT_CALLBACKS

Callback for plain text content

Public Class Methods

new() click to toggle source

Initialization of the loaded dir

Define the constant for the prefix url for binary file and the directory where all file will be saved

# File lib/plantuml-loader.rb, line 51
def initialize()
    conf = Jekyll.configuration({});
    initConf = conf['plantuml'] || {};
    pconf = PlantUmlConfig::DEFAULT.merge(initConf);
    dirname = conf['source'] + File::SEPARATOR + pconf[:assets].gsub(/\//, File::SEPARATOR).sub(/\/*$/, '').sub(/^\/*/, '');
    Jekyll.logger.info "Directory for storage remote data : %s" % [dirname],
    unless File.directory?(dirname) then
        Jekyll.logger.info "Create directory %s because this seems to be missing" % [dirname]
        FileUtils.mkdir_p(dirname)
    end
    @prefixUrl = pconf[:assets];
    @dirname = dirname;
end

Public Instance Methods

createRemoteUri(params) click to toggle source

Internal : get the url from a config

@param a hash with {:url, :code, :type } inside it Returns the url for remote to retrieve

# File lib/plantuml-loader.rb, line 69
def createRemoteUri(params)
    uri = params[:url];
    uri = uri.gsub(/\{code\}/, params[:code])
    uri = uri.gsub(/\{type\}/,  params[:type])
    return uri;
end
getData(params) click to toggle source

Internal : get the data for the remote connection

@param a hash with {:url, :code, :type } inside it Returns the data as a hash

# File lib/plantuml-loader.rb, line 80
def getData(params)
    ruri = createRemoteUri(params);
    fn = Digest::SHA256.hexdigest(ruri) + "." + params[:type]
    return { :remoteUri => ruri, :uri  => @prefixUrl + fn, :path => @dirname + File::SEPARATOR + fn }
end
loadText(params) click to toggle source

Public : get and saved the remote uri from a parameters hash if the same content has already been downloaded previously, just return the file content.

@param a hash with {:url, :code, :type } inside it Returns the content of the remote

# File lib/plantuml-loader.rb, line 134
def loadText(params)
    d = savedRemoteBinary(params);
    content, tc = File.read(d[:path]), CONTENT_CALLBACKS[params[:type].downcase];
    if tc then
        content = content.gsub(tc[:matcher], tc[:replacement]);
    end
    return content;
end
savedRemoteBinary(params) click to toggle source

Public : get and saved the remote uri from a parameters hash if the same content has already been downloaded previously, just retrieve return the file information.

@param a hash with {:url, :code, :type } inside it Returns a hash with { :remoteUri, :uri, :path }

# File lib/plantuml-loader.rb, line 92
def savedRemoteBinary(params)
    Jekyll.logger.debug "Plantuml remote loader params :", params;
    data = getData(params);
    unless File.exist?(data[:path]) then
        Jekyll.logger.info "Starting download content at %{remoteUri} done into file %{path}." % data;
        open(data[:path], 'wb') do |file|
            file << open(data[:remoteUri]).read
            Jekyll.logger.info "End download content at %{remoteUri} done into file %{path}." % data;
        end
    else
        Jekyll.logger.info "File %{path} has been found. Not download at %{remoteUri} will be made." % data;
    end
    return data;
end
savedRemoteBinaryBase64(params) click to toggle source
# File lib/plantuml-loader.rb, line 108
def savedRemoteBinaryBase64(params)
    Jekyll.logger.debug "Plantuml remote loader params :", params;
    data = getData(params);
    unless File.exist?(data[:path]) then
        Jekyll.logger.info "Starting download content at %{remoteUri} done into file %{path}." % data;
        open(data[:path], 'wb') do |file|
            file << open(data[:remoteUri]).read
            Jekyll.logger.info "End download content at %{remoteUri} done into file %{path}." % data;
        end
    else
        Jekyll.logger.info "File %{path} has been found. Not download at %{remoteUri} will be made." % data;
    end
    Jekyll.logger.info "turn data into base64 format" % data;
    echo = "cat %{path}" %data;
    base64 = `#{echo}  | base64`
    #Jekyll.logger.info  "ZZZZZ we got > #{base64}"
    #return base64
    return "<img src=\"data:image/png;base64, #{base64}#center\">"
end