Class: WebResourceBundler::Filters::ImageEncodeFilter::ImageData

Inherits:
Object
  • Object
show all
Defined in:
lib/web_resource_bundler/filters/image_encode_filter/image_data.rb

Overview

ImageData contains info about image found in css files

Constant Summary collapse

MAX_RAND_FOR_ID =
10000

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, folder) ⇒ ImageData

Returns a new instance of ImageData.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/web_resource_bundler/filters/image_encode_filter/image_data.rb', line 10

def initialize(url, folder)
    @url = url
    #computing absolute file path using folder of css file
	@path = File.join(folder, url) 
    if File.file?(@path)
	  @exist = true 
    else 
      @exist = false
    end
    if WebResourceBundler::Bundler.instance.logger and !@path.include?('://') and !@exist
      WebResourceBundler::Bundler.instance.logger.info("Image not found #{@path}")
    end
	if @exist
		@size = File.size(@path)
		name, @extension = File.basename(@path).split('.')
		@id = Digest::MD5.hexdigest(url) 
	end
end

Instance Attribute Details

#existObject (readonly)

Returns the value of attribute exist.



8
9
10
# File 'lib/web_resource_bundler/filters/image_encode_filter/image_data.rb', line 8

def exist
  @exist
end

#extensionObject (readonly)

Returns the value of attribute extension.



8
9
10
# File 'lib/web_resource_bundler/filters/image_encode_filter/image_data.rb', line 8

def extension
  @extension
end

#idObject (readonly)

Returns the value of attribute id.



8
9
10
# File 'lib/web_resource_bundler/filters/image_encode_filter/image_data.rb', line 8

def id
  @id
end

#pathObject (readonly)

Returns the value of attribute path.



8
9
10
# File 'lib/web_resource_bundler/filters/image_encode_filter/image_data.rb', line 8

def path
  @path
end

#urlObject (readonly)

Returns the value of attribute url.



8
9
10
# File 'lib/web_resource_bundler/filters/image_encode_filter/image_data.rb', line 8

def url
  @url
end

Instance Method Details

#construct_mhtml_image_data(separator) ⇒ Object

constructs part of css header with data for current image



34
35
36
37
38
39
40
41
# File 'lib/web_resource_bundler/filters/image_encode_filter/image_data.rb', line 34

def construct_mhtml_image_data(separator)
	if @exist
		result = separator + "\n"
		result << 'Content-Location:' << @id << "\n"
		result <<	'Content-Transfer-Encoding:base64' << "\n\n"
		result << encoded << "\n\n"
	end
end

#encodedObject



43
44
45
46
# File 'lib/web_resource_bundler/filters/image_encode_filter/image_data.rb', line 43

def encoded
	return nil unless @exist
	Base64.encode64(File.read(@path)).gsub("\n", '')
end

#sizeObject



29
30
31
# File 'lib/web_resource_bundler/filters/image_encode_filter/image_data.rb', line 29

def size
  @size / 1024
end