Module: Faraday::Utils

Defined in:
lib/faraday/utils.rb,
lib/faraday/utils/headers.rb,
lib/faraday/utils/params_hash.rb

Overview

Utils contains various static helper methods.

Defined Under Namespace

Classes: Headers, ParamsHash

Constant Summary collapse

ESCAPE_RE =
/[^a-zA-Z0-9 .~_-]/
DEFAULT_SEP =
/[&;] */n

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.default_params_encoderObject



51
52
53
# File 'lib/faraday/utils.rb', line 51

def default_params_encoder
  @default_params_encoder ||= NestedParamsEncoder
end

.default_space_encodingObject



20
21
22
# File 'lib/faraday/utils.rb', line 20

def default_space_encoding
  @default_space_encoding ||= '+'
end

Class Method Details

.basic_header_from(login, pass) ⇒ Object



55
56
57
58
59
# File 'lib/faraday/utils.rb', line 55

def basic_header_from(, pass)
  value = ["#{}:#{pass}"].pack('m') # Base64 encoding
  value.delete!("\n")
  "Basic #{value}"
end

.build_nested_query(params) ⇒ Object



16
17
18
# File 'lib/faraday/utils.rb', line 16

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

.build_query(params) ⇒ Object



12
13
14
# File 'lib/faraday/utils.rb', line 12

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

.deep_merge(source, hash) ⇒ Object

Recursive hash merge



113
114
115
# File 'lib/faraday/utils.rb', line 113

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

.deep_merge!(target, hash) ⇒ Object

Recursive hash update



101
102
103
104
105
106
107
108
109
110
# File 'lib/faraday/utils.rb', line 101

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

.default_uri_parserObject



80
81
82
# File 'lib/faraday/utils.rb', line 80

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

.default_uri_parser=(parser) ⇒ Object



84
85
86
87
88
89
90
# File 'lib/faraday/utils.rb', line 84

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

.escape(str) ⇒ Object



30
31
32
33
34
# File 'lib/faraday/utils.rb', line 30

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

.normalize_path(url) ⇒ Object

Receives a String or URI and returns just the path with the query string sorted.



94
95
96
97
98
# File 'lib/faraday/utils.rb', line 94

def normalize_path(url)
  url = URI(url)
  (url.path.start_with?('/') ? url.path : "/#{url.path}") +
    (url.query ? "?#{sort_query_params(url.query)}" : '')
end

.parse_nested_query(query) ⇒ Object



47
48
49
# File 'lib/faraday/utils.rb', line 47

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

.parse_query(query) ⇒ Object

Adapted from Rack



43
44
45
# File 'lib/faraday/utils.rb', line 43

def parse_query(query)
  FlatParamsEncoder.decode(query)
end

.sort_query_params(query) ⇒ Object



117
118
119
# File 'lib/faraday/utils.rb', line 117

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

.unescape(str) ⇒ Object



36
37
38
# File 'lib/faraday/utils.rb', line 36

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

.URI(url) ⇒ Object

Normalize URI() behavior across Ruby versions

url - A String or URI.

Returns a parsed URI.



70
71
72
73
74
75
76
77
78
# File 'lib/faraday/utils.rb', line 70

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