Class: Cloudfront::Helpers::CacheBehavior

Inherits:
Object
  • Object
show all
Includes:
Utils::ConfigurationChecker, Utils::XmlSerializer
Defined in:
lib/cloudfront/helpers/cache_behavior.rb

Instance Attribute Summary collapse

Attributes included from Utils::ConfigurationChecker

#error_messages

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils::XmlSerializer

#to_xml

Methods included from Utils::ConfigurationChecker

#check_configuration, #valid?

Constructor Details

#initialize(&block) ⇒ CacheBehavior

Returns a new instance of CacheBehavior.



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/cloudfront/helpers/cache_behavior.rb', line 21

def initialize(&block)
  #set default values
  @is_default = false
  @query_string_forward = true
  @cookies_forward_policy = "all"
  @trusted_signers = TrustedSigners.new
  @viewer_protocol_policy = "allow-all"
  @min_ttl = 86400 # one day as the default minimum ttl

  #set values from block
  instance_eval &block if block_given?
end

Instance Attribute Details

#cookies_forward_policyObject

Returns the value of attribute cookies_forward_policy.



11
12
13
# File 'lib/cloudfront/helpers/cache_behavior.rb', line 11

def cookies_forward_policy
  @cookies_forward_policy
end

#cookies_to_forwardObject

Returns the value of attribute cookies_to_forward.



11
12
13
# File 'lib/cloudfront/helpers/cache_behavior.rb', line 11

def cookies_to_forward
  @cookies_to_forward
end

#is_defaultObject

Returns the value of attribute is_default.



11
12
13
# File 'lib/cloudfront/helpers/cache_behavior.rb', line 11

def is_default
  @is_default
end

#min_ttlObject

Returns the value of attribute min_ttl.



11
12
13
# File 'lib/cloudfront/helpers/cache_behavior.rb', line 11

def min_ttl
  @min_ttl
end

#path_patternObject

Returns the value of attribute path_pattern.



11
12
13
# File 'lib/cloudfront/helpers/cache_behavior.rb', line 11

def path_pattern
  @path_pattern
end

#query_string_forwardObject

Returns the value of attribute query_string_forward.



11
12
13
# File 'lib/cloudfront/helpers/cache_behavior.rb', line 11

def query_string_forward
  @query_string_forward
end

#target_origin_idObject

Returns the value of attribute target_origin_id.



11
12
13
# File 'lib/cloudfront/helpers/cache_behavior.rb', line 11

def target_origin_id
  @target_origin_id
end

#trusted_signersObject

Returns the value of attribute trusted_signers.



11
12
13
# File 'lib/cloudfront/helpers/cache_behavior.rb', line 11

def trusted_signers
  @trusted_signers
end

#viewer_protocol_policyObject

Returns the value of attribute viewer_protocol_policy.



11
12
13
# File 'lib/cloudfront/helpers/cache_behavior.rb', line 11

def viewer_protocol_policy
  @viewer_protocol_policy
end

Class Method Details

.from_hash(hash) ⇒ Object

Creates a CacheBehavior instance from a hash

{
  "CacheBehavior" => {
      "PathPattern"     => "*",
      "TargetOriginId"  => "id",
      "ForwardedValues" => {
          "QueryString"   => "true",
          "Cookies"       => {
              "Forward"          => "all | whitelist | none",
              "WhitelistedNames" => {
                  "Quantity"       => "0",
                  "Items"          =>
                      {
                          "Name"    => ["name of a cookie to forward to the origin"]
                      }
              }
          }
      },
      "TrustedSigners"       => {},
      "ViewerProtocolPolicy" => "allow-all",
      "MinTTL"                => "86400"
  }
}


104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/cloudfront/helpers/cache_behavior.rb', line 104

def self.from_hash(hash)
  hash = hash["DefaultCacheBehavior"] || hash["CacheBehavior"] || hash
  self.new do
    self.is_default = !hash.has_key?("PathPattern")
    self.path_pattern = hash["PathPattern"]
    self.target_origin_id = hash["TargetOriginId"]
    forwarded_values = hash["ForwardedValues"]
    if forwarded_values
      self.query_string_forward = (forwarded_values["QueryString"] || "true").to_bool
      cookies = forwarded_values["Cookies"]
      if cookies
        self.cookies_forward_policy = cookies["Forward"] || "all"
        white_listed_names = cookies["WhitelistedNames"]
        if (white_listed_names && white_listed_names["Items"]||{})["Name"]
          self.cookies_to_forward = Array.wrap white_listed_names["Items"]["Name"]
        end
      end
    end
    self.trusted_signers = TrustedSigners.from_hash(hash["TrustedSigners"])
    self.viewer_protocol_policy = hash["ViewerProtocolPolicy"] || "allow-all"
    self.min_ttl = (hash["MinTTL"] || "86400").to_i
  end
end

Instance Method Details

#build_xml(xml) ⇒ Object

The cache behavior class container

<CacheBehavior>
  <PathPattern>pattern that specifies files that this cache behavior applies to</PathPattern>
  <TargetOriginId>ID of the origin that this cache behavior applies to</TargetOriginId>
  <ForwardedValues>
    <QueryString>true | false</QueryString>
    <Cookies>
      <Forward>all | whitelist | none</Forward>
      <!-- Required when Forward = whitelist, omitted otherwise. -->
      <WhitelistedNames>
        <Quantity>number of cookie names to forward to origin</Quantity>
        <Items>
          <Name>name of a cookie to forward to the origin</Name>
        </Items>
      </WhitelistedNames>
    </Cookies>
  </ForwardedValues>
  <TrustedSigners>
    ... see TrustedSigners class
  </TrustedSigners>
  <ViewerProtocolPolicy>allow-all | https-only</ViewerProtocolPolicy>
  <MinTTL>minimum TTL in seconds for files specified by PathPattern</MinTTL>
</CacheBehavior>


151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/cloudfront/helpers/cache_behavior.rb', line 151

def build_xml(xml)
  check_configuration
  if @is_default
    xml.DefaultCacheBehavior {
      cache_behavior_body(xml)
    }
  else
    xml.CacheBehavior {
      cache_behavior_body(xml)
    }
  end
end

#cache_behavior_body(xml) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/cloudfront/helpers/cache_behavior.rb', line 53

def cache_behavior_body(xml)
  unless @is_default
    xml.PathPattern @path_pattern
  end
  xml.TargetOriginId @target_origin_id
  xml.ForwardedValues {
    xml.QueryString @query_string_forward
    xml.Cookies {
      xml.Forward @cookies_forward_policy
      if (@cookies_forward_policy == "whitelist")
        xml.WhitelistedNames {
          xml.Quantity @cookies_to_forward.size
          if (@cookies_to_forward.size > 0)
            xml.Items {
              for cookie in @cookies_to_forward
                xml.Name cookie
              end
            }
          end
        }
      end
    }
  }
  @trusted_signers.build_xml(xml)
  xml.ViewerProtocolPolicy @viewer_protocol_policy
  xml.MinTTL @min_ttl
end

#validateObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/cloudfront/helpers/cache_behavior.rb', line 34

def validate
  # some wrapping
  @cookies_to_forward = Array.wrap @cookies_to_forward

  # Error checking
  error_messages.push "target_origin_id shouldn't be nil" if @target_origin_id.nil?
  error_messages.push "query_string_forward must be a boolean" unless !!@query_string_forward == @query_string_forward
  error_messages.push "cookies_forward_policy should be one of #{Cloudfront::Utils::Util::FORWARD_POLICY_VALUES.join(', ')}" unless Cloudfront::Utils::Util::FORWARD_POLICY_VALUES.include?(@cookies_forward_policy)
  # trusted signers validation
  if @trusted_signers.is_a?(TrustedSigners)
    @trusted_signers.check_configuration
  else
    error_messages.push "trusted_signer field should be a TrustedSigners"
  end

  error_messages.push "viewer_protocol_policy should be one of #{Cloudfront::Utils::Util::VIEWER_PROTOCOL_VALUES.join(', ')}" unless Cloudfront::Utils::Util::VIEWER_PROTOCOL_VALUES.include?(@viewer_protocol_policy)
  error_messages.push "min_ttl should be a number" unless @min_ttl.is_a? Fixnum
end