Class: Gcloud::Storage::Bucket::Cors
- Inherits:
-
Array
- Object
- Array
- Gcloud::Storage::Bucket::Cors
- Defined in:
- lib/gcloud/storage/bucket/cors.rb
Overview
Bucket Cors
A special-case Array for managing the website CORS rules for a bucket. Accessed via a block argument to Project#create_bucket, Bucket#cors, or Bucket#update.
For more information about CORS, see Cross-Origin Resource Sharing (CORS).
require "gcloud"
gcloud = Gcloud.new
storage = gcloud.storage
bucket = storage.bucket "my-todo-app"
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"],
response_headers: ["X-My-Custom-Header"],
max_age: 3600
end
Instance Method Summary collapse
-
#add_rule(origin, methods, headers: nil, max_age: nil) ⇒ Object
Add a CORS rule to the CORS rules for a bucket.
-
#changed? ⇒ Boolean
:nodoc:.
-
#initialize(cors = []) ⇒ Cors
constructor
Initialize a new CORS rules builder with existing CORS rules, if any.
Constructor Details
#initialize(cors = []) ⇒ Cors
Initialize a new CORS rules builder with existing CORS rules, if any.
51 52 53 54 |
# File 'lib/gcloud/storage/bucket/cors.rb', line 51 def initialize cors = [] #:nodoc: super cors.dup @original = cors.dup end |
Instance Method Details
#add_rule(origin, methods, headers: nil, max_age: nil) ⇒ Object
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 and simple headers.
Parameters
origin-
The origin or origins permitted for cross origin resource sharing with the bucket. Note: “*” is permitted in the list of origins, and means “any Origin”. (
StringorArray) 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”. (
StringorArray) 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. (
StringorArray) 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.) (Integer)
Example
require "gcloud"
gcloud = Gcloud.new
storage = gcloud.storage
bucket = storage.create_bucket "my-bucket" do |c|
c.add_rule ["http://example.org", "https://example.org"],
"*",
response_headers: ["X-My-Custom-Header"],
max_age: 300
end
103 104 105 106 107 108 |
# File 'lib/gcloud/storage/bucket/cors.rb', line 103 def add_rule origin, methods, headers: nil, max_age: nil rule = { "origin" => Array(origin), "method" => Array(methods) } rule["responseHeader"] = Array(headers) || [] rule["maxAgeSeconds"] = max_age || 1800 push rule end |
#changed? ⇒ Boolean
:nodoc:
56 57 58 |
# File 'lib/gcloud/storage/bucket/cors.rb', line 56 def changed? #:nodoc: @original != self end |