Class: Aliyun::Oss::Utils

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

Class Method Summary collapse

Class Method Details

.content_size(content) ⇒ Integer

Calculate content length

Returns:

  • (Integer)


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

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

.hash_slice(hash, *selected_keys) ⇒ Hash

Examples:

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

Returns:

  • (Hash)


31
32
33
34
35
# File 'lib/aliyun/oss/utils.rb', line 31

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

.md5_digest(body) ⇒ String

Digest body with MD5 and then encoding with Base64

Returns:

  • (String)


23
24
25
# File 'lib/aliyun/oss/utils.rb', line 23

def self.md5_digest(body)
  Base64.encode64(Digest::MD5.digest(body)).strip
end

.to_data(file_or_bin) ⇒ Bin data

Convert File or Bin data to bin data

Returns:

  • (Bin data)


40
41
42
# File 'lib/aliyun/oss/utils.rb', line 40

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

.to_xml(hash) ⇒ Object

nodoc



44
45
46
# File 'lib/aliyun/oss/utils.rb', line 44

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