class Oss::Cors

Attributes

client[RW]
xml_obj[RW]

Public Class Methods

new(client) click to toggle source
# File lib/oss/cors.rb, line 12
def initialize(client)
  @client = client
end

Public Instance Methods

delete_bucket_cors(bucket_name) click to toggle source
# File lib/oss/cors.rb, line 61
def delete_bucket_cors(bucket_name)
  client.delete(
      host:         "#{bucket_name}.#{client.endpoint}",
      path:         '/?cors',
      sign_configs: {resource: "/#{bucket_name}"},
  )

  true
end
get_bucket_cors(bucket_name) click to toggle source
# File lib/oss/cors.rb, line 46
def get_bucket_cors(bucket_name)
  @xml_obj = client.get(
      host:         "#{bucket_name}.#{client.endpoint}",
      sign_configs: {resource: "/#{bucket_name}"},
      path:         '/?cors',
  )

  rules = Array.new
  @xml_obj.xpath('CORSConfiguration/CORSRule').each do |rule|
    rules << rule
  end

  rules
end
method_missing(method) click to toggle source
Calls superclass method
# File lib/oss/cors.rb, line 93
def method_missing(method)
  if @xml_obj.nil?
    super
  else
    camel = Util.camelize(method)
    value = @xml_obj.xpath(camel)
    raise "missing xml attribute #{camel}" if value.length == 0
    value.inner_text
  end
end
option_object(bucket_name, object_name, origin, request_method, request_headers) click to toggle source
# File lib/oss/cors.rb, line 71
def option_object(bucket_name, object_name, origin, request_method, request_headers)
  # sign configs
  sign_configs = Hash.new
  sign_configs[:resource] = "/#{bucket_name}"

  headers = Hash.new
  headers['Origin'] = origin
  headers['Access-Control-Request-Method']  = request_method
  headers['Access-Control-Request-Headers'] = request_headers

  resp = client.options(
      host:         "#{bucket_name}.#{client.endpoint}",
      path:         "/#{object_name}",
      sign_configs: sign_configs,
      headers:      headers,
      as:           :raw
  )

  # return response header
  resp.headers
end
put_bucket_cors(bucket_name, rules) click to toggle source
# File lib/oss/cors.rb, line 16
def put_bucket_cors(bucket_name, rules)
  # sign configs
  sign_configs = Hash.new
  sign_configs[:resource]             = "/#{bucket_name}"
  sign_configs[:content_type]         = 'application/x-www-form-urlencoded'
  sign_configs[:content_length_check] = true

  # build payload xml
  payload = Nokogiri::XML::Builder.new do |xml|
    xml.CORSConfiguration do
      rules.each do |rule|
        xml.CORSRule do
          rule.each do |a_rule|
            xml.send(a_rule[:key], a_rule[:value])
          end
        end
      end
    end
  end

  @xml_obj = client.put(
      host: "#{bucket_name}.#{client.endpoint}",
      path: '/?cors',
      sign_configs: sign_configs,
      payload: payload.to_xml
  )

  true
end