Class: ImageHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/helpers/image_helper.rb

Constant Summary collapse

IMAGE_CLASS_REGEX =
/xmp:Label="(.+)"/
RATING_REGEX =
/xmp:Rating="(.+)"/

Class Method Summary collapse

Class Method Details

.color_class(image) ⇒ Object



14
15
16
17
18
19
# File 'lib/helpers/image_helper.rb', line 14

def self.color_class(image)
  contents = xmp(image)
  return if contents.nil?
  matches = contents.match(IMAGE_CLASS_REGEX)
  matches[1] if matches
end

.contains_color_class?(image, values) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
24
# File 'lib/helpers/image_helper.rb', line 21

def self.contains_color_class?(image, values)
  values = [values] unless values.is_a? Array
  values.include? color_class(image)
end

.is_5_star?(image) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/helpers/image_helper.rb', line 38

def self.is_5_star?(image)
  rating(image) == '5'
end

.is_jpeg?(path) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
45
46
47
# File 'lib/helpers/image_helper.rb', line 42

def self.is_jpeg?(path)
  # remove . from the beginning
  extension = File.extname(path)[1..-1]
  return false if extension.nil?
  JPEG_EXTENSIONS.include? extension.downcase
end

.is_select?(image) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/helpers/image_helper.rb', line 34

def self.is_select?(image)
  contains_color_class?(image, SELECT_COLOR_TAGS) || rating(image) >= SELECT_RATING
end

.rating(image) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/helpers/image_helper.rb', line 26

def self.rating(image)
  contents = xmp(image)
  return 0 unless contents
  matches = contents.match(RATING_REGEX)
  return matches[1].to_i if matches && matches[1].match(/^\d+$/)
  0
end

.xmp(image) ⇒ Object

identify -format ‘%[EXIF:*]’ .jepg



8
9
10
11
12
# File 'lib/helpers/image_helper.rb', line 8

def self.xmp(image)
  xmp = File.join(File.dirname(image), File.basename(image, ".*") + ".XMP")
  return unless File.exist?(xmp)
  File.read(xmp)
end