integrating .sum and .filter_map + backports

This commit is contained in:
HoneyryderChuck 2021-09-03 17:12:45 +01:00
parent 0575c87dec
commit d350bebe81
7 changed files with 58 additions and 20 deletions

View File

@ -22,6 +22,8 @@ module Faraday
# :nocov:
module RequestMixin
using ::HTTPX::HashExtensions
private
def build_request(env)

View File

@ -54,6 +54,51 @@ module HTTPX
Numeric.__send__(:include, NegMethods)
end
module HashExtensions
refine Hash do
def compact
h = {}
each do |key, value|
h[key] = value unless value == nil
end
h
end unless Hash.method_defined?(:compact)
end
end
module ArrayExtensions
refine Array do
def filter_map
return to_enum(:filter_map) unless block_given?
each_with_object([]) do |item, res|
processed = yield(item)
res << processed if processed
end
end unless Array.method_defined?(:filter_map)
def sum(accumulator = 0, &block)
values = block_given? ? map(&block) : self
values.inject(accumulator, :+)
end unless Array.method_defined?(:sum)
end
end
module IOExtensions
refine IO do
# provides a fallback for rubies where IO#wait isn't implemented,
# but IO#wait_readable and IO#wait_writable are.
def wait(timeout = nil, _mode = :read_write)
r, w = IO.select([self], [self], nil, timeout)
return unless r || w
self
end unless IO.method_defined?(:wait) && IO.instance_method(:wait).arity == 2
end
end
module RegexpExtensions
# If you wonder why this is there: the oauth feature uses a refinement to enhance the
# Regexp class locally with #match? , but this is never tested, because ActiveSupport

View File

@ -72,6 +72,8 @@ module HTTPX
end
module ResponseBodyMethods
using ArrayExtensions
attr_reader :encodings
def initialize(*)
@ -90,7 +92,7 @@ module HTTPX
Float::INFINITY
end
@_inflaters = @headers.get("content-encoding").map do |encoding|
@_inflaters = @headers.get("content-encoding").filter_map do |encoding|
next if encoding == "identity"
inflater = @options.encodings.registry(encoding).inflater(compressed_length)
@ -100,7 +102,7 @@ module HTTPX
@encodings << encoding
inflater
end.compact
end
# this can happen if the only declared encoding is "identity"
remove_instance_variable(:@_inflaters) if @_inflaters.empty?
@ -134,7 +136,7 @@ module HTTPX
end
def each(&blk)
return enum_for(__method__) unless block_given?
return enum_for(__method__) unless blk
return deflate(&blk) if @buffer.size.zero?

View File

@ -8,6 +8,7 @@ require "httpx/resolver"
module HTTPX
class Pool
using ArrayExtensions
extend Forwardable
def_delegator :@timers, :after
@ -162,7 +163,7 @@ module HTTPX
end
def next_timeout
@resolvers.values.reject(&:closed?).map(&:timeout).compact.min || @connections.map(&:timeout).compact.min
@resolvers.values.reject(&:closed?).filter_map(&:timeout).min || @connections.filter_map(&:timeout).min
end
def find_resolver_for(connection)

View File

@ -2,20 +2,6 @@
require "io/wait"
module IOExtensions
refine IO do
# provides a fallback for rubies where IO#wait isn't implemented,
# but IO#wait_readable and IO#wait_writable are.
def wait(timeout = nil, _mode = :read_write)
r, w = IO.select([self], [self], nil, timeout)
return unless r || w
self
end
end
end
class HTTPX::Selector
READABLE = %i[rw r].freeze
WRITABLE = %i[rw w].freeze
@ -23,7 +9,7 @@ class HTTPX::Selector
private_constant :READABLE
private_constant :WRITABLE
using IOExtensions unless IO.method_defined?(:wait) && IO.instance_method(:wait).arity == 2
using HTTPX::IOExtensions
def initialize
@selectables = []

View File

@ -9,6 +9,7 @@ module HTTPX::Transcoder
module_function
class Encoder
using HTTPX::ArrayExtensions
extend Forwardable
def_delegator :@raw, :to_s
@ -21,7 +22,7 @@ module HTTPX::Transcoder
if @raw.respond_to?(:bytesize)
@raw.bytesize
elsif @raw.respond_to?(:to_ary)
@raw.map(&:bytesize).reduce(0, :+)
@raw.sum(&:bytesize)
elsif @raw.respond_to?(:size)
@raw.size || Float::INFINITY
elsif @raw.respond_to?(:length)

View File

@ -4,6 +4,7 @@ require_relative "test_helper"
class OptionsTest < Minitest::Test
include HTTPX
using HashExtensions
def test_options_unknown
ex = assert_raises(Error) { Options.new(foo: "bar") }