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)


27
28
29
30
31
32
33
# File 'lib/aliyun/oss/utils.rb', line 27

def self.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


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

def self.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

.get_endpoint(bucket, host) ⇒ String

Get endpoint

Examples:


get_endpoint('bucket-name', 'oss-cn-hangzhou.aliyuncs.com')
# => 'http://bucket-name.oss-cn-hangzhou.aliyuncs.com'

Parameters:

  • bucket (String)

    the Bucket name

  • host (String)

    the host of Bucket

Returns:

  • (String)


20
21
22
# File 'lib/aliyun/oss/utils.rb', line 20

def self.get_endpoint(bucket, host)
  "http://#{bucket}.#{host}/"
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)


47
48
49
50
51
# File 'lib/aliyun/oss/utils.rb', line 47

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)


38
39
40
# File 'lib/aliyun/oss/utils.rb', line 38

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

.stringify_keys!(hash) ⇒ Object



104
105
106
107
108
# File 'lib/aliyun/oss/utils.rb', line 104

def self.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)


56
57
58
# File 'lib/aliyun/oss/utils.rb', line 56

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

.to_xml(hash) ⇒ Object

nodoc



60
61
62
# File 'lib/aliyun/oss/utils.rb', line 60

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

.underscore(str) ⇒ Object

See Also:



83
84
85
86
87
88
89
90
91
# File 'lib/aliyun/oss/utils.rb', line 83

def self.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



94
95
96
97
98
99
100
101
102
# File 'lib/aliyun/oss/utils.rb', line 94

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