Class: QcloudCos::Utils

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

Class Method Summary collapse

Class Method Details

.content_size(content) ⇒ Object

计算 content 的大小



10
11
12
13
14
15
16
# File 'lib/qcloud_cos/utils.rb', line 10

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

.generate_file_sha(file_path) ⇒ Object

生成文件的 sha1 值



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

def generate_file_sha(file_path)
  Digest::SHA1.file(file_path).hexdigest
end

.generate_sha(content) ⇒ Object

生成 content 的 sha



19
20
21
# File 'lib/qcloud_cos/utils.rb', line 19

def generate_sha(content)
  Digest::SHA1.hexdigest content
end

.hash_slice(hash, *selected_keys) ⇒ Object

获取 Hash 中的一部分键值对

Examples:


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


40
41
42
43
44
# File 'lib/qcloud_cos/utils.rb', line 40

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

.stringify_keys!(hash) ⇒ Object

将 hash 的 key 统一转化为 string



29
30
31
32
33
# File 'lib/qcloud_cos/utils.rb', line 29

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

.url_encode(path) ⇒ Object

对 path 进行 url_encode



5
6
7
# File 'lib/qcloud_cos/utils.rb', line 5

def url_encode(path)
  ERB::Util.url_encode(path).gsub('%2F', '/')
end