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


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

def default_params_encoder
  @default_params_encoder ||= NestedParamsEncoder
end

.default_space_encodingObject


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

def default_space_encoding
  @default_space_encoding ||= '+'
end

Class Method Details

.build_nested_query(params) ⇒ Object


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

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

.build_query(params) ⇒ Object


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

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

.deep_merge(source, hash) ⇒ Object

Recursive hash merge


109
110
111
# File 'lib/faraday/utils.rb', line 109

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

.deep_merge!(target, hash) ⇒ Object

Recursive hash update


97
98
99
100
101
102
103
104
105
106
# File 'lib/faraday/utils.rb', line 97

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

.default_uri_parserObject


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

def default_uri_parser
  @default_uri_parser ||= begin
    require 'uri'
    Kernel.method(:URI)
  end
end

.default_uri_parser=(parser) ⇒ Object


80
81
82
83
84
85
86
# File 'lib/faraday/utils.rb', line 80

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


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

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.


90
91
92
93
94
# File 'lib/faraday/utils.rb', line 90

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


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

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

.parse_query(query) ⇒ Object

Adapted from Rack


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

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

.sort_query_params(query) ⇒ Object


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

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

.unescape(str) ⇒ Object


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

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.


63
64
65
66
67
68
69
70
71
# File 'lib/faraday/utils.rb', line 63

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