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_encoder ⇒ Object
51
52
53
|
# File 'lib/faraday/utils.rb', line 51
def default_params_encoder
@default_params_encoder ||= NestedParamsEncoder
end
|
.default_space_encoding ⇒ Object
20
21
22
|
# File 'lib/faraday/utils.rb', line 20
def default_space_encoding
@default_space_encoding ||= '+'
end
|
Class Method Details
55
56
57
58
59
|
# File 'lib/faraday/utils.rb', line 55
def (login, pass)
value = ["#{login}:#{pass}"].pack('m') value.delete!("\n")
"Basic #{value}"
end
|
.build_nested_query(params) ⇒ Object
.build_query(params) ⇒ Object
.deep_merge(source, hash) ⇒ Object
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
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_parser ⇒ Object
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
.parse_query(query) ⇒ Object
.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) 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
|