Module: Baidu::Support::Util

Defined in:
lib/baidu/support/util.rb

Class Method Summary collapse

Class Method Details

.blank?(obj) ⇒ Boolean

Check whether obj is blank or not

Examples:

blank?('  ')  #true
blank?('i ')  #false
blank?(nil)   #true
blank?(true)  #false
blank?(false) #true
blank?({})    #true

Parameters:

  • obj (Object)

Returns:

  • (Boolean)


17
18
19
20
21
22
23
24
25
# 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) ⇒ Hash

Delete object which value is nil from params

Examples:

clean_params({first: 'Lonre', middle: nil, last: 'Wang'})
=> {first: 'Lonre', last: 'Wang'}

Parameters:

  • params (Hash)

Returns:

  • (Hash)

Raises:

  • (ArgumentError)


44
45
46
47
48
49
# 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) ⇒ String

Change “\ ? | ” > < : “* to ”_“, multi ”/“ to one ”/“

Examples:

edit_path(" .hello///path<f:aa/go.zip. ")
=> "hello/path_f_aa/go.zip"

Returns:

  • (String)


56
57
58
59
60
61
62
63
# 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) ⇒ String

Encode params to www form style

Examples:

encode_params({first: 'Lonre', last: 'Wang'})
=> 'first=Lonre&last=Wang'

Parameters:

  • params (Hash)

Returns:

  • (String)

Raises:

  • (ArgumentError)


33
34
35
36
# 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