class Baidubce::Utils

Constants

DEFAULT_CNAME_LIKE_LIST

Public Class Methods

append_uri(base_uri, *path_components) click to toggle source

Append path_components to the end of base_uri in order.

# File lib/baidubce/utils/utils.rb, line 49
def self.append_uri(base_uri, *path_components)
    uri = [base_uri]
    path_components.reject(&:empty?)
    path_components.each { |path| uri << path }

    unless uri.empty?
        uri[0].gsub!(/([\/]*$)/, '')
        uri[-1].gsub!(/(^[\/]*)/, '')
        uri.each { |u| u.gsub!(/(^[\/]*)|([\/]*$)/, '') }
    end

    uri.join("/")
end
generate_headers(headers) click to toggle source
# File lib/baidubce/utils/utils.rb, line 106
def self.generate_headers(headers)
    user_metadata = {}

    resp_headers = headers.inject({}) do |ret, (k, v)|
        key = k.to_s.tr('_', '-')
        if key.start_with?(Http::BCE_USER_METADATA_PREFIX)
            key.slice!(Http::BCE_USER_METADATA_PREFIX)
            user_metadata[key] = v
        elsif key.downcase == Http::ETAG.downcase
            ret[key] = v.delete('\"')
        elsif key.downcase == Http::CONTENT_LENGTH.downcase
            ret[key] = v.to_i
        else
            ret[key] = v
        end
        ret
    end
    resp_headers['user-metadata'] = user_metadata unless user_metadata.empty?
    resp_headers
end
generate_response(headers, body, return_body) click to toggle source
# File lib/baidubce/utils/utils.rb, line 97
def self.generate_response(headers, body, return_body)
    return body if return_body
    return generate_headers(headers) if body.to_s.empty?
    ret = JSON.parse(body)
    return ret
    rescue JSON::ParserError
    return body
end
get_canonical_querystring(params, for_signature) click to toggle source
# File lib/baidubce/utils/utils.rb, line 68
def self.get_canonical_querystring(params, for_signature)
    return '' if params.nil? || params.empty?

    arr = []
    params.each do |key, value|
        if !for_signature || key.downcase != Http::AUTHORIZATION.downcase
            value = '' if value.nil?
            str = ERB::Util.url_encode(key) + "=" + ERB::Util.url_encode(value)
            arr << str
        end
    end
    arr.sort!
    arr.join("&")
end
get_md5_from_file(file_name, content_length, buf_size=8192) click to toggle source
# File lib/baidubce/utils/utils.rb, line 83
def self.get_md5_from_file(file_name, content_length, buf_size=8192)

    md5 = Digest::MD5.new
    left_size = content_length
    File.open(file_name, 'rb') do |io|
        bytes_to_read = left_size > buf_size ? buf_size : left_size
        until left_size <= 0
            md5.update(io.read(bytes_to_read))
            left_size -= bytes_to_read
        end
    end
    md5.base64digest
end
is_cname_like_host(host) click to toggle source
# File lib/baidubce/utils/utils.rb, line 127
def self.is_cname_like_host(host)
    DEFAULT_CNAME_LIKE_LIST.each do |suffix|
        return true if host.to_s.downcase.end_with?(suffix)
    end
    return false
end
parse_url_host(config, use_backup_endpoint=false) click to toggle source

parse protocol, host, port from endpoint in config.

# File lib/baidubce/utils/utils.rb, line 29
def self.parse_url_host(config, use_backup_endpoint=false)
    endpoint = config.endpoint
    if use_backup_endpoint
        endpoint = config.backup_endpoint
    end
    unless endpoint.include?"://"
        protocol = config.protocol.downcase
        raise "Invalid protocol #{protocol}." if protocol != "http" && protocol != 'https'
        endpoint = sprintf("%s://%s", protocol, endpoint)
    end
    parsed_endpoint = URI.parse(endpoint)
    scheme = parsed_endpoint.scheme.downcase
    raise "Invalid endpoint #{endpoint}, unsupported scheme #{scheme}." if scheme != "http" && protocol != 'https'
    host = parsed_endpoint.host
    port = parsed_endpoint.port
    host += ":#{port}" unless scheme == 'http' && port == 80 || scheme == 'https' && port == 443
    return "#{scheme}://#{host}", host
end
url_encode_except_slash(path) click to toggle source
# File lib/baidubce/utils/utils.rb, line 63
def self.url_encode_except_slash(path)
     encoded_path = ERB::Util.url_encode(path)
     encoded_path.gsub('%2F', '/')
end