Class: Cloudkeeper::Entities::Image

Inherits:
Object
  • Object
show all
Defined in:
lib/cloudkeeper/entities/image.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri, checksum, size = 0, image_files = []) ⇒ Image

Returns a new instance of Image.



6
7
8
9
10
11
12
13
# File 'lib/cloudkeeper/entities/image.rb', line 6

def initialize(uri, checksum, size = 0, image_files = [])
  raise Cloudkeeper::Errors::ArgumentError, 'uri and checksum cannot be nil nor empty' if uri.blank? || checksum.blank?

  @uri = uri
  @checksum = checksum
  @size = size
  @image_files = image_files
end

Instance Attribute Details

#checksumObject

Returns the value of attribute checksum.



4
5
6
# File 'lib/cloudkeeper/entities/image.rb', line 4

def checksum
  @checksum
end

#image_filesObject

Returns the value of attribute image_files.



4
5
6
# File 'lib/cloudkeeper/entities/image.rb', line 4

def image_files
  @image_files
end

#sizeObject

Returns the value of attribute size.



4
5
6
# File 'lib/cloudkeeper/entities/image.rb', line 4

def size
  @size
end

#uriObject

Returns the value of attribute uri.



4
5
6
# File 'lib/cloudkeeper/entities/image.rb', line 4

def uri
  @uri
end

Class Method Details

.from_hash(image_hash) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/cloudkeeper/entities/image.rb', line 29

def self.from_hash(image_hash)
  raise Cloudkeeper::Errors::Parsing::InvalidImageHashError, 'invalid image hash' if image_hash.blank?

  image_hash.deep_symbolize_keys!
  Image.new image_hash[:'hv:uri'], image_hash[:'sl:checksum:sha512'], image_hash[:'hv:size']
rescue Cloudkeeper::Errors::ArgumentError => ex
  raise Cloudkeeper::Errors::Parsing::InvalidImageHashError, ex, "image hash #{image_hash.inspect} " \
                                                                 "doesn't contain all the necessary data"
end

Instance Method Details

#add_image_file(file) ⇒ Object



15
16
17
18
19
# File 'lib/cloudkeeper/entities/image.rb', line 15

def add_image_file(file)
  raise Cloudkeeper::Errors::ArgumentError, 'image file cannot be nil' if file.nil?

  image_files << file
end

#available_formatsObject



21
22
23
# File 'lib/cloudkeeper/entities/image.rb', line 21

def available_formats
  image_files.map(&:format).sort
end

#image_file(format) ⇒ Object



25
26
27
# File 'lib/cloudkeeper/entities/image.rb', line 25

def image_file(format)
  image_files.select { |file| file.format == format }.first
end