Class: Prestashop::Mapper::Image
- Defined in:
- lib/prestashop/mapper/models/image.rb
Instance Attribute Summary collapse
-
#file ⇒ Object
Returns the value of attribute file.
-
#id ⇒ Object
Returns the value of attribute id.
-
#id_resource ⇒ Object
Returns the value of attribute id_resource.
-
#resource ⇒ Object
Returns the value of attribute resource.
-
#source ⇒ Object
Returns the value of attribute source.
Instance Method Summary collapse
- #images ⇒ Object
-
#initialize(args = {}) ⇒ Image
constructor
A new instance of Image.
- #payload ⇒ Object
- #upload ⇒ Object
- #uploader(source) ⇒ Object
Methods inherited from Model
#hash_lang, #meta_description, #meta_keywords, #meta_title, model, resource
Methods included from Extension
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
#file ⇒ Object
Returns the value of attribute file.
10 11 12 |
# File 'lib/prestashop/mapper/models/image.rb', line 10 def file @file end |
#id ⇒ Object
Returns the value of attribute id.
10 11 12 |
# File 'lib/prestashop/mapper/models/image.rb', line 10 def id @id end |
#id_resource ⇒ Object
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 |
#resource ⇒ Object
Returns the value of attribute resource.
10 11 12 |
# File 'lib/prestashop/mapper/models/image.rb', line 10 def resource @resource end |
#source ⇒ Object
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
#images ⇒ Object
19 20 21 |
# File 'lib/prestashop/mapper/models/image.rb', line 19 def images source.kind_of?(Array) ? source : [source] end |
#payload ⇒ Object
47 48 49 |
# File 'lib/prestashop/mapper/models/image.rb', line 47 def payload { image: Faraday::UploadIO.new(file.path, 'image') } end |
#upload ⇒ Object
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 |