Class: Aliyun::Odps::Utils

Inherits:
Object
  • Object
show all
Defined in:
lib/aliyun/odps/utils.rb

Class Method Summary collapse

Class Method Details

.content_size(content) ⇒ Integer

Calculate content length

Returns:

  • (Integer)


13
14
15
16
17
18
19
# File 'lib/aliyun/odps/utils.rb', line 13

def content_size(content)
  if content.respond_to?(:size)
    content.size
  elsif content.is_a?(IO)
    content.stat.size
  end
end

.dig_value(hash, *keys) ⇒ Object

Dig values in deep hash

Examples:

dig_value({ 'a' => { 'b' => { 'c' => 3 } } }, 'a', 'b', 'c')  # => 3


55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/aliyun/odps/utils.rb', line 55

def dig_value(hash, *keys)
  new_hash = hash.dup

  keys.each do |key|
    if new_hash.is_a?(Hash) && new_hash.key?(key)
      new_hash = new_hash[key]
    else
      return nil
    end
  end
  new_hash
end

.generate_uuid(flag) ⇒ Object



96
97
98
# File 'lib/aliyun/odps/utils.rb', line 96

def generate_uuid(flag)
  "#{flag}#{Time.now.strftime('%Y%m%d%H%M%S')}#{SecureRandom.hex(3)}"
end

.hash_slice(hash, *selected_keys) ⇒ Hash

Examples:

# { 'a' => 1, 'c' => 3 }
Utils.hash_slice({ 'a' => 1, 'b' => 2, 'c' => 3 }, 'a', 'c')

Returns:

  • (Hash)


33
34
35
36
37
# File 'lib/aliyun/odps/utils.rb', line 33

def hash_slice(hash, *selected_keys)
  new_hash = {}
  selected_keys.each { |k| new_hash[k] = hash[k] if hash.key?(k) }
  new_hash
end

.md5_hexdigest(body) ⇒ String

HexDigest body with MD5

Returns:

  • (String)


24
25
26
# File 'lib/aliyun/odps/utils.rb', line 24

def md5_hexdigest(body)
  Digest::MD5.hexdigest(body).strip
end

.stringify_keys!(hash) ⇒ Object



90
91
92
93
94
# File 'lib/aliyun/odps/utils.rb', line 90

def stringify_keys!(hash)
  hash.keys.each do |key|
    hash[key.to_s] = hash.delete(key)
  end
end

.to_data(file_or_bin) ⇒ Bin data

Convert File or Bin data to bin data

Returns:

  • (Bin data)


42
43
44
# File 'lib/aliyun/odps/utils.rb', line 42

def to_data(file_or_bin)
  file_or_bin.respond_to?(:read) ? IO.binread(file_or_bin) : file_or_bin
end

.to_xml(hash, options = {}) ⇒ Object

nodoc



46
47
48
# File 'lib/aliyun/odps/utils.rb', line 46

def to_xml(hash, options = {}) # nodoc
  %(<?xml version="1.0" encoding="UTF-8"?>#{Gyoku.xml(hash, options)})
end

.underscore(str) ⇒ Object

See Also:



69
70
71
72
73
74
75
76
77
# File 'lib/aliyun/odps/utils.rb', line 69

def underscore(str)
  word = str.to_s.dup
  word.gsub!(/::/, '/')
  word.gsub!(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2')
  word.gsub!(/([a-z\d])([A-Z])/, '\1_\2')
  word.tr!('-', '_')
  word.downcase!
  word
end

.wrap(object) ⇒ Object



80
81
82
83
84
85
86
87
88
# File 'lib/aliyun/odps/utils.rb', line 80

def wrap(object)
  if object.nil?
    []
  elsif object.respond_to?(:to_ary)
    object.to_ary || [object]
  else
    [object]
  end
end