Module: Qiniu::Utils
- Extended by:
- Utils
- Included in:
- AccessToken, DownloadToken, Fop::Image, QboxToken, Storage, UploadToken, Utils
- Defined in:
- lib/qiniu/utils.rb
Instance Method Summary collapse
- #crc32checksum(filepath) ⇒ Object
- #debug(msg) ⇒ Object
- #encode_entry_uri(bucket, key) ⇒ Object
-
#http_request(url, data = nil, options = {}) ⇒ Object
已过时,仅作为兼容接口保留.
- #safe_json_parse(data) ⇒ Object
-
#send_request_with(url, data = nil, options = {}) ⇒ Object
已过时,仅作为兼容接口保留.
- #urlsafe_base64_decode(encoded_content) ⇒ Object
- #urlsafe_base64_encode(content) ⇒ Object
Instance Method Details
#crc32checksum(filepath) ⇒ Object
104 105 106 |
# File 'lib/qiniu/utils.rb', line 104 def crc32checksum(filepath) File.open(filepath, "rb") { |f| Zlib.crc32 f.read } end |
#debug(msg) ⇒ Object
34 35 36 37 38 |
# File 'lib/qiniu/utils.rb', line 34 def debug(msg) if Config.settings[:enable_debug] Log.logger.debug(msg) end end |
#encode_entry_uri(bucket, key) ⇒ Object
23 24 25 26 |
# File 'lib/qiniu/utils.rb', line 23 def encode_entry_uri(bucket, key) entry_uri = bucket + ':' + key urlsafe_base64_encode(entry_uri) end |
#http_request(url, data = nil, options = {}) ⇒ Object
已过时,仅作为兼容接口保留
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/qiniu/utils.rb', line 77 def http_request url, data = nil, = {} retry_times = 0 begin retry_times += 1 send_request_with url, data, rescue Errno::ECONNRESET => err if Config.settings[:auto_reconnect] && retry_times < Config.settings[:max_retry_times] retry else Log.logger.error err end rescue => e Log.logger.warn "#{e.} => Utils.http_request('#{url}')" code = 0 data = {} body = {} if e.respond_to? :response res = e.response code = res.code.to_i if res.respond_to? :code body = res.respond_to?(:body) ? res.body : "" raw_headers = res.respond_to?(:raw_headers) ? res.raw_headers : {} data = safe_json_parse(body) unless body.empty? end [code, data, raw_headers] end end |
#safe_json_parse(data) ⇒ Object
28 29 30 31 32 |
# File 'lib/qiniu/utils.rb', line 28 def safe_json_parse(data) JSON.parse(data) rescue JSON::ParserError {} end |
#send_request_with(url, data = nil, options = {}) ⇒ Object
已过时,仅作为兼容接口保留
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/qiniu/utils.rb', line 41 def send_request_with url, data = nil, = {} [:method] = Config.settings[:method] unless [:method] [:content_type] = Config.settings[:content_type] unless [:content_type] = { :accept => :json, :user_agent => Config.settings[:user_agent] } auth_token = nil if ![:qbox_signature_token].nil? && ![:qbox_signature_token].empty? auth_token = 'QBox ' + [:qbox_signature_token] elsif ![:upload_signature_token].nil? && ![:upload_signature_token].empty? auth_token = 'UpToken ' + [:upload_signature_token] elsif [:access_token] auth_token = 'Bearer ' + [:access_token] end .merge!('Authorization' => auth_token) unless auth_token.nil? case [:method] when :get response = RestClient.get(url, ) when :post .merge!(:content_type => [:content_type]) response = RestClient.post(url, data, ) end code = response.respond_to?(:code) ? response.code.to_i : 0 unless HTTP.is_response_ok?(code) raise RequestFailed.new("Request Failed", response) else data = {} body = response.respond_to?(:body) ? response.body : {} raw_headers = response.respond_to?(:raw_headers) ? response.raw_headers : {} data = safe_json_parse(body) unless body.empty? end [code, data, raw_headers] end |
#urlsafe_base64_decode(encoded_content) ⇒ Object
19 20 21 |
# File 'lib/qiniu/utils.rb', line 19 def urlsafe_base64_decode encoded_content Base64.decode64 encoded_content.gsub('_','/').gsub('-', '+') end |
#urlsafe_base64_encode(content) ⇒ Object
15 16 17 |
# File 'lib/qiniu/utils.rb', line 15 def urlsafe_base64_encode content Base64.encode64(content).strip.gsub('+', '-').gsub('/','_').gsub(/\r?\n/, '') end |