Sha256: 0a5b9ce39ac46dfda7d28243577a8bdd368f5aa1a63ad3331381d1001050ff18

Contents?: true

Size: 898 Bytes

Versions: 4

Compression:

Stored size: 898 Bytes

Contents

module Poms
  module Fields
    # Module to retrieve media related information from poms items.
    module Media
      extend self

      IMAGE_TYPE_PRIORITY = %w[PROMO_LANDSCAPE PICTURE].freeze

      # Returns the images from the hash
      def images(item)
        item['images'].try(:sort_by) do |i|
          image_order_index(i)
        end
      end

      # Extracts the image id from an image hash
      # Expects a hash of just an image from POMS
      def image_id(image)
        return unless image['imageUri']
        image['imageUri'].split(':').last
      end

      def image_order_index(image)
        IMAGE_TYPE_PRIORITY.index(image['type']) || IMAGE_TYPE_PRIORITY.size
      end

      # Returns the id of the first image or nil if there are none.
      def first_image_id(item)
        return unless images(item)
        image_id(images(item).first)
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
poms-2.6.0 lib/poms/fields/media.rb
poms-2.5.1 lib/poms/fields/media.rb
poms-2.5.0 lib/poms/fields/media.rb
poms-2.4.0 lib/poms/fields/media.rb