Class: Faker::Placeholdit

Inherits:
Base
  • Object
show all
Defined in:
lib/faker/default/placeholdit.rb

Constant Summary collapse

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

Constants inherited from Base

Base::Letters, Base::Numbers, Base::ULetters

Class Method Summary collapse

Methods inherited from Base

bothify, disable_enforce_available_locales, fetch, fetch_all, flexible, letterify, method_missing, numerify, parse, rand, rand_in_range, regexify, resolve, respond_to_missing?, sample, shuffle, translate, unique, with_locale

Class Method Details

.image(size = '300x300', format = 'png', background_color = nil, text_color = nil, text = nil) ⇒ Object

Raises:

  • (ArgumentError)

8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/faker/default/placeholdit.rb', line 8

def image(size = '300x300', format = 'png', background_color = nil, text_color = nil, text = nil)
  background_color = generate_color if background_color == :random
  text_color = generate_color if text_color == :random

  raise ArgumentError, 'Size should be specified in format 300x300' unless size =~ /^[0-9]+x[0-9]+$/
  raise ArgumentError, "Supported formats are #{SUPPORTED_FORMATS.join(', ')}" unless SUPPORTED_FORMATS.include?(format)
  raise ArgumentError, "background_color must be a hex value without '#'" unless background_color.nil? || background_color =~ /((?:^\h{3}$)|(?:^\h{6}$)){1}(?!.*\H)/
  raise ArgumentError, "text_color must be a hex value without '#'" unless text_color.nil? || text_color =~ /((?:^\h{3}$)|(?:^\h{6}$)){1}(?!.*\H)/

  image_url = "https://placehold.it/#{size}.#{format}"
  image_url += "/#{background_color}" if background_color
  image_url += "/#{text_color}" if text_color
  image_url += "?text=#{text}" if text
  image_url
end