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

Parameters:

  • value

    the value to set the attribute default_params_encoder to.



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

def default_params_encoder=(value)
  @default_params_encoder = value
end

Instance Method Details

#build_nested_query(params) ⇒ Object



187
188
189
# File 'lib/faraday/utils.rb', line 187

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

#build_query(params) ⇒ Object



183
184
185
# File 'lib/faraday/utils.rb', line 183

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

#deep_merge(source, hash) ⇒ Object

Recursive hash merge



307
308
309
# File 'lib/faraday/utils.rb', line 307

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

#deep_merge!(target, hash) ⇒ Object

Recursive hash update



295
296
297
298
299
300
301
302
303
304
# File 'lib/faraday/utils.rb', line 295

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



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

def default_params_encoder
  @default_params_encoder ||= NestedParamsEncoder
end

#default_uri_parserObject



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

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

#default_uri_parser=(parser) ⇒ Object



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

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



193
194
195
196
197
# File 'lib/faraday/utils.rb', line 193

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



221
222
223
224
225
226
227
228
229
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
# File 'lib/faraday/utils.rb', line 221

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.



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

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



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

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

#parse_query(query) ⇒ Object

Adapted from Rack



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

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

#unescape(s) ⇒ Object



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

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.



262
263
264
265
266
267
268
269
270
# File 'lib/faraday/utils.rb', line 262

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