module Faraday::Utils

Experimental RBS support (using type sampling data from the type_fusion project).

# sig/faraday/utils.rbs

module Faraday::Utils
  def escape: (String str) -> untyped
end

def URI(url) # rubocop:disable Naming/MethodName

rubocop:disable Naming/MethodName
Returns a parsed URI.

url - A String or URI.

Normalize URI() behavior across Ruby versions
def URI(url) # rubocop:disable Naming/MethodName
  if url.respond_to?(:host)
    url
  elsif url.respond_to?(:to_str)
    default_uri_parser.call(url)
  else
    raise ArgumentError, 'bad argument (expected URI object or URI string)'
  end
end

def basic_header_from(login, pass)

def basic_header_from(login, pass)
  value = Base64.encode64("#{login}:#{pass}")
  value.delete!("\n")
  "Basic #{value}"
end

def build_nested_query(params)

def build_nested_query(params)
  NestedParamsEncoder.encode(params)
end

def build_query(params)

def build_query(params)
  FlatParamsEncoder.encode(params)
end

def deep_merge(source, hash)

Recursive hash merge
def deep_merge(source, hash)
  deep_merge!(source.dup, hash)
end

def deep_merge!(target, hash)

Recursive hash update
def deep_merge!(target, hash)
  hash.each do |key, value|
    target[key] = if value.is_a?(Hash) && (target[key].is_a?(Hash) || target[key].is_a?(Options))
                    deep_merge(target[key], value)
                  else
                    value
                  end
  end
  target
end

def default_params_encoder

def default_params_encoder
  @default_params_encoder ||= NestedParamsEncoder
end

def default_space_encoding

def default_space_encoding
  @default_space_encoding ||= '+'
end

def default_uri_parser

def default_uri_parser
  @default_uri_parser ||= Kernel.method(:URI)
end

def default_uri_parser=(parser)

def default_uri_parser=(parser)
  @default_uri_parser = if parser.respond_to?(:call) || parser.nil?
                          parser
                        else
                          parser.method(:parse)
                        end
end

def escape(str)

Experimental RBS support (using type sampling data from the type_fusion project).

def escape: (String str) -> untyped

This signature was generated using 1 sample from 1 application.

def escape(str)
  str.to_s.gsub(ESCAPE_RE) do |match|
    "%#{match.unpack('H2' * match.bytesize).join('%').upcase}"
  end.gsub(' ', default_space_encoding)
end

def normalize_path(url)

the path with the query string sorted.
Receives a String or URI and returns just
def normalize_path(url)
  url = URI(url)
  (url.path.start_with?('/') ? url.path : "/#{url.path}") +
    (url.query ? "?#{sort_query_params(url.query)}" : '')
end

def parse_nested_query(query)

def parse_nested_query(query)
  NestedParamsEncoder.decode(query)
end

def parse_query(query)

Adapted from Rack
def parse_query(query)
  FlatParamsEncoder.decode(query)
end

def sort_query_params(query)

def sort_query_params(query)
  query.split('&').sort.join('&')
end

def unescape(str)

def unescape(str)
  CGI.unescape str.to_s
end