whichdigital / active-rest-client

ActiveRestClient API Client
https://rubygems.org/gems/active_rest_client
MIT License
386 stars 44 forks source link

Issue with automatic DateTime parse Base.initialize #113

Open wgreene opened 8 years ago

wgreene commented 8 years ago

Hello, I ran into an issue today where I was instantiating an object directly with a hash for which one of the values was a 4 digit integer. The following exception was raised: no implicit conversion of Fixnum into String

I tracked the problem down to lines 29-30 in ActiveRestClient::Base.rb in the initialize method.

        elsif attribute_value.to_s[/^([\+-]?\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)?)?)?)?$/]
          @attributes[attribute_name] = DateTime.parse(attribute_value)

Here's an example of the attrs hash passed to new that causes the exception:

{
:name=>"How to Build and Maintain Good Credit", 
:view_count=>1001,  
:published_at=>"2015-10-08T20:59:55Z"
}

The exception is thrown as it interprets the view_count 1001 as matching the DateTime regex.

I would appreciate the help if I am missing something here. Similar values appear to instantiate without issue when done via get. I only see this problem when calling new directly.

I patched this locally by rescuing the exception...e.g:

          @attributes[attribute_name] = DateTime.parse(attribute_value) rescue attribute_value

In our live environment I modified the solr result to format the view_count as a float (1001.0) which bypasses the reggae match. Do you know of a better work around?

Thanks so much. Again, this library has been a great help. If you would like any assistance with it, I'd be happy to fork it and implement any solution you might suggest.

-Will

andyjeffries commented 8 years ago

As the original author of most of the code in this gem, when it wasn't being maintained I forked it last year. I believe this bug was fixed in Flexirest 1.2.10. It's interface compatible with ActiveRestClient, you just need to change all instances of ActiveRestClient to Flexirest and update your Gemfile.

Let me know on https://github.com/andyjeffries/flexirest if it's not working and I'll fix it there...

wgreene commented 8 years ago

Thank you so much! Flexirest is working perfectly. Had no trouble migrating over.

andyjeffries commented 8 years ago

Glad to hear it. You're welcome.