Class: RemoteLoader
- Inherits:
-
Object
- Object
- RemoteLoader
- Includes:
- Singleton
- Defined in:
- lib/plantuml-loader.rb
Overview
data is loaded once time. If we can found data in cache, just use the cache instead of making lot of remote call.
Constant Summary collapse
- CONTENT_CALLBACKS =
Callback for plain text content
{ 'svg' => { :matcher => /\<\?xml.*?\>/, :replacement => '' } }
Instance Method Summary collapse
-
#createRemoteUri(params) ⇒ Object
Internal : get the url from a config.
-
#getData(params) ⇒ Object
Internal : get the data for the remote connection.
-
#initialize ⇒ RemoteLoader
constructor
Initialization of the loaded dir.
-
#loadText(params) ⇒ Object
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.
-
#savedRemoteBinary(params) ⇒ Object
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.
- #savedRemoteBinaryBase64(params) ⇒ Object
Constructor Details
#initialize ⇒ RemoteLoader
Initialization of the loaded dir
Define the constant for the prefix url for binary file and the directory where all file will be saved
51 52 53 54 55 56 57 58 59 60 61 62 63 |
# 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 |
Instance Method Details
#createRemoteUri(params) ⇒ Object
Internal : get the url from a config
Returns the url for remote to retrieve
69 70 71 72 73 74 |
# 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) ⇒ Object
Internal : get the data for the remote connection
Returns the data as a hash
80 81 82 83 84 |
# 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) ⇒ Object
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.
Returns the content of the remote
134 135 136 137 138 139 140 141 |
# 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) ⇒ Object
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.
Returns a hash with { :remoteUri, :uri, :path }
92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# 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) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# 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 |