Module: Faraday::Utils

Extended by:
Utils, Rack::Utils
Includes:
Rack::Utils
Included in:
Connection, Utils
Defined in:
lib/faraday/utils.rb

Constant Summary collapse

HEADERS =
Hash.new do |h, k|
  if k.respond_to?(:to_str)
    k
  else
    k.to_s.split('_').            # :user_agent => %w(user agent)
      each { |w| w.capitalize! }. # => %w(User Agent)
      join('-')                   # => "User-Agent"
  end
end

Instance Method Summary collapse

Instance Method Details

#escape(s) ⇒ Object

Be sure to URI escape ‘+’ symbols to %2B. Otherwise, they get interpreted as spaces.



27
28
29
30
31
# File 'lib/faraday/utils.rb', line 27

def escape(s)
  s.to_s.gsub(/([^a-zA-Z0-9_.-]+)/n) do
    '%' << $1.unpack('H2'*bytesize($1)).join('%').tap { |c| c.upcase! }
  end
end

#merge_headers(existing_headers, new_headers) ⇒ Object

Turns headers keys and values into strings. Look up symbol keys in the the HEADERS hash.

h = merge_headers(HeaderHash.new, :content_type => 'text/plain')
h['Content-Type'] # = 'text/plain'


46
47
48
49
50
# File 'lib/faraday/utils.rb', line 46

def merge_headers(existing_headers, new_headers)
  new_headers.each do |key, value|
    existing_headers[HEADERS[key]] = value.to_s
  end
end

#merge_params(existing_params, new_params) ⇒ Object

Turns param keys into strings



34
35
36
37
38
# File 'lib/faraday/utils.rb', line 34

def merge_params(existing_params, new_params)
  new_params.each do |key, value|
    existing_params[key.to_s] = value
  end
end

#normalize_path(url) ⇒ Object

Receives a URL and returns just the path with the query string sorted.



53
54
55
56
# File 'lib/faraday/utils.rb', line 53

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