Class: Baidubce::Utils
- Inherits:
-
Object
- Object
- Baidubce::Utils
- Defined in:
- lib/baidubce/utils/utils.rb
Class Method Summary collapse
-
.append_uri(base_uri, *path_components) ⇒ Object
Append path_components to the end of base_uri in order.
- .generate_headers(headers) ⇒ Object
- .generate_response(headers, body, return_body) ⇒ Object
- .get_canonical_querystring(params, for_signature) ⇒ Object
- .get_md5_from_file(file_name, content_length, buf_size = 8192) ⇒ Object
-
.parse_url_host(config) ⇒ Object
parse protocol, host, port from endpoint in config.
- .url_encode_except_slash(path) ⇒ Object
Class Method Details
.append_uri(base_uri, *path_components) ⇒ Object
Append path_components to the end of base_uri in order.
44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/baidubce/utils/utils.rb', line 44 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) ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/baidubce/utils/utils.rb', line 101 def self.generate_headers(headers) = {} 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) [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'] = unless .empty? resp_headers end |
.generate_response(headers, body, return_body) ⇒ Object
92 93 94 95 96 97 98 99 |
# File 'lib/baidubce/utils/utils.rb', line 92 def self.generate_response(headers, body, return_body) return body if return_body return generate_headers(headers) if body.empty? ret = JSON.parse(body) return ret rescue JSON::ParserError return body end |
.get_canonical_querystring(params, for_signature) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/baidubce/utils/utils.rb', line 63 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) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/baidubce/utils/utils.rb', line 78 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 |
.parse_url_host(config) ⇒ Object
parse protocol, host, port from endpoint in config.
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/baidubce/utils/utils.rb', line 27 def self.parse_url_host(config) endpoint = config.endpoint 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) ⇒ Object
58 59 60 61 |
# File 'lib/baidubce/utils/utils.rb', line 58 def self.url_encode_except_slash(path) encoded_path = ERB::Util.url_encode(path) encoded_path.gsub('%2F', '/') end |