Sha256: 26d3f92dd4fe2cad29ad3b93ab8d34542ff250bcfe492145b45c8d53a0e91424

Contents?: true

Size: 740 Bytes

Versions: 2

Compression:

Stored size: 740 Bytes

Contents

module Restspec::Schema::Types
  class DateTimeType < BasicType
    def example_for(attribute)
      Faker::Time.between(initial_example_interval, final_example_interval).iso8601
    end

    def valid?(attribute, value)
      return false unless value.present?
      allowed_date_time_formats.any? do |format|
        DateTime.parse(value).strftime(format) == value
      end
    rescue ArgumentError
      false
    end

    private

    def allowed_date_time_formats
      ['%Y-%m-%dT%H:%M:%S.%LZ', '%Y-%m-%dT%H:%M:%S%Z']
    end

    def initial_example_interval
      example_options.fetch(:initial_interval, 1.month.ago)
    end

    def final_example_interval
      example_options.fetch(:final_interval, Time.now)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
restspec-0.0.4 lib/restspec/schema/types/datetime_type.rb
restspec-0.0.3 lib/restspec/schema/types/datetime_type.rb