Sha256: b00a6bc82473fd6f86ca0a62b370e7d8aeb5874ff226065caf7e438ebd6d77c3

Contents?: true

Size: 1.6 KB

Versions: 19

Compression:

Stored size: 1.6 KB

Contents

# encoding: utf-8
#
# = 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 value.blank?
        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

19 entries across 18 versions & 9 rubygems

Version Path
mail-2.5.5 lib/mail/fields/date_field.rb
mail-2.5.5.rc1 lib/mail/fields/date_field.rb
angular-rails4-templates-0.4.1 vendor/ruby/2.1.0/gems/mail-2.6.3/lib/mail/fields/date_field.rb
angular-rails4-templates-0.4.0 vendor/ruby/2.1.0/gems/mail-2.6.3/lib/mail/fields/date_field.rb
tdiary-4.2.1 vendor/bundle/ruby/2.2.0/gems/mail-2.6.3/lib/mail/fields/date_field.rb
tdiary-4.2.1 vendor/bundle/ruby/2.3.0/gems/mail-2.6.3/lib/mail/fields/date_field.rb
angular-rails4-templates-0.3.0 vendor/ruby/2.1.0/gems/mail-2.6.3/lib/mail/fields/date_field.rb
sc_core-0.0.7 test/dummy/vendor/bundle/ruby/2.2.0/gems/mail-2.6.3/lib/mail/fields/date_field.rb
solidus_backend-1.0.0.pre3 vendor/bundle/gems/mail-2.6.3/lib/mail/fields/date_field.rb
solidus_backend-1.0.0.pre2 vendor/bundle/gems/mail-2.6.3/lib/mail/fields/date_field.rb
solidus_backend-1.0.0.pre vendor/bundle/gems/mail-2.6.3/lib/mail/fields/date_field.rb
shoppe-paypal-1.1.0 vendor/bundle/ruby/2.1.0/gems/mail-2.6.3/lib/mail/fields/date_field.rb
mail-2.6.3 lib/mail/fields/date_field.rb
mail-portertech-2.6.2.edge lib/mail/fields/date_field.rb
mail-2.6.1 lib/mail/fields/date_field.rb
mail-2.6.0 lib/mail/fields/date_field.rb
challah-1.0.0 vendor/bundle/gems/mail-2.5.4/lib/mail/fields/date_field.rb
swipe-rails-0.0.5 vendor/bundle/gems/mail-2.5.4/lib/mail/fields/date_field.rb
mail-2.5.4 lib/mail/fields/date_field.rb