Sha256: 5d1d9906004714ca1c2c28d3e997e2b9112f1e99499d09bd8d5bd474fcf739e3

Contents?: true

Size: 931 Bytes

Versions: 6

Compression:

Stored size: 931 Bytes

Contents

# return always utc-timestam in iso format 'YYYYMMDDhhmmss'
module Reactor
  module Attributes
    class DateSerializer
      def initialize(key, value)
        @attr = key
        @value = value
      end

      def serialize
        @serialized ||= serialize_date(@value)
      end

      private

      def serialize_date(value)
        return nil if value.blank?

        value = value.to_datetime if value.is_a?(Date)
        if value.is_a?(Time) || value.is_a?(DateTime)
          value.utc.to_s(:number)
        elsif value.is_a?(String)
          if iso_format?(value)
            value
          else
            parse_time(value)
          end
        end
      end

      def parse_time(value)
        parsed_time = Time.zone.parse(value)
        return nil unless parsed_time.present?

        parsed_time.utc.to_s(:number)
      end

      def iso_format?(val)
        val =~ /^[0-9]{14}$/
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
infopark_reactor-1.29.1 lib/reactor/attributes/date_serializer.rb
infopark_reactor-1.29.0 lib/reactor/attributes/date_serializer.rb
infopark_reactor-1.28.0 lib/reactor/attributes/date_serializer.rb
infopark_reactor-1.27.0.rc10 lib/reactor/attributes/date_serializer.rb
infopark_reactor-1.27.0.rc9 lib/reactor/attributes/date_serializer.rb
infopark_reactor-1.27.0.rc8 lib/reactor/attributes/date_serializer.rb