Class: Oss::Cors

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/oss/cors.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util

#camelize, #hash_filter, #oss_headers_to_s, #set_query_string

Constructor Details

#initialize(client) ⇒ Cors

Returns a new instance of Cors.



12
13
14
# File 'lib/oss/cors.rb', line 12

def initialize(client)
  @client = client
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method) ⇒ Object



93
94
95
96
97
98
99
100
101
102
# 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

Instance Attribute Details

#clientObject

Returns the value of attribute client.



10
11
12
# File 'lib/oss/cors.rb', line 10

def client
  @client
end

#xml_objObject

Returns the value of attribute xml_obj.



10
11
12
# File 'lib/oss/cors.rb', line 10

def xml_obj
  @xml_obj
end

Instance Method Details

#delete_bucket_cors(bucket_name) ⇒ Object



61
62
63
64
65
66
67
68
69
# 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) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# 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

#option_object(bucket_name, object_name, origin, request_method, request_headers) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# 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) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# 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