Class: Faker::Avatar

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

Constant Summary

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_slug = NOT_GIVEN, legacy_size = NOT_GIVEN, legacy_format = NOT_GIVEN, legacy_set = NOT_GIVEN, legacy_bgset = NOT_GIVEN, slug: nil, size: '300x300', format: 'png', set: 'set1', bgset: nil) ⇒ String

Produces a URL for an avatar from robohash.org

Examples:

Faker::Avatar.image
  #=> "https://robohash.org/sitsequiquia.png?size=300x300&set=set1"
Faker::Avatar.image(slug: "my-own-slug")
  #=> "https://robohash.org/my-own-slug.png?size=300x300&set=set1"
Faker::Avatar.image(slug: "my-own-slug", size: "50x50")
  #=> "https://robohash.org/my-own-slug.png?size=50x50&set=set1"
Faker::Avatar.image(slug: "my-own-slug", size: "50x50", format: "jpg")
  #=> "https://robohash.org/my-own-slug.jpg?size=50x50&set=set1"
Faker::Avatar.image(slug: "my-own-slug", size: "50x50", format: "bmp")
  #=> "https://robohash.org/my-own-slug.bmp?size=50x50&set=set1"
Faker::Avatar.image(slug: "my-own-slug", size: "50x50", format: "bmp", set: "set1", bgset: "bg1")
  #=> "https://robohash.org/my-own-slug.bmp?size=50x50&set=set1&bgset=bg1"

Parameters:

  • slug (String, nil) (defaults to: nil)
  • size (String) (defaults to: '300x300')

    image size in pixels, in the format of 'AxB'

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

    The image file format

  • set (String) (defaults to: 'set1')

    The avatar set to use

  • bgset (String, nil) (defaults to: nil)

    The background set to use

Returns:

  • (String)

    A robohash.org URL

Raises:

  • (ArgumentError)

Available since:

  • 1.4.3


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/faker/default/avatar.rb', line 41

def image(legacy_slug = NOT_GIVEN, legacy_size = NOT_GIVEN, legacy_format = NOT_GIVEN, legacy_set = NOT_GIVEN, legacy_bgset = NOT_GIVEN, slug: nil, size: '300x300', format: 'png', set: 'set1', bgset: nil)
  # rubocop:enable Metrics/ParameterLists
  warn_for_deprecated_arguments do |keywords|
    keywords << :slug if legacy_slug != NOT_GIVEN
    keywords << :size if legacy_size != NOT_GIVEN
    keywords << :format if legacy_format != NOT_GIVEN
    keywords << :set if legacy_set != NOT_GIVEN
    keywords << :bgset if legacy_bgset != NOT_GIVEN
  end

  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)

  slug ||= Faker::Lorem.words.join
  bgset_query = "&bgset=#{bgset}" if bgset
  "https://robohash.org/#{slug}.#{format}?size=#{size}&set=#{set}#{bgset_query}"
end