From 366745b13460d6b20a167a5dce64d59deafabf68 Mon Sep 17 00:00:00 2001 From: Carlos Antonio da Silva <carlosantoniodasilva@gmail.com> Date: Thu, 10 Nov 2022 11:14:51 -0300 Subject: [PATCH] Replace `mime-types` with `mini_mime` for content type lookup `mini_mime` is a minimal mime type library that's more performant and less memory hungry. https://github.com/discourse/mini_mime It has replaced `mime-types` in the `mail` gem: (which is a dependency of `actionmailer`, and by extension, `rails`) https://github.com/mikel/mail/pull/1059 As well as `capybara`: https://github.com/teamcapybara/capybara/pull/1884 Which also means using it as a dependency of `httparty` would be able to reuse the same dependency that should be already available in most Rails apps, instead of pulling in an extra `mime-types` dependency. The change in code is pretty straightforward, the same one made by the capybara PR linked above. --- Changelog.md | 1 + httparty.gemspec | 2 +- lib/httparty.rb | 2 +- lib/httparty/request/body.rb | 4 ++-- 4 files changed, 5 insertions(+), 4 deletions(-) diff --git a/Changelog.md b/Changelog.md index 139438d8..50637a66 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,6 +1,7 @@ ## unreleased * Fix request marshaling (https://github.com/jnunemaker/httparty/pull/767) +* [Replace `mime-types` with `mini_mime`](https://github.com/jnunemaker/httparty/pull/769) ## 0.20.0 diff --git a/httparty.gemspec b/httparty.gemspec index bec51d60..a0f251b8 100644 --- a/httparty.gemspec +++ b/httparty.gemspec @@ -16,7 +16,7 @@ Gem::Specification.new do |s| s.required_ruby_version = '>= 2.3.0' s.add_dependency 'multi_xml', ">= 0.5.2" - s.add_dependency('mime-types', "~> 3.0") + s.add_dependency 'mini_mime', ">= 1.0.0" # If this line is removed, all hard partying will cease. s.post_install_message = "When you HTTParty, you must party hard!" diff --git a/lib/httparty.rb b/lib/httparty.rb index cf20488b..772ffcac 100644 --- a/lib/httparty.rb +++ b/lib/httparty.rb @@ -5,7 +5,7 @@ require 'uri' require 'zlib' require 'multi_xml' -require 'mime/types' +require 'mini_mime' require 'json' require 'csv' diff --git a/lib/httparty/request/body.rb b/lib/httparty/request/body.rb index 4479e1b6..f498cb3d 100644 --- a/lib/httparty/request/body.rb +++ b/lib/httparty/request/body.rb @@ -84,8 +84,8 @@ def content_body(object) def content_type(object) return object.content_type if object.respond_to?(:content_type) - mime = MIME::Types.type_for(object.path) - mime.empty? ? 'application/octet-stream' : mime[0].content_type + mime = MiniMime.lookup_by_filename(object.path) + mime ? mime.content_type : 'application/octet-stream' end def file_name(object)