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

HTTParty::Request::Body#multipart? fixes #626

Merged
merged 3 commits into from
Jan 25, 2019

Conversation

EmilioCristalli
Copy link
Contributor

This PR intends to fix a few issues with the multipart? method:

  1. It wasn't working for params that respond to #to_hash but not to #detect (like ActionDispatch::Request::Session). Same for params that respond to #to_ary but not to #detect
class HashLike; def to_hash; { a: 3 }; end; end
HTTParty::Request::Body.new(a: HashLike.new).multipart? # NoMethodError: undefined method `detect' for #<HashLike:0x00007fbf8971ae08>
  1. Wasn't detecting files inside arrays
    (includes_hash?(value) was always returning true for not empty arrays, because all objects respond to #hash, and then it was doing hash.detect do |key, value| with an array which actually yields only one arg)
HTTParty::Request::Body.new(a: [Tempfile.new(['some_temp_file','.gif'])]).multipart? # false
  1. looked like it was intended to be recursive and look for a file inside all hash values or array elements recursively but I think it wasn't doing it (if the method was not supposed to be recursive please let me know because this might impact performance)

@FLarra
Copy link

FLarra commented Dec 19, 2018

@TheSmartnik It'd be great to have this PR merged as soon as you feel confident with it.

I'm maintaining the gem https://github.com/smartinez87/exception_notification which relies on HTTParty in several notifiers and they are not working if updating HTTParty version. The last working version is on tag: https://github.com/jnunemaker/httparty/releases/tag/v0.15.7

It'd be great to merge this #multipart? fix so we don't need to lock our users to versions prior to the multipart refactor.

Cheers

Copy link
Collaborator

@TheSmartnik TheSmartnik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot for the PR, sorry it took a long time to respond. It looks good, a few minor comments about naming and we are good to go.

else
file?(value)
end
def file?(value)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like #has_file? is better a naming for this method, anyway.
You still ask whether the params have a file or not. You're right that it can be file, but it can also be hash, array and #has_file? is a more inclusive naming

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, you are right. I changed here 5a778ed

elsif value.respond_to?(:to_ary)
value.to_ary.any? { |v| file?(v) }
else
value.respond_to?(:path) && value.respond_to?(:read)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previous use of a method was more readable and understandable , imho.
It showed that if value responds to path and read it is a file, which is exactly what we are looking for.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you mean this method?

      def file?(object)
        object.respond_to?(:path) && object.respond_to?(:read)
      end

if that's what you meant, i think it makes sense, and also would help for the generate_multipart which should only check if the value is a file (not recursive) if i understand it correctly.

I changed it here as well: 5a778ed

@EmilioCristalli
Copy link
Contributor Author

@TheSmartnik thanks for the review!
I think i addressed your comments, but let me know if i misunderstood them or if you have any other comments!

@TheSmartnik
Copy link
Collaborator

Thanks! Look good. I'll run a few more tests later today and bump the version

@TheSmartnik TheSmartnik merged commit ad80a4c into jnunemaker:master Jan 25, 2019
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

Successfully merging this pull request may close these issues.

3 participants