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.


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

def default_params_encoder=(value)
  @default_params_encoder = value
end

Instance Method Details

#build_nested_query(params) ⇒ Object


179
180
181
# File 'lib/faraday/utils.rb', line 179

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

#build_query(params) ⇒ Object


175
176
177
# File 'lib/faraday/utils.rb', line 175

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

#deep_merge(source, hash) ⇒ Object

Recursive hash merge


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

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

#deep_merge!(target, hash) ⇒ Object

Recursive hash update


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

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


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

def default_params_encoder
  @default_params_encoder ||= NestedParamsEncoder
end

#default_uri_parserObject


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

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

#default_uri_parser=(parser) ⇒ Object


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

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


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

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


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
238
239
240
241
242
243
244
245
246
247
# File 'lib/faraday/utils.rb', line 213

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.


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

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


200
201
202
# File 'lib/faraday/utils.rb', line 200

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

#parse_query(query) ⇒ Object

Adapted from Rack


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

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

#unescape(s) ⇒ Object


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

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.


254
255
256
257
258
259
260
261
262
# File 'lib/faraday/utils.rb', line 254

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