Class: Prestashop::Mapper::Image

Inherits:
Model
  • Object
show all
Defined in:
lib/prestashop/mapper/models/image.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Model

#hash_lang, #meta_description, #meta_keywords, #meta_title, model, resource

Methods included from Extension

included

Constructor Details

#initialize(args = {}) ⇒ Image

Returns a new instance of Image.


12
13
14
15
16
17
# File 'lib/prestashop/mapper/models/image.rb', line 12

def initialize args = {}
  @id          = args[:id]
  @resource    = args.fetch(:resource)
  @id_resource = args.fetch(:id_resource)
  @source      = args.fetch(:source)
end

Instance Attribute Details

#fileObject

Returns the value of attribute file


10
11
12
# File 'lib/prestashop/mapper/models/image.rb', line 10

def file
  @file
end

#idObject

Returns the value of attribute id


10
11
12
# File 'lib/prestashop/mapper/models/image.rb', line 10

def id
  @id
end

#id_resourceObject

Returns the value of attribute id_resource


10
11
12
# File 'lib/prestashop/mapper/models/image.rb', line 10

def id_resource
  @id_resource
end

#resourceObject

Returns the value of attribute resource


10
11
12
# File 'lib/prestashop/mapper/models/image.rb', line 10

def resource
  @resource
end

#sourceObject

Returns the value of attribute source


10
11
12
# File 'lib/prestashop/mapper/models/image.rb', line 10

def source
  @source
end

Instance Method Details

#imagesObject


19
20
21
# File 'lib/prestashop/mapper/models/image.rb', line 19

def images
  source.kind_of?(Array) ? source : [source]
end

#payloadObject


47
48
49
# File 'lib/prestashop/mapper/models/image.rb', line 47

def payload
  { image: Faraday::UploadIO.new(file.path, 'image') }
end

#uploadObject


23
24
25
26
27
28
29
# File 'lib/prestashop/mapper/models/image.rb', line 23

def upload
  result = []
  images.each do |image|
    result << uploader(image)
  end unless images.empty?
  result
end

#uploader(source) ⇒ Object


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/prestashop/mapper/models/image.rb', line 31

def uploader source
  if source =~ URI::regexp
    source = URI::encode(source)
    self.file = MiniMagick::Image.open(source)
    file.format 'png' unless %w(jpg jpeg png gif).include?(file[:format])
    result = Client.upload 'images', resource, id_resource, payload, file
    result[:image][:id] if result
  else
    false # Not valid url
  end
rescue MiniMagick::Invalid
  false # It's not valid image
rescue OpenURI::HTTPError
  false # Image not found
end