Sha256: 3e9cb694bc939b5b11158fe5f16a06fe91e8701cf46710fa4933e8b6afc16a4f

Contents?: true

Size: 1.64 KB

Versions: 23

Compression:

Stored size: 1.64 KB

Contents

# encoding: utf-8
# frozen_string_literal: true
#
# = Date Field
#
# The Date field inherits from StructuredField and handles the Date: header
# field in the email.
#
# Sending date to a mail message will instantiate a Mail::Field object that
# has a DateField as its field type.  This includes all Mail::CommonAddress
# module instance methods.
#
# There must be excatly one Date field in an RFC2822 email.
#
# == Examples:
#
#  mail = Mail.new
#  mail.date = 'Mon, 24 Nov 1997 14:22:01 -0800'
#  mail.date       #=> #<DateTime: 211747170121/86400,-1/3,2299161>
#  mail.date.to_s  #=> 'Mon, 24 Nov 1997 14:22:01 -0800'
#  mail[:date]     #=> '#<Mail::Field:0x180e5e8 @field=#<Mail::DateField:0x180e1c4
#  mail['date']    #=> '#<Mail::Field:0x180e5e8 @field=#<Mail::DateField:0x180e1c4
#  mail['Date']    #=> '#<Mail::Field:0x180e5e8 @field=#<Mail::DateField:0x180e1c4
#
require 'mail/fields/common/common_date'

module Mail
  class DateField < StructuredField

    include Mail::CommonDate

    FIELD_NAME = 'date'
    CAPITALIZED_FIELD = "Date"

    def initialize(value = nil, charset = 'utf-8')
      self.charset = charset
      if Utilities.blank?(value)
        value = ::DateTime.now.strftime('%a, %d %b %Y %H:%M:%S %z')
      else
        value = strip_field(FIELD_NAME, value)
        value.to_s.gsub!(/\(.*?\)/, '')
        value = ::DateTime.parse(value.to_s.squeeze(" ")).strftime('%a, %d %b %Y %H:%M:%S %z')
      end
      super(CAPITALIZED_FIELD, value, charset)
    rescue ArgumentError => e
      raise e unless "invalid date"==e.message
    end

    def encoded
      do_encode(CAPITALIZED_FIELD)
    end

    def decoded
      do_decode
    end

  end
end

Version data entries

23 entries across 21 versions & 6 rubygems

Version Path
tdiary-5.0.6 vendor/bundle/gems/mail-2.6.6/lib/mail/fields/date_field.rb
tdiary-5.0.5 vendor/bundle/gems/mail-2.6.4/lib/mail/fields/date_field.rb
tdiary-5.0.5 vendor/bundle/gems/mail-2.6.6/lib/mail/fields/date_field.rb
tdiary-5.0.5 vendor/bundle/gems/tdiary-5.0.4/vendor/bundle/gems/mail-2.6.4/lib/mail/fields/date_field.rb
mail-2.6.6 lib/mail/fields/date_field.rb
mail-2.7.0.rc1 lib/mail/fields/date_field.rb
mail-2.6.6.rc1 lib/mail/fields/date_field.rb
enju_leaf-1.2.1 vendor/bundle/ruby/2.3/gems/mail-2.6.5/lib/mail/fields/date_field.rb
mail-2.6.5 lib/mail/fields/date_field.rb
mail-2.6.5.rc1 lib/mail/fields/date_field.rb
tdiary-5.0.4 vendor/bundle/gems/mail-2.6.4/lib/mail/fields/date_field.rb
autocompl-0.2.2 test/dummy/vendor/bundle/ruby/2.3.0/gems/mail-2.6.4/lib/mail/fields/date_field.rb
autocompl-0.2.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/mail-2.6.4/lib/mail/fields/date_field.rb
autocompl-0.2.0 test/dummy/vendor/bundle/ruby/2.3.0/gems/mail-2.6.4/lib/mail/fields/date_field.rb
autocompl-0.1.2 test/dummy/vendor/bundle/ruby/2.3.0/gems/mail-2.6.4/lib/mail/fields/date_field.rb
autocompl-0.1.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/mail-2.6.4/lib/mail/fields/date_field.rb
autocompl-0.1.0 test/dummy/vendor/bundle/ruby/2.3.0/gems/mail-2.6.4/lib/mail/fields/date_field.rb
autocompl-0.0.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/mail-2.6.4/lib/mail/fields/date_field.rb
abaci-0.3.0 vendor/bundle/gems/mail-2.6.4/lib/mail/fields/date_field.rb
tdiary-5.0.2 vendor/bundle/gems/mail-2.6.4/lib/mail/fields/date_field.rb