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::LLetters, Base::Letters, Base::NOT_GIVEN, 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(legacy_size = NOT_GIVEN, legacy_format = NOT_GIVEN, legacy_background_color = NOT_GIVEN, legacy_text_color = NOT_GIVEN, legacy_text = NOT_GIVEN, size: '300x300', format: 'png', background_color: nil, text_color: nil, text: nil) ⇒ String

Produces a random placeholder image from placehold.it.

Faker::Placeholdit.image #=> "https://placehold.it/300x300.png"
Faker::Placeholdit.image(size: '50x50') #=> "https://placehold.it/50x50.png"
Faker::Placeholdit.image(size: '50x50', format: 'jpg') #=> "https://placehold.it/50x50.jpg"
Faker::Placeholdit.image(size: '50x50', format: 'gif', background_color: 'ffffff') #=> "https://placehold.it/50x50.gif/ffffff"
Faker::Placeholdit.image(size: '50x50', format: 'jpeg', background_color: :random) #=> "https://placehold.it/50x50.jpeg/39eba7"
Faker::Placeholdit.image(size: '50x50', format: 'jpeg', background_color: 'ffffff', text_color: '000') #=> "https://placehold.it/50x50.jpeg/ffffff/000"
Faker::Placeholdit.image(size: '50x50', format: 'jpg', background_color: 'ffffff', text_color: '000', text: 'Some Custom Text') #=> "https://placehold.it/50x50.jpg/ffffff/000?text=Some Custom Text"

rubocop:disable Metrics/ParameterLists

Examples:

# Keyword arguments: size, format, background_color, text_color, text

Parameters:

  • size (String) (defaults to: '300x300')

    Specifies the image's size, dimensions separated by 'x'.

  • format (String) (defaults to: 'png')

    Specifies the image's extension.

  • background_color (String, Symbol) (defaults to: nil)

    Specifies the background color, either in hexadecimal format (without #) or as :random.

  • text_color (String, Symbol) (defaults to: nil)

    Specifies the text color, either in hexadecimal format (without #) or as :random.

  • text (String) (defaults to: nil)

    Specifies a custom text to be used.

Returns:

  • (String)

Raises:

  • (ArgumentError)

Available since:

  • 1.6.0


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/faker/default/placeholdit.rb', line 30

def image(legacy_size = NOT_GIVEN, legacy_format = NOT_GIVEN, legacy_background_color = NOT_GIVEN, legacy_text_color = NOT_GIVEN, legacy_text = NOT_GIVEN, size: '300x300', format: 'png', background_color: nil, text_color: nil, text: nil)
  # rubocop:enable Metrics/ParameterLists
  warn_for_deprecated_arguments do |keywords|
    keywords << :size if legacy_size != NOT_GIVEN
    keywords << :format if legacy_format != NOT_GIVEN
    keywords << :background_color if legacy_background_color != NOT_GIVEN
    keywords << :text_color if legacy_text_color != NOT_GIVEN
    keywords << :text if legacy_text != NOT_GIVEN
  end

  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