Module: CORS::Policy

Included in:
S3, S3Post
Defined in:
lib/cors/policy.rb,
lib/cors/policy/s3.rb,
lib/cors/policy/s3_post.rb

Overview

Mixin for declaring CORS Policies.

Classes who include this mixin should define both #manifest and #sign.

Examples:

class S3
  include CORS::Policy

  def manifest
    # create the manifest
    [].tap do |manifest|
      manifest << attributes["method"].upcase
    end.join("\n")
  end

  def sign(access_key, secret_access_key)
    # sign the manifest
  end
end

policy = S3.create do |rules|
  rules.required "method", %w[GET]
end

See Also:

Defined Under Namespace

Modules: ClassMethods Classes: S3, S3Post

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#attributesHash<String, Object>

Returns:

  • (Hash<String, Object>)


79
80
81
# File 'lib/cors/policy.rb', line 79

def attributes
  @attributes
end

#errorsHash

Returns:

  • (Hash)


83
84
85
# File 'lib/cors/policy.rb', line 83

def errors
  @errors
end

Class Method Details

.included(other) ⇒ Object

Extends the target with ClassMethods

Parameters:

  • other (#extend)


60
61
62
# File 'lib/cors/policy.rb', line 60

def included(other)
  other.extend(ClassMethods)
end

Instance Method Details

#initialize(attributes) ⇒ Object

Note:

attribute keys are converted to strings and downcased for validation

Note:

validations are run instantly

Initialize the policy with the given attributes and validate the attributes.

Parameters:

  • attributes (Hash)

See Also:



73
74
75
76
# File 'lib/cors/policy.rb', line 73

def initialize(attributes)
  self.attributes = Hash[normalize_attributes(attributes)]
  self.errors = rules.validate(self.attributes)
end

#rulesCORS::Rules

Returns rules assigned to this policy.

Returns:

Raises:

  • (RuntimeError)

    raises if no rules have been defined



88
89
90
# File 'lib/cors/policy.rb', line 88

def rules
  self.class.rules or raise "no rules defined for policy #{inspect}"
end

#sign(*args, &block) ⇒ String

Note:

should not be overridden by the includers!

Signs the manifest, but only if it is #valid?.

Returns:

  • (String)

    signature derived from the manifest



101
102
103
# File 'lib/cors/policy.rb', line 101

def sign(*args, &block)
  sign!(*args, &block) if valid?
end

#sign!String

Note:

should be overridden by includers!

Returns signature derived from the manifest.

Returns:

  • (String)

    signature derived from the manifest

Raises:

  • (NotImplementedError)


107
108
109
# File 'lib/cors/policy.rb', line 107

def sign!(*)
  raise NotImplementedError, "#sign has not been defined on #{inspect}"
end

#valid?Boolean

Returns true if no errors was encountered during validation in #initialize.

Returns:

  • (Boolean)

    true if no errors was encountered during validation in #initialize



93
94
95
# File 'lib/cors/policy.rb', line 93

def valid?
  errors.empty?
end