Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transforms dates by default #2

Open
kalasjocke opened this issue Aug 17, 2011 · 4 comments
Open

Transforms dates by default #2

kalasjocke opened this issue Aug 17, 2011 · 4 comments

Comments

@kalasjocke
Copy link

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 bblimke/webmock@772585e

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
Copy link
Author

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}

@fcbajao
Copy link

fcbajao commented Sep 23, 2011

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.

@kalasjocke
Copy link
Author

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!

@darzuaga
Copy link

darzuaga commented Jan 8, 2012

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants