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


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

def default_params_encoder=(value)
  @default_params_encoder = value
end

Instance Method Details

#build_nested_query(params) ⇒ Object


167
168
169
# File 'lib/faraday/utils.rb', line 167

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

#build_query(params) ⇒ Object


163
164
165
# File 'lib/faraday/utils.rb', line 163

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

#deep_merge(source, hash) ⇒ Object

Recursive hash merge


287
288
289
# File 'lib/faraday/utils.rb', line 287

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

#deep_merge!(target, hash) ⇒ Object

Recursive hash update


275
276
277
278
279
280
281
282
283
284
# File 'lib/faraday/utils.rb', line 275

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


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

def default_params_encoder
  @default_params_encoder ||= NestedParamsEncoder
end

#default_uri_parserObject


252
253
254
255
256
257
# File 'lib/faraday/utils.rb', line 252

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

#default_uri_parser=(parser) ⇒ Object


259
260
261
262
263
264
265
# File 'lib/faraday/utils.rb', line 259

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


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

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


201
202
203
204
205
206
207
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
# File 'lib/faraday/utils.rb', line 201

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.


268
269
270
271
272
# File 'lib/faraday/utils.rb', line 268

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


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

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

#parse_query(query) ⇒ Object

Adapted from Rack


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

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

#unescape(s) ⇒ Object


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

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.


242
243
244
245
246
247
248
249
250
# File 'lib/faraday/utils.rb', line 242

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