Module: Faraday::Utils
Defined Under Namespace
Classes: Headers, ParamsHash
Constant Summary collapse
- DEFAULT_SEP =
/[&;] */n
Instance Method Summary collapse
-
#build_nested_query(value, prefix = nil) ⇒ Object
Rack’s version modified to handle non-String values.
-
#build_query(params) ⇒ Object
Copied from Rack.
-
#deep_merge(source, hash) ⇒ Object
Recursive hash merge.
-
#deep_merge!(target, hash) ⇒ Object
Recursive hash update.
- #escape(s) ⇒ Object
-
#normalize_params(params, name, v = nil) ⇒ Object
Stolen from Rack.
-
#normalize_path(url) ⇒ Object
Receives a URL and returns just the path with the query string sorted.
- #parse_nested_query(qs) ⇒ Object
-
#parse_query(qs) ⇒ Object
Adapted from Rack.
- #unescape(s) ⇒ Object
Instance Method Details
#build_nested_query(value, prefix = nil) ⇒ Object
Rack’s version modified to handle non-String values
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/faraday/utils.rb', line 157 def build_nested_query(value, prefix = nil) case value when Array value.map { |v| build_nested_query(v, "#{prefix}%5B%5D") }.join("&") when Hash value.map { |k, v| build_nested_query(v, prefix ? "#{prefix}%5B#{escape(k)}%5D" : escape(k)) }.join("&") when NilClass prefix else raise ArgumentError, "value must be a Hash" if prefix.nil? "#{prefix}=#{escape(value)}" end end |
#build_query(params) ⇒ Object
Copied from Rack
146 147 148 149 150 151 152 153 154 |
# File 'lib/faraday/utils.rb', line 146 def build_query(params) params.map { |k, v| if v.class == Array build_query(v.map { |x| [k, x] }) else v.nil? ? escape(k) : "#{escape(k)}=#{escape(v)}" end }.join("&") end |
#deep_merge(source, hash) ⇒ Object
Recursive hash merge
258 259 260 |
# File 'lib/faraday/utils.rb', line 258 def deep_merge(source, hash) deep_merge!(source.dup, hash) end |
#deep_merge!(target, hash) ⇒ Object
Recursive hash update
246 247 248 249 250 251 252 253 254 255 |
# File 'lib/faraday/utils.rb', line 246 def deep_merge!(target, hash) hash.each do |key, value| if Hash === value and Hash === target[key] target[key] = deep_merge(target[key], value) else target[key] = value end end target end |
#escape(s) ⇒ Object
173 |
# File 'lib/faraday/utils.rb', line 173 def escape(s) CGI.escape s.to_s end |
#normalize_params(params, name, v = nil) ⇒ Object
Stolen from Rack
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/faraday/utils.rb', line 208 def normalize_params(params, name, v = nil) name =~ %r(\A[\[\]]*([^\[\]]+)\]*) k = $1 || '' after = $' || '' return if k.empty? if after == "" params[k] = v elsif after == "[]" params[k] ||= [] raise TypeError, "expected Array (got #{params[k].class.name}) for param `#{k}'" unless params[k].is_a?(Array) params[k] << v elsif after =~ %r(^\[\]\[([^\[\]]+)\]$) || after =~ %r(^\[\](.+)$) child_key = $1 params[k] ||= [] raise TypeError, "expected Array (got #{params[k].class.name}) for param `#{k}'" unless params[k].is_a?(Array) if params[k].last.is_a?(Hash) && !params[k].last.key?(child_key) normalize_params(params[k].last, child_key, v) else params[k] << normalize_params({}, child_key, v) end else params[k] ||= {} raise TypeError, "expected Hash (got #{params[k].class.name}) for param `#{k}'" unless params[k].is_a?(Hash) params[k] = normalize_params(params[k], after, v) end return params end |
#normalize_path(url) ⇒ Object
Receives a URL and returns just the path with the query string sorted.
240 241 242 243 |
# File 'lib/faraday/utils.rb', line 240 def normalize_path(url) (url.path != "" ? url.path : "/") + (url.query ? "?#{sort_query_params(url.query)}" : "") end |
#parse_nested_query(qs) ⇒ Object
197 198 199 200 201 202 203 204 205 |
# File 'lib/faraday/utils.rb', line 197 def parse_nested_query(qs) params = {} (qs || '').split(DEFAULT_SEP).each do |p| k, v = p.split('=', 2).map { |s| unescape(s) } normalize_params(params, k, v) end params end |
#parse_query(qs) ⇒ Object
Adapted from Rack
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/faraday/utils.rb', line 180 def parse_query(qs) params = {} (qs || '').split(DEFAULT_SEP).each do |p| k, v = p.split('=', 2).map { |x| unescape(x) } if cur = params[k] if cur.class == Array then params[k] << v else params[k] = [cur, v] end else params[k] = v end end params end |
#unescape(s) ⇒ Object
175 |
# File 'lib/faraday/utils.rb', line 175 def unescape(s) CGI.unescape s.to_s end |