File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -103,17 +103,19 @@ def self.attribute_aliases
103103 def self . date_accessor ( *attrs )
104104 attrs . each do |attr |
105105 define_method ( attr ) do
106- value = instance_variable_get ( :"@#{ attr } " )
107-
108- value . nil? ? nil : parse_datetime ( value )
106+ parse_datetime ( instance_variable_get ( :"@#{ attr } " ) )
109107 end
110108 end
111109 end
112110
113111 protected
114112
115113 def parse_datetime ( value )
114+ return nil unless value . is_a? ( String )
115+
116116 Time . parse ( value )
117+ rescue ArgumentError
118+ nil
117119 end
118120
119121 def process_data_attributes ( data )
Original file line number Diff line number Diff line change 1414 it "parses a date string" do
1515 expect ( device . created_at ) . to be_a ( Time )
1616 end
17+
18+ it "returns nil for a missing date" do
19+ expect ( Seam ::Resources ::Device . new ( device_id : "device_id_1234" ) . created_at ) . to be_nil
20+ end
21+
22+ it "returns nil for an empty string" do
23+ expect ( Seam ::Resources ::Device . new ( created_at : "" ) . created_at ) . to be_nil
24+ end
25+
26+ it "returns nil for a string that is not a date" do
27+ expect ( Seam ::Resources ::Device . new ( created_at : "not a date" ) . created_at ) . to be_nil
28+ end
29+
30+ it "returns nil for an epoch integer" do
31+ expect ( Seam ::Resources ::Device . new ( created_at : 1_654_641_254 ) . created_at ) . to be_nil
32+ end
1733 end
1834
1935 describe ".load_from_response" do
You can’t perform that action at this time.
0 commit comments