Class: Aliyun::OSS::Callback

Inherits:
Common::Struct::Base show all
Includes:
Common::Logging
Defined in:
lib/aliyun/oss/struct.rb

Overview

Callback represents a HTTP call made by OSS to user's application server after an event happens, such as an object is successfully uploaded to OSS. See: https://help.aliyun.com/document_detail/oss/api-reference/object/Callback.html Attributes:

  • url [String] the URL WITHOUT the query string

  • query [Hash] the query to generate query string

  • body [String] the body of the request

  • content_type [String] the Content-Type of the request

  • host [String] the Host in HTTP header for this request

Constant Summary

Constants included from Common::Logging

Common::Logging::DEFAULT_LOG_FILE, Common::Logging::MAX_NUM_LOG, Common::Logging::ROTATE_SIZE

Instance Method Summary collapse

Methods included from Common::Logging

logger, #logger, set_log_file, set_log_level

Methods inherited from Common::Struct::Base

#initialize, #to_s

Methods included from Common::Struct::Base::AttrHelper

#attrs

Constructor Details

This class inherits a constructor from Aliyun::Common::Struct::Base

Instance Method Details

#default_content_typeObject (private)


201
202
203
# File 'lib/aliyun/oss/struct.rb', line 201

def default_content_type
  "application/x-www-form-urlencoded"
end

#normalize_url(url) ⇒ Object (private)


186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/aliyun/oss/struct.rb', line 186

def normalize_url(url)
  uri = URI.parse(url)
  uri = URI.parse("http://#{url}") unless uri.scheme

  if uri.scheme != 'http' and uri.scheme != 'https'
    fail ClientError, "Only HTTP and HTTPS endpoint are accepted."
  end

  unless uri.query.nil?
    fail ClientError, "Query parameters should not appear in URL."
  end

  uri.to_s
end

#serializeObject


169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/aliyun/oss/struct.rb', line 169

def serialize
  query_string = (query || {}).map { |k, v|
    [CGI.escape(k.to_s), CGI.escape(v.to_s)].join('=') }.join('&')

  cb = {
    'callbackUrl' => "#{normalize_url(url)}?#{query_string}",
    'callbackBody' => body,
    'callbackBodyType' => content_type || default_content_type
  }
  cb['callbackHost'] = host if host

  logger.debug("Callback json: #{cb}")

  Base64.strict_encode64(cb.to_json)
end