Skip to content

Commit

Permalink
Merge pull request #760 from orgads/lazy-require
Browse files Browse the repository at this point in the history
Lazy load some gems
  • Loading branch information
jnunemaker authored Mar 20, 2024
2 parents 5b25ae9 + 2116595 commit b9bd421
Show file tree
Hide file tree
Showing 7 changed files with 10 additions and 5 deletions.
2 changes: 2 additions & 0 deletions features/steps/mongrel_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
5 changes: 0 additions & 5 deletions lib/httparty.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
1 change: 1 addition & 0 deletions lib/httparty/logger/logstash_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def format(request, response)
attr_reader :request, :response

def logstash_message
require 'json'
{
'@timestamp' => current_time,
'@version' => 1,
Expand Down
3 changes: 3 additions & 0 deletions lib/httparty/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions lib/httparty/request/body.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions spec/httparty/logger/logstash_formatter_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'json'
require 'spec_helper'

RSpec.describe HTTParty::Logger::LogstashFormatter do
Expand Down
2 changes: 2 additions & 0 deletions spec/httparty/parser_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'spec_helper'
require 'multi_xml'

RSpec.describe HTTParty::Parser do
describe ".SupportedFormats" do
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit b9bd421

Please sign in to comment.