class Google::Cloud::Storage::Bucket::Cors

# Bucket Cors

A special-case Array for managing the website CORS rules for a bucket. Accessed via {Bucket#cors}.

@see cloud.google.com/storage/docs/cross-origin Cross-Origin

Resource Sharing (CORS)

@example

require "google/cloud/storage"

storage = Google::Cloud::Storage.new

bucket = storage.bucket "my-bucket"

bucket.cors do |c|
  # Remove the last CORS rule from the array
  c.pop
  # Remove all existing rules with the https protocol
  c.delete_if { |r| r.origin.include? "http://example.com" }
  c.add_rule ["http://example.org", "https://example.org"],
             ["GET", "POST", "DELETE"],
             headers: ["X-My-Custom-Header"],
             max_age: 3600
end

@example Retrieving the bucket's CORS rules.

require "google/cloud/storage"

storage = Google::Cloud::Storage.new

bucket = storage.bucket "my-bucket"
bucket.cors.size #=> 2
rule = bucket.cors.first
rule.origin #=> ["http://example.org"]
rule.methods #=> ["GET","POST","DELETE"]
rule.headers #=> ["X-My-Custom-Header"]
rule.max_age #=> 3600

Public Class Methods

from_gapi(gapi_list) click to toggle source

@private

# File lib/google/cloud/storage/bucket/cors.rb, line 123
def self.from_gapi gapi_list
  rules = Array(gapi_list).map { |gapi| Rule.from_gapi gapi }
  new rules
end
new(rules = []) click to toggle source

@private Initialize a new CORS rules builder with existing CORS rules, if any.

Calls superclass method
# File lib/google/cloud/storage/bucket/cors.rb, line 66
def initialize rules = []
  super rules
  @original = to_gapi.map(&:to_json)
end

Public Instance Methods

add_rule(origin, methods, headers: nil, max_age: nil) click to toggle source

Add a CORS rule to the CORS rules for a bucket. Accepts options for setting preflight response headers. Preflight requests and responses are required if the request method and headers are not both [simple methods](www.w3.org/TR/cors/#simple-method) and [simple headers](www.w3.org/TR/cors/#simple-header).

@param [String, Array<String>] origin The

[origin](http://tools.ietf.org/html/rfc6454) or origins permitted
for cross origin resource sharing with the bucket. Note: "*" is
permitted in the list of origins, and means "any Origin".

@param [String, Array<String>] methods The list of HTTP methods

permitted in cross origin resource sharing with the bucket. (GET,
OPTIONS, POST, etc) Note: "*" is permitted in the list of methods,
and means "any method".

@param [String, Array<String>] headers The list of header field

names to send in the Access-Control-Allow-Headers header in the
preflight response. Indicates the custom request headers that may
be used in the actual request.

@param [Integer] max_age The value to send in the

Access-Control-Max-Age header in the preflight response. Indicates
how many seconds the results of a preflight request can be cached
in a preflight result cache. The default value is `1800` (30
minutes.)

@example

require "google/cloud/storage"

storage = Google::Cloud::Storage.new

bucket = storage.create_bucket "my-bucket" do |b|
  b.cors.add_rule ["http://example.org", "https://example.org"],
                  "*",
                  headers: ["X-My-Custom-Header"],
                  max_age: 300
end
# File lib/google/cloud/storage/bucket/cors.rb, line 113
def add_rule origin, methods, headers: nil, max_age: nil
  push Rule.new(origin, methods, headers: headers, max_age: max_age)
end
changed?() click to toggle source

@private

# File lib/google/cloud/storage/bucket/cors.rb, line 72
def changed?
  @original != to_gapi.map(&:to_json)
end
freeze() click to toggle source

@private

Calls superclass method
# File lib/google/cloud/storage/bucket/cors.rb, line 129
def freeze
  each(&:freeze)
  super
end
to_gapi() click to toggle source

@private

# File lib/google/cloud/storage/bucket/cors.rb, line 118
def to_gapi
  map(&:to_gapi)
end