Class: PlaySolder::Image

Inherits:
Fake
  • Object
show all
Defined in:
lib/play_solder/image.rb

Constant Summary collapse

EXTENSIONS =
[ ".jpg", ".png", ".gif" ]
DEFAULT_SIZE =
"200x100"

Instance Attribute Summary

Attributes inherited from Fake

#path

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Fake

#escaped_faked_file, #extension, #faked_file, for_path, #initialize, #text

Constructor Details

This class inherits a constructor from PlaySolder::Fake

Class Method Details

.auto_resize(options) ⇒ Object



6
7
8
# File 'lib/play_solder/image.rb', line 6

def self.auto_resize(options)
  @@auto_resize = options
end

Instance Method Details

#generateObject



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/play_solder/image.rb', line 10

def generate
  return faked_file if File.exist?(faked_file)

  cmd = "convert"
  cmd += " -background \\#{ random_colour }"
  cmd += " -size #{ size }"
  cmd += " -gravity center"
  cmd += " label:\"#{ text }\""
  cmd += " \"#{ escaped_faked_file }\""
  system(cmd)
  faked_file
end

#mime_typeObject



23
24
25
26
27
28
29
30
31
# File 'lib/play_solder/image.rb', line 23

def mime_type
  if extension == ".jpg"
    "image/jpeg"
  elsif extension == ".png"
    "image/png"
  elsif extension == ".gif"
    "image/gif"
  end
end

#random_colourObject



42
43
44
45
46
# File 'lib/play_solder/image.rb', line 42

def random_colour
  comps = []
  6.times { comps << random_component }
  "##{ comps.join }"
end

#random_componentObject



48
49
50
# File 'lib/play_solder/image.rb', line 48

def random_component
  (3..13).sort_by { rand }.first.to_s(16)
end

#sizeObject



33
34
35
36
37
38
39
40
# File 'lib/play_solder/image.rb', line 33

def size
  if defined?(@@auto_resize) and @@auto_resize
    @@auto_resize.each do |regexp, size|
      return size if path.match(regexp)
    end
  end
  DEFAULT_SIZE
end