From 2116595ab4afc85f592f258c171f2ab2ef39ee80 Mon Sep 17 00:00:00 2001 From: Orgad Shaneh Date: Thu, 18 Aug 2022 16:28:35 +0300 Subject: [PATCH] Lazy load some gems Only load them when they are used. --- features/steps/mongrel_helper.rb | 2 ++ lib/httparty.rb | 5 ----- lib/httparty/logger/logstash_formatter.rb | 1 + lib/httparty/parser.rb | 3 +++ lib/httparty/request/body.rb | 1 + spec/httparty/logger/logstash_formatter_spec.rb | 1 + spec/httparty/parser_spec.rb | 2 ++ 7 files changed, 10 insertions(+), 5 deletions(-) diff --git a/features/steps/mongrel_helper.rb b/features/steps/mongrel_helper.rb index 5d78895d..dcab043c 100644 --- a/features/steps/mongrel_helper.rb +++ b/features/steps/mongrel_helper.rb @@ -30,6 +30,7 @@ def process(request, response) reply_with(response, 406, 'No deflate accept encoding found in request') else response.start do |head, body| + require 'zlib' head['Content-Encoding'] = 'deflate' body.write Zlib::Deflate.deflate(response_body) end @@ -53,6 +54,7 @@ def process(request, response) protected def gzip(string) + require 'zlib' sio = StringIO.new('', 'r+') gz = Zlib::GzipWriter.new sio gz.write string diff --git a/lib/httparty.rb b/lib/httparty.rb index 4a7529cf..d8a9a4c6 100644 --- a/lib/httparty.rb +++ b/lib/httparty.rb @@ -3,11 +3,6 @@ require 'pathname' require 'net/http' require 'uri' -require 'zlib' -require 'multi_xml' -require 'mini_mime' -require 'json' -require 'csv' require 'httparty/module_inheritable_attributes' require 'httparty/cookie_hash' diff --git a/lib/httparty/logger/logstash_formatter.rb b/lib/httparty/logger/logstash_formatter.rb index b029cf31..dd129ec9 100644 --- a/lib/httparty/logger/logstash_formatter.rb +++ b/lib/httparty/logger/logstash_formatter.rb @@ -24,6 +24,7 @@ def format(request, response) attr_reader :request, :response def logstash_message + require 'json' { '@timestamp' => current_time, '@version' => 1, diff --git a/lib/httparty/parser.rb b/lib/httparty/parser.rb index 272b9fd2..44451ce4 100644 --- a/lib/httparty/parser.rb +++ b/lib/httparty/parser.rb @@ -118,16 +118,19 @@ def parse protected def xml + require 'multi_xml' MultiXml.parse(body) end UTF8_BOM = "\xEF\xBB\xBF" def json + require 'json' JSON.parse(body, :quirks_mode => true, :allow_nan => true) end def csv + require 'csv' CSV.parse(body) end diff --git a/lib/httparty/request/body.rb b/lib/httparty/request/body.rb index 0de9de6d..3b0e6a0f 100644 --- a/lib/httparty/request/body.rb +++ b/lib/httparty/request/body.rb @@ -91,6 +91,7 @@ def content_body(object) def content_type(object) return object.content_type if object.respond_to?(:content_type) + require 'mini_mime' mime = MiniMime.lookup_by_filename(object.path) mime ? mime.content_type : 'application/octet-stream' end diff --git a/spec/httparty/logger/logstash_formatter_spec.rb b/spec/httparty/logger/logstash_formatter_spec.rb index 303dd5e6..6e1e080c 100644 --- a/spec/httparty/logger/logstash_formatter_spec.rb +++ b/spec/httparty/logger/logstash_formatter_spec.rb @@ -1,3 +1,4 @@ +require 'json' require 'spec_helper' RSpec.describe HTTParty::Logger::LogstashFormatter do diff --git a/spec/httparty/parser_spec.rb b/spec/httparty/parser_spec.rb index e19f0859..a23597fc 100644 --- a/spec/httparty/parser_spec.rb +++ b/spec/httparty/parser_spec.rb @@ -1,4 +1,5 @@ require 'spec_helper' +require 'multi_xml' RSpec.describe HTTParty::Parser do describe ".SupportedFormats" do @@ -183,6 +184,7 @@ def self.name end it "parses csv with CSV" do + require 'csv' expect(CSV).to receive(:parse).with('body') subject.send(:csv) end