Class: Cloudfront::Helpers::Invalidation

Inherits:
Object
  • Object
show all
Includes:
Utils::ConfigurationChecker, Utils::XmlSerializer
Defined in:
lib/cloudfront/helpers/invalidation.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) ⇒ Invalidation

Returns a new instance of Invalidation.



14
15
16
17
18
19
20
21
# File 'lib/cloudfront/helpers/invalidation.rb', line 14

def initialize(&block)
  #setting default values
  @caller_reference = Cloudfront::Utils::Util.generate_caller_reference
  @files = []

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

Instance Attribute Details

#caller_referenceObject

Returns the value of attribute caller_reference.



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

def caller_reference
  @caller_reference
end

#filesObject

Returns the value of attribute files.



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

def files
  @files
end

Class Method Details

.from_hash(hash) ⇒ Object

Creates a Invalidation from a hash {

"InvalidationBatch" => {
              "Paths" => {
        "Quantity" => "number of objects to invalidate",
           "Items" => {
            "Path" => "/path to object to invalidate"
        }
    },
    "CallerReference" => "unique identifier for this invalidation batch"
}

}



45
46
47
48
49
50
51
52
# File 'lib/cloudfront/helpers/invalidation.rb', line 45

def self.from_hash(hash)
  hash = hash["InvalidationBatch"] || hash
  self.new do
    self.caller_reference = hash["CallerReference"]
    items = (hash["Paths"] || {})["Items"]
    self.files = Array.wrap (items["Path"])
  end
end

Instance Method Details

#build_xml(xml) ⇒ Object

<?xml version=“1.0” encoding=“UTF-8”?> <InvalidationBatch xmlns=“cloudfront.amazonaws.com/doc/2012-07-01/”>

<Paths>
   <Quantity>number of objects to invalidate</Quantity>
   <Items>
      <Path>/path to object to invalidate</Path>
   </Items>
</Paths>
<CallerReference>unique identifier for this invalidation batch</CallerReference>

</InvalidationBatch>



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/cloudfront/helpers/invalidation.rb', line 64

def build_xml(xml)
  check_configuration
  xml.InvalidationBatch('xmlns' => "http://cloudfront.amazonaws.com/doc/#{Cloudfront::Utils::Api.version}/") {
    xml.Paths {
      xml.Quantity @files.size
      if @files.size > 0
        xml.Items {
          for file in Array.wrap @files
            xml.Path file
          end
        }
      end
    }
    xml.CallerReference @caller_reference
  }
end

#validateObject



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

def validate
  @files = Array.wrap @files

  # Error checking
  error_messages.push "files shouldn't be empty" unless @files.any?
  for file in @files
    error_messages.push "the file '#{file}' isn't valid, it should start with '/'" unless file.start_with? '/'
  end
end