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 =
/[^\w .~-]+/
DEFAULT_SEP =
/[&;] */n

Instance Method Summary collapse

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



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

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

#deep_merge!(target, hash) ⇒ Object

Recursive hash update



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

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



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

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

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

Stolen from Rack



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
# File 'lib/faraday/utils.rb', line 214

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.



246
247
248
249
# File 'lib/faraday/utils.rb', line 246

def normalize_path(url)
  (url.path != "" ? url.path : "/") +
  (url.query ? "?#{sort_query_params(url.query)}" : "")
end

#parse_nested_query(qs) ⇒ Object



203
204
205
206
207
208
209
210
211
# File 'lib/faraday/utils.rb', line 203

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



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/faraday/utils.rb', line 186

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



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

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