Class: Cloudkeeper::Entities::Image

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

Constant Summary collapse

IMAGE_LIST_IMAGE_ATTRIBUTES =
%i[hv:uri sl:checksum:sha512 hv:size hv:version].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Image.



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

def initialize(uri, checksum, size = 0, digest = '', 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
  @digest = digest
  @image_files = image_files
end

Instance Attribute Details

#checksumObject

Returns the value of attribute checksum.



9
10
11
# File 'lib/cloudkeeper/entities/image.rb', line 9

def checksum
  @checksum
end

#digestObject

Returns the value of attribute digest.



9
10
11
# File 'lib/cloudkeeper/entities/image.rb', line 9

def digest
  @digest
end

#image_filesObject

Returns the value of attribute image_files.



9
10
11
# File 'lib/cloudkeeper/entities/image.rb', line 9

def image_files
  @image_files
end

#sizeObject

Returns the value of attribute size.



9
10
11
# File 'lib/cloudkeeper/entities/image.rb', line 9

def size
  @size
end

#uriObject

Returns the value of attribute uri.



9
10
11
# File 'lib/cloudkeeper/entities/image.rb', line 9

def uri
  @uri
end

Class Method Details

.from_hash(image_hash) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/cloudkeeper/entities/image.rb', line 35

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

  image_hash.deep_symbolize_keys!
  image_hash.keep_if { |key| IMAGE_LIST_IMAGE_ATTRIBUTES.include? key }

  Image.new image_hash[:'hv:uri'], image_hash[:'sl:checksum:sha512'], image_hash[:'hv:size'],
            Digest::SHA512.hexdigest(image_hash.to_json)
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



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

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

  image_files << file
end

#available_formatsObject



27
28
29
# File 'lib/cloudkeeper/entities/image.rb', line 27

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

#image_file(format) ⇒ Object



31
32
33
# File 'lib/cloudkeeper/entities/image.rb', line 31

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