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 .~_-]/.freeze
DEFAULT_SEP =
/[&;] */n.freeze

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.default_params_encoderObject


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

def default_params_encoder
  @default_params_encoder ||= NestedParamsEncoder
end

.default_space_encodingObject


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

def default_space_encoding
  @default_space_encoding ||= '+'
end

Class Method Details

.basic_header_from(login, pass) ⇒ Object


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

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

.build_nested_query(params) ⇒ Object


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

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

.build_query(params) ⇒ Object


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

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

.deep_merge(source, hash) ⇒ Object

Recursive hash merge


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

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

.deep_merge!(target, hash) ⇒ Object

Recursive hash update


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

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


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

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

.default_uri_parser=(parser) ⇒ Object


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

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


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

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.


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

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


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

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

.parse_query(query) ⇒ Object

Adapted from Rack


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

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

.sort_query_params(query) ⇒ Object


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

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

.unescape(str) ⇒ Object


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

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.


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

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