Module: Stretcher::Util
- Defined in:
- lib/stretcher/util.rb
Class Method Summary collapse
- .clean_params(params = {}) ⇒ Object
-
.curl_format(req) ⇒ Object
cURL formats a Faraday req.
- .querify(hash) ⇒ Object
-
.qurl(url, query_opts = nil) ⇒ Object
Formats a url + query opts.
Class Method Details
.clean_params(params = {}) ⇒ Object
23 24 25 26 27 28 29 30 |
# File 'lib/stretcher/util.rb', line 23 def self.clean_params params={} return unless params clean_params = {} params.each do |key, value| clean_params[key] = value.is_a?(Array) ? value.join(',') : value end clean_params end |
.curl_format(req) ⇒ Object
cURL formats a Faraday req. Useful for logging
5 6 7 8 9 10 11 12 |
# File 'lib/stretcher/util.rb', line 5 def self.curl_format(req) body = "-d '#{req.body.is_a?(Hash) ? MultiJson.dump(req.body) : req.body}'" if req.body headers = req.headers.map {|name, value| "'-H #{name}: #{value}'" }.sort.join(' ') method = req.method.to_s.upcase url = Util.qurl(req.path,req.params) ["curl -X#{method}", "'" + url + "'", body, headers].compact.join(' ') end |
.querify(hash) ⇒ Object
19 20 21 |
# File 'lib/stretcher/util.rb', line 19 def self.querify(hash) hash.map {|k,v| "#{k}=#{v}"}.join('&') end |
.qurl(url, query_opts = nil) ⇒ Object
Formats a url + query opts
15 16 17 |
# File 'lib/stretcher/util.rb', line 15 def self.qurl(url, query_opts=nil) query_opts && !query_opts.empty? ? "#{url}?#{querify(query_opts)}" : url end |