Module: Faraday::Utils

Extended by:
Utils
Included in:
Utils
Defined in:
lib/faraday/utils.rb

Defined Under Namespace

Classes: Headers, ParamsHash

Constant Summary collapse

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

Class Attribute Summary collapse

Instance Method Summary collapse

Class Attribute Details

.default_params_encoder=(value) ⇒ Object (writeonly)

Sets the attribute default_params_encoder


226
227
228
# File 'lib/faraday/utils.rb', line 226

def default_params_encoder=(value)
  @default_params_encoder = value
end

Instance Method Details

#build_nested_query(params) ⇒ Object


196
197
198
# File 'lib/faraday/utils.rb', line 196

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

#build_query(params) ⇒ Object


192
193
194
# File 'lib/faraday/utils.rb', line 192

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

#deep_merge(source, hash) ⇒ Object

Recursive hash merge


316
317
318
# File 'lib/faraday/utils.rb', line 316

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

#deep_merge!(target, hash) ⇒ Object

Recursive hash update


304
305
306
307
308
309
310
311
312
313
# File 'lib/faraday/utils.rb', line 304

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

#default_params_encoderObject


221
222
223
# File 'lib/faraday/utils.rb', line 221

def default_params_encoder
  @default_params_encoder ||= NestedParamsEncoder
end

#default_uri_parserObject


281
282
283
284
285
286
# File 'lib/faraday/utils.rb', line 281

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

#default_uri_parser=(parser) ⇒ Object


288
289
290
291
292
293
294
# File 'lib/faraday/utils.rb', line 288

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

#escape(s) ⇒ Object


202
203
204
205
206
# File 'lib/faraday/utils.rb', line 202

def escape(s)
  s.to_s.gsub(ESCAPE_RE) {|match|
    '%' + match.unpack('H2' * match.bytesize).join('%').upcase
  }.tr(' ', '+')
end

#normalize_params(params, name, v = nil) ⇒ Object

Stolen from Rack


230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/faraday/utils.rb', line 230

def normalize_params(params, name, v = nil)
  name =~ %r(\A[\[\]]*([^\[\]]+)\]*)
  k = $1 || ''
  after = $' || ''

  return if k.empty?

  if after == ""
    if params[k]
      params[k] = Array[params[k]] unless params[k].kind_of?(Array)
      params[k] << v
    else
      params[k] = v
    end
  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 String or URI and returns just the path with the query string sorted.


297
298
299
300
301
# File 'lib/faraday/utils.rb', line 297

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


217
218
219
# File 'lib/faraday/utils.rb', line 217

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

#parse_query(query) ⇒ Object

Adapted from Rack


213
214
215
# File 'lib/faraday/utils.rb', line 213

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

#unescape(s) ⇒ Object


208
# File 'lib/faraday/utils.rb', line 208

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

#URI(url) ⇒ Object

Normalize URI() behavior across Ruby versions

url - A String or URI.

Returns a parsed URI.


271
272
273
274
275
276
277
278
279
# File 'lib/faraday/utils.rb', line 271

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