Sha256: b6928968e0298bf0057c6f44c0137797376bf701f1e0662b7b40959e3a32c54c

Contents?: true

Size: 1.25 KB

Versions: 5

Compression:

Stored size: 1.25 KB

Contents

# frozen_string_literal: true

require 'cgi'
require 'tempfile'
require 'open-uri'

module FFaker
  module Image
    extend ModuleUtils
    extend self

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

    def url(size = '300x300', format = 'png', bg_color = :random, text_color = :random, text = nil)
      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

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

    private

    def check_size!(size)
      return true if size =~ /\A\d+x\d+\z/

      raise ArgumentError, 'Size should be specified in format 300x300'
    end

    def check_format!(format)
      return true if SUPPORTED_FORMATS.include?(format)

      raise ArgumentError, "Supported formats are #{SUPPORTED_FORMATS.join(', ')}"
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
ffaker-2.22.0 lib/ffaker/image.rb
ffaker-2.21.0 lib/ffaker/image.rb
ffaker-2.20.0 lib/ffaker/image.rb
ffaker-2.19.0 lib/ffaker/image.rb
ffaker-2.18.0 lib/ffaker/image.rb