From 404f7d8290c6e97d3fc778c5223d79fa12cff4de Mon Sep 17 00:00:00 2001 From: Guillaume DOTT Date: Tue, 3 Sep 2013 16:57:37 +0200 Subject: [PATCH] Use DateTime object for date in Sms class --- lib/biju/sms.rb | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/lib/biju/sms.rb b/lib/biju/sms.rb index 175ab8b..d576aed 100644 --- a/lib/biju/sms.rb +++ b/lib/biju/sms.rb @@ -1,6 +1,9 @@ +require 'date' + module Biju class Sms - attr_accessor :id, :phone_number, :datetime, :message + attr_accessor :id, :phone_number, :message + attr_reader :datetime def initialize(params={}) params.each do |attr, value| @@ -8,8 +11,15 @@ module Biju end if params end - def datetime - @datetime.sub(/(\d+)\D+(\d+)\D+(\d+),(\d*\D)(\d*\D)(\d+)(.*)/, '20\1-\2-\3 \4\5\6') + def datetime=(arg) + @datetime = case arg + when String + DateTime.strptime(arg, "%y/%m/%d,%T") + when DateTime + arg + else + nil + end end def to_s