module Baidu::Support::Util
Public Class Methods
blank?(obj)
click to toggle source
Check whether obj
is blank or not @example
blank?(' ') #true blank?('i ') #false blank?(nil) #true blank?(true) #false blank?(false) #true blank?({}) #true
@param [Object] obj
# File lib/baidu/support/util.rb, line 17 def blank?(obj) case obj when String then /[^[:space:]]/ !~ obj when NilClass then true when TrueClass then false when FalseClass then true else obj.respond_to?(:empty?) ? obj.empty? : !obj end end
clean_params(params)
click to toggle source
Delete object which value is nil
from params @example
clean_params({first: 'Lonre', middle: nil, last: 'Wang'}) => {first: 'Lonre', last: 'Wang'}
@param [Hash] params @return [Hash]
# File lib/baidu/support/util.rb, line 44 def clean_params(params) raise ArgumentError, 'require Hash instance' unless params.is_a? Hash unless blank? params params.delete_if { |k, v| v.nil? } end end
edit_path(path)
click to toggle source
Change +“\ ? | ” > < : “*+ to +”_“+, multi ”/“ to one ”/“ @example
edit_path(" .hello///path<f:aa/go.zip. ") => "hello/path_f_aa/go.zip"
@return [String]
# File lib/baidu/support/util.rb, line 56 def edit_path(path) return nil if path.nil? path = path.gsub(/\/+/, '/') path = path.gsub(/\A[\s\.]*/, '') path = path.gsub(/[\s\.]*\z/, '') path = path.gsub(/[\\\|\?"><:\*]/, '_') path end
encode_params(params)
click to toggle source
Encode params to www form style @example
encode_params({first: 'Lonre', last: 'Wang'}) => 'first=Lonre&last=Wang'
@param [Hash] params @return [String]
# File lib/baidu/support/util.rb, line 33 def encode_params(params) raise ArgumentError, 'require Hash instance' unless params.is_a? Hash URI::encode_www_form params end