Module: Faraday::Utils
- Extended by:
- Utils, Rack::Utils
- Includes:
- Rack::Utils
- Included in:
- Utils
- Defined in:
- lib/faraday/utils.rb
Defined Under Namespace
Classes: Headers, ParamsHash
Instance Method Summary collapse
-
#build_nested_query(value, prefix = nil) ⇒ Object
Override Rack’s version since it doesn’t handle non-String values.
-
#deep_merge(source, hash) ⇒ Object
Recursive hash merge.
-
#deep_merge!(target, hash) ⇒ Object
Recursive hash update.
-
#escape(s) ⇒ Object
Be sure to URI escape ‘+’ symbols to %2B.
-
#normalize_path(url) ⇒ Object
Receives a URL and returns just the path with the query string sorted.
Instance Method Details
#build_nested_query(value, prefix = nil) ⇒ Object
Override Rack’s version since it doesn’t handle non-String values
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/faraday/utils.rb', line 109 def build_nested_query(value, prefix = nil) case value when Array value.map { |v| build_nested_query(v, "#{prefix}[]") }.join("&") when Hash value.map { |k, v| build_nested_query(v, prefix ? "#{prefix}[#{escape(k)}]" : escape(k)) }.join("&") when NilClass prefix else raise ArgumentError, "value must be a Hash" if prefix.nil? "#{prefix}=#{escape(value)}" end end |
#deep_merge(source, hash) ⇒ Object
Recursive hash merge
152 153 154 |
# File 'lib/faraday/utils.rb', line 152 def deep_merge(source, hash) deep_merge!(source.dup, hash) end |
#deep_merge!(target, hash) ⇒ Object
Recursive hash update
140 141 142 143 144 145 146 147 148 149 |
# File 'lib/faraday/utils.rb', line 140 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
Be sure to URI escape ‘+’ symbols to %2B. Otherwise, they get interpreted as spaces.
127 128 129 130 131 |
# File 'lib/faraday/utils.rb', line 127 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 |
#normalize_path(url) ⇒ Object
Receives a URL and returns just the path with the query string sorted.
134 135 136 137 |
# File 'lib/faraday/utils.rb', line 134 def normalize_path(url) (url.path != "" ? url.path : "/") + (url.query ? "?#{sort_query_params(url.query)}" : "") end |