Sha256: 8e886951cc89551c2e702423f5d6791af718c461b31dbb4bf9c949894a456768

Contents?: true

Size: 1.5 KB

Versions: 5

Compression:

Stored size: 1.5 KB

Contents

module Wukong
  module Elasticsearch

    # A class that makes Ruby's Time class serialize the way
    # Elasticsearch expects.
    #
    # Elasticsearch's date parsing engine [expects to
    # receive](http://www.elasticsearch.org/guide/reference/mapping/date-format.html)
    # a date formatted according to the Java library
    # [Joda's](http://joda-time.sourceforge.net/)
    # [ISODateTimeFormat.dateOptionalTimeParser](http://joda-time.sourceforge.net/api-release/org/joda/time/format/ISODateTimeFormat.html#dateOptionalTimeParser())
    # class.
    #
    # This format looks like this: `2012-11-30T01:15:23`.
    #
    # @see http://www.elasticsearch.org/guide/reference/mapping/date-format.html The Elasticsearch guide's Date Format entry
    # @see http://joda-time.sourceforge.net/api-release/org/joda/time/format/ISODateTimeFormat.html#dateOptionalTimeParser() The Joda class's API documentation
    class Timestamp < Time

      # Parses the given `string` into a Timestamp instance.
      #
      # @param [String] string
      # @return [Timestamp]
      def self.receive string
        return if string.nil? || string.empty?
        begin
          t = Time.parse(string)
        rescue ArgumentError => e
          return
        end
        new(t.year, t.month, t.day, t.hour, t.min, t.sec, t.utc_offset)
      end

      # Formats the Timestamp according to ISO 8601 rules.
      #
      # @param [Hash] options
      # @return [String]
      def to_wire(options={})
        utc.iso8601
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
wonderdog-0.2.0 lib/wonderdog/timestamp.rb
wonderdog-0.1.1 lib/wonderdog/timestamp.rb
wonderdog-0.1.0 lib/wonderdog/timestamp.rb
wonderdog-0.0.2 lib/wonderdog/timestamp.rb
wonderdog-0.0.1 lib/wonderdog/timestamp.rb