Sha256: b6f2f0c2f907c754db9384ee96699a09e4fdb177254f53dbde626cf782ed16e8

Contents?: true

Size: 1.23 KB

Versions: 2

Compression:

Stored size: 1.23 KB

Contents

module Fakeit
  module Openapi
    module Example
      FORMAT_HANDLERS = {
        'uri' => -> { Faker::Internet.url },
        'uuid' => -> { SecureRandom.uuid },
        'guid' => -> { SecureRandom.uuid },
        'email' => -> { Faker::Internet.email },
        'date' => -> { Faker::Date.backward(100).iso8601 },
        'date-time' => -> { Faker::Time.backward(100).iso8601 }
      }.freeze

      def string_example
        if enum then enum.to_a.sample
        elsif pattern then Faker::Base.regexify(pattern)
        elsif format then string_format
        elsif length_constraint then string_with_length
        else Faker::Book.title
        end
      end

      private

      def length_constraint
        minLength || maxLength
      end

      def string_with_length
        Faker::Internet.user_name(min_string_length..max_string_length)
      end

      def min_string_length
        minLength || 0
      end

      def max_string_length
        maxLength || min_string_length + 10
      end

      def string_format
        (FORMAT_HANDLERS[format] || method(:unknown_format))[]
      end

      def unknown_format
        Fakeit::Logger.warn("Unknown string format: #{format}")
        'Unknown string format'
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
fakeit-0.1.5 lib/fakeit/openapi/example/string_example.rb
fakeit-0.1.4 lib/fakeit/openapi/example/string_example.rb