class Restspec::Schema::Types::DateTimeType

Manages DateTime

Options:

Example:
  initial_interval: The initial interval (default: 2 months ago)
  final_interval: The final interval (default: `Time.now`)

Public Instance Methods

example_for(attribute) click to toggle source

Generates an example time formatted as ISO8601.

@param attribute [Restspec::Schema::Attribute] the atribute of the schema. @return [string] A time between 1 month ago and now formatted as ISO8601.

# File lib/restspec/schema/types/datetime_type.rb, line 13
def example_for(attribute)
  Faker::Time.between(initial_example_interval, final_example_interval).iso8601
end
valid?(attribute, value) click to toggle source

Validates is the value is a correct time according to ISO8601.

@param attribute [Restspec::Schema::Attribute] the atribute of the schema. @param value [Object] the value of the attribute.

@return [true, false] If the value is accord to ISO8601.

# File lib/restspec/schema/types/datetime_type.rb, line 23
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 Instance Methods

allowed_date_time_formats() click to toggle source
# File lib/restspec/schema/types/datetime_type.rb, line 34
def allowed_date_time_formats
  ['%Y-%m-%dT%H:%M:%S.%LZ', '%Y-%m-%dT%H:%M:%S%Z']
end
final_example_interval() click to toggle source
# File lib/restspec/schema/types/datetime_type.rb, line 42
def final_example_interval
  example_options.fetch(:final_interval, Time.now)
end
initial_example_interval() click to toggle source
# File lib/restspec/schema/types/datetime_type.rb, line 38
def initial_example_interval
  example_options.fetch(:initial_interval, 1.month.ago)
end