Skip to content

Commit

Permalink
Merge pull request #92 from GiveCorps/master
Browse files Browse the repository at this point in the history
Returning the string value for a field that looks like a Date/DateTime instead of raising an error when the value can't be parsed
  • Loading branch information
andyjeffries committed Apr 18, 2018
2 parents 209af76 + 202bbfe commit c5e4099
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/flexirest/attribute_parsing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ def parse_attribute_value(v)
return v if v.is_a?(Date) || v.is_a?(DateTime)

if v.to_s[(/\A(((19|20)\d\d[- \/.](0[1-9]|1[012]|[1-9])[- \/.](0[1-9]|[12][0-9]|3[01]|[1-9]))|((0[1-9]|1[012]|[1-9])[- \/.](0[1-9]|[12][0-9]|3[01]|[1-9])[- \/.](19|20)\d\d))\Z/)]
Date.parse(v)
Date.parse(v) rescue v
elsif v.to_s[/\A([\+-]?\d{4}(?!\d{2}\b))((-?)((0[1-9]|1[0-2])(\3([12]\d|0[1-9]|3[01]))?|W([0-4]\d|5[0-2])(-?[1-7])?|(00[1-9]|0[1-9]\d|[12]\d{2}|3([0-5]\d|6[1-6])))([T\s]((([01]\d|2[0-3])((:?)[0-5]\d)?|24\:?00)([\.,]\d+(?!:))?)?(\17[0-5]\d([\.,]\d+)?)?([zZ]|([\+-])([01]\d|2[0-3]):?([0-5]\d)?)?))\Z/]
DateTime.parse(v)
DateTime.parse(v) rescue v
else
v
end
Expand Down
4 changes: 4 additions & 0 deletions spec/lib/attribute_parsing_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,8 @@ def test(v)
it "should return floats for float values" do
expect(subject.test(1980.12)).to eq(1980.12)
end

it "should return as a string a date-like string that can't be parsed" do
expect(subject.test("7/29/2018")).to eq("7/29/2018")
end
end

0 comments on commit c5e4099

Please sign in to comment.