Module: Qiniu::S3::Utils

Extended by:
Utils
Included in:
Auth, IO, Image, RS, Utils
Defined in:
lib/qiniu/s3/utils.rb

Instance Method Summary collapse

Instance Method Details

#encode_entry_uri(bucket, key) ⇒ Object



21
22
23
24
# File 'lib/qiniu/s3/utils.rb', line 21

def encode_entry_uri(bucket, key)
  entry_uri = bucket + ':' + key
  urlsafe_base64_encode(entry_uri)
end

#generate_query_string(params) ⇒ Object



116
117
118
119
120
# File 'lib/qiniu/s3/utils.rb', line 116

def generate_query_string(params)
  return params if params.is_a?(String)
  total_param = params.map { |key, value| key.to_s+"="+value.to_s }
  URI.escape(total_param.join("&"))
end

#http_request(url, data = nil, options = {}) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/qiniu/s3/utils.rb', line 58

def http_request url, data = nil, options = {}
  retry_times = 0
  begin
    retry_times += 1
    send_request_with url, data, options
  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.message} => 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 : ""
      data = safe_json_parse(body) unless body.empty?
    end
    [code, data]
  end
end

#safe_json_parse(data) ⇒ Object



26
27
28
29
30
# File 'lib/qiniu/s3/utils.rb', line 26

def safe_json_parse(data)
  JSON.parse(data)
rescue JSON::ParserError
  {}
end

#send_request_with(url, data = nil, options = {}) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/qiniu/s3/utils.rb', line 32

def send_request_with url, data = nil, options = {}
  options[:method] = Config.settings[:method] unless options[:method]
  options[:content_type] = Config.settings[:content_type] unless options[:content_type]
  header_options = {
    :accept => :json,
    :user_agent => Config.settings[:user_agent]
  }
  header_options.merge!('Authorization' => "Bearer #{options[:access_token]}") if options[:access_token]
  case options[:method]
  when :get
    response = RestClient.get url, header_options
  when :post
    header_options.merge!(:content_type => options[:content_type])
    response = RestClient.post url, data, header_options
  end
  code = response.respond_to?(:code) ? response.code.to_i : 0
  if code != 200
    raise RequestFailed.new(response)
  else
    data = {}
    body = response.respond_to?(:body) ? response.body : {}
    data = safe_json_parse(body) unless body.empty?
  end
  [code, data]
end

#upload_multipart_data(url, filepath, action_string, callback_query_string = '') ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/qiniu/s3/utils.rb', line 84

def upload_multipart_data(url, filepath, action_string, callback_query_string = '')
  code, data = 0, {}
  begin
    header_options = {
      :accept => :json,
      :user_agent => Config.settings[:user_agent]
    }
    post_data = {
      :file => File.new(filepath, 'rb'),
      :params => callback_query_string,
      :action => action_string,
      :multipart => true
    }
    response = RestClient.post url, post_data, header_options
    body = response.respond_to?(:body) ? response.body : ""
    data = safe_json_parse(body) unless body.empty?
    code = response.code.to_i if response.respond_to?(:code)
  rescue Errno::ECONNRESET => err
    Log.logger.error err
  rescue => e
    Log.logger.warn "#{e.message} => Utils.http_request('#{url}')"
    res = e.response
    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 : ""
      data = safe_json_parse(body) unless body.empty?
    end
  end
  [code, data]
end

#urlsafe_base64_decode(encoded_content) ⇒ Object



17
18
19
# File 'lib/qiniu/s3/utils.rb', line 17

def urlsafe_base64_decode encoded_content
  Base64.decode64 encoded_content.gsub('_','/').gsub('-', '+')
end

#urlsafe_base64_encode(content) ⇒ Object



13
14
15
# File 'lib/qiniu/s3/utils.rb', line 13

def urlsafe_base64_encode content
  Base64.encode64(content).strip.gsub('+', '-').gsub('/','_').gsub(/\r?\n/, '')
end