Skip to content

Commit 35ed10e

Browse files
razor-xclaude
andauthored
fix: Return nil for unparseable date fields instead of raising on read (#581)
Co-authored-by: Claude <noreply@anthropic.com>
1 parent 80ea383 commit 35ed10e

2 files changed

Lines changed: 21 additions & 3 deletions

File tree

‎lib/seam/base_resource.rb‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff 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)

‎spec/resources/base_resource_spec.rb‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,22 @@
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

0 commit comments

Comments
 (0)