imgur
imgur copied to clipboard
Please don't extend Ruby core objects
I've just spent 5 hours trying to debug why my application suddenly started crashing for no apparent reason until I found out this gem is extending Array
with to_hash
method. Please don't do this - this can cause serious issues when code expects hash or hash-like object but also supports arrays, especially considering this was not expected from a gem for external API integration.
:+1:
:+1:
To give a specific example, I added this gem to a Rails project and simply having it in the Gemfile
caused any requests containing a file encoded with multipart/form-data to throw an exception during Rack parsing, long before the controller action had any chance to fire.
[2016-04-06 12:06:55] ERROR ArgumentError: unexpected prefix: {"RackMultipart"=>nil, ".jpg"=>nil}
/Users/brit/.rbenv/versions/2.3.0/lib/ruby/2.3.0/tmpdir.rb:111:in `make_tmpname'
/Users/brit/.rbenv/versions/2.3.0/lib/ruby/2.3.0/tmpdir.rb:129:in `create'
/Users/brit/.rbenv/versions/2.3.0/lib/ruby/2.3.0/tempfile.rb:131:in `initialize'
/Users/brit/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/rack-1.6.4/lib/rack/multipart/parser.rb:22:in `new'
/Users/brit/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/rack-1.6.4/lib/rack/multipart/parser.rb:22:in `block in create'
/Users/brit/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/rack-1.6.4/lib/rack/multipart/parser.rb:135:in `get_current_head_and_filename_and_content_type_and_name_and_body'
This can be tested as easily as rails new whatever
, adding imgurapi
to the Gemfile
, and running curl -iv -F filename=test.jpg -F [email protected] http://localhost:3000/an-example
with a local file of your choosing. Since the request is parsed before routing, you don't even need to define a route to observe this failure. It is my expectation that the to_hash
extension to Arrays is the culprit.
👍 , they're not "nice extensions to base classes", I forked a version without these extensions: https://github.com/tam-vo/imgur
I have the same problem, but i didn't include gem 'imgurapi'. im using ruby 2.3.1 rails 4.2.6. So, how exactly i have to solve this issue? Btw, im newbie in rails.