api_smith icon indicating copy to clipboard operation
api_smith copied to clipboard

Transforms dates by default

Open kalasjocke opened this issue 13 years ago • 4 comments

Hi,

When I have response containing a date on the form yyyy-mm-dd this is automatically transformed to a date, even without a proper Date.parse transformer.

I believe that it should be up to the developer to decide if the dates should be transformed or not, do you guys agree with me? And I also believe that this issue is created buy the Crack JSON parser, perhaps you can use another one if you? WebMock recently had the same problem, and they switched to their own version of Crack. See https://github.com/bblimke/webmock/commit/772585e7025803e786234e4957c89e1244eb7ee1

This is an example of this behavior:

require 'crack/json'
puts Crack::JSON.parse('{"date":"2010-01-01"}')

Thanks for a great gem, looking forward to use this in my project!

kalasjocke avatar Aug 17 '11 10:08 kalasjocke

And, if you define a transformer for dates everything breaks when reading a date string over HTTP with:

NoMethodError: undefined method `gsub!' for #<Date: 2011-12-31 (4911853/2,0,2299161)>

This when defining the class with a date property like:

property :date, :transformer => lambda {|date| Date.parse(date)}

This works as expected, it parses the date to a Date object:

Post.new(:date => "2010-01-01")

You can work around this if you define your transformer as:

 lambda {|date| Date.parse(date) unless date.is_a? Date}

kalasjocke avatar Aug 17 '11 11:08 kalasjocke

I experienced something related to this issue. We implemented our date transformers with the assumption that strings will be automatically transformed into date objects first. But then after an update of the httparty gem version to 0.8.0, the date strings we're not being transformed automatically anymore. So if you want the strings to not be transformed automatically, maybe try locking your httparty version to 0.8.0, if not, then use 0.7.8. Hope this may be helpful to someone.

fcbajao avatar Sep 23 '11 01:09 fcbajao

Thanks fcbajao, i actually implemented the API-consumer in an async fashion with EventMachine's HttpRequest gem instead. But thanks for the info, it will be useful in the future!

kalasjocke avatar Sep 23 '11 06:09 kalasjocke

hey guys i am having the same problem and i'm stuck, can't figure it out. can you please help me. is there a way to format the date so that it gets parsed the right way?

here is the error i'm getting

undefined method `gsub!' for Thu, 05 Jan 2012:Date

darzuaga avatar Jan 08 '12 16:01 darzuaga