class Faker::Placeholdit

Constants

SUPPORTED_FORMATS

Public Class Methods

image(size = '300x300', format = 'png', background_color = '000', text_color = nil, text = nil) click to toggle source
# File lib/faker/placeholdit.rb, line 6
def image(size = '300x300', format = 'png', background_color = '000', text_color = nil, text = nil)
  raise ArgumentError, "Size should be specified in format 300x300" unless size.match(/^[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.match(/((?:^\h{3}$)|(?:^\h{6}$)){1}(?!.*\H)/)
  raise ArgumentError, "text_color must be a hex value without '#'" unless text_color.nil? || text_color.match(/((?:^\h{3}$)|(?:^\h{6}$)){1}(?!.*\H)/)

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