Module: FFaker::Image

Extended by:
Image, ModuleUtils
Included in:
Image
Defined in:
lib/ffaker/image.rb

Constant Summary collapse

SUPPORTED_FORMATS =
%w[png jpg jpeg gif].freeze

Instance Method Summary collapse

Methods included from ModuleUtils

const_missing, k, luhn_check, underscore, unique

Methods included from RandomUtils

#fetch_sample, #rand, #shuffle

Instance Method Details

#file(*args, size: '300x300', format: 'png', bg_color: :random, text_color: :random, text: nil) ⇒ Object

‘*args` for old format support, it will be removed with deprecation rubocop:disable Metrics/ParameterLists



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/ffaker/image.rb', line 39

def file(*args, size: '300x300', format: 'png', bg_color: :random, text_color: :random, text: nil)
  if args.any?
    warn "Positional arguments for Image##{__method__} are deprecated. Please use keyword arguments."
    size = args[0]
    format = args[1] if args.size > 1
    bg_color = args[2] if args.size > 2
    text_color = args[3] if args.size > 3
    text = args[4] if args.size > 4
  end

  uri = URI.parse(url(size: size, format: format, bg_color: bg_color, text_color: text_color, text: text))
  file = Tempfile.new('ffaker_image')
  file.binmode
  file << uri.open.read
  file.close
  File.new(file.path)
end

#url(*args, size: '300x300', format: 'png', bg_color: :random, text_color: :random, text: nil) ⇒ Object

‘*args` for old format support, it will be removed with deprecation rubocop:disable Metrics/ParameterLists



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/ffaker/image.rb', line 16

def url(*args, size: '300x300', format: 'png', bg_color: :random, text_color: :random, text: nil)
  if args.any?
    warn "Positional arguments for Image##{__method__} are deprecated. Please use keyword arguments."
    size = args[0]
    format = args[1] if args.size > 1
    bg_color = args[2] if args.size > 2
    text_color = args[3] if args.size > 3
    text = args[4] if args.size > 4
  end

  check_size!(size)
  check_format!(format)

  bg_color = FFaker::Color.hex_code if bg_color == :random
  text_color = FFaker::Color.hex_code if text_color == :random
  text = CGI.escape(text.to_s)

  "https://dummyimage.com/#{size}/#{bg_color}/#{text_color}.#{format}?text=#{text}"
end