Class: Cloudfront::Helpers::DownloadDistribution

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

Returns a new instance of DownloadDistribution.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/cloudfront/helpers/download_distribution.rb', line 26

def initialize(&block)
  #setting default values
  @caller_reference = Cloudfront::Utils::Util.generate_caller_reference
  @cnames = []
  @default_root_object = "/index.html"
  @origins = []
  @default_cache_behavior = CacheBehavior.new do
    self.is_default = true
  end
  @cache_behaviors = []
  @comment = "Created with cloudfront Gem, visit https://github.com/Belkacem/cloudfront"
  @logging = Logging.new
  @price_class = "PriceClass_All"
  @enabled = false

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

Instance Attribute Details

#cache_behaviorsObject

Returns the value of attribute cache_behaviors.



15
16
17
# File 'lib/cloudfront/helpers/download_distribution.rb', line 15

def cache_behaviors
  @cache_behaviors
end

#caller_referenceObject

Returns the value of attribute caller_reference.



15
16
17
# File 'lib/cloudfront/helpers/download_distribution.rb', line 15

def caller_reference
  @caller_reference
end

#cnamesObject

Returns the value of attribute cnames.



15
16
17
# File 'lib/cloudfront/helpers/download_distribution.rb', line 15

def cnames
  @cnames
end

#commentObject

Returns the value of attribute comment.



15
16
17
# File 'lib/cloudfront/helpers/download_distribution.rb', line 15

def comment
  @comment
end

#default_cache_behaviorObject

Returns the value of attribute default_cache_behavior.



15
16
17
# File 'lib/cloudfront/helpers/download_distribution.rb', line 15

def default_cache_behavior
  @default_cache_behavior
end

#default_root_objectObject

Returns the value of attribute default_root_object.



15
16
17
# File 'lib/cloudfront/helpers/download_distribution.rb', line 15

def default_root_object
  @default_root_object
end

#enabledObject

Returns the value of attribute enabled.



15
16
17
# File 'lib/cloudfront/helpers/download_distribution.rb', line 15

def enabled
  @enabled
end

#loggingObject

Returns the value of attribute logging.



15
16
17
# File 'lib/cloudfront/helpers/download_distribution.rb', line 15

def logging
  @logging
end

#originsObject

Returns the value of attribute origins.



15
16
17
# File 'lib/cloudfront/helpers/download_distribution.rb', line 15

def origins
  @origins
end

#price_classObject

Returns the value of attribute price_class.



15
16
17
# File 'lib/cloudfront/helpers/download_distribution.rb', line 15

def price_class
  @price_class
end

Class Method Details

.from_hash(hash) ⇒ Object

Creates a distribution configuration wrapper from a hash {

"DistributionConfig" => {
    "CallerReference" => "unique description for this distribution config",
    "Aliases" => "Aliases class",
    "DefaultRootObject" => "URL for default root object",
    "Origins" => ... see Origins class",
    "DefaultCacheBehavior" =>
        "\n... see CacheBehaviour class\n                       ",
    "CacheBehaviors" => {
       ... see CacheBehaviors
     },
    "Comment" => "comment about the distribution",
    "Logging" =>
          "... see Logging class",
    "PriceClass" => "maximum price class for the distribution",
    "Enabled" => "true | false"
}

}



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/cloudfront/helpers/download_distribution.rb', line 93

def self.from_hash(hash)
  hash = hash["DistributionConfig"] || hash
  self.new do
    self.caller_reference = hash["CallerReference"]
    self.cnames = Aliases.from_hash(hash["Aliases"]).cnames # Aliases class contains the code that extract cnames
    self.default_root_object = hash["DefaultRootObject"]
    self.origins = Origins.from_hash(hash["Origins"]).origins
    self.default_cache_behavior = CacheBehavior.from_hash(hash["DefaultCacheBehavior"])
    self.cache_behaviors = CacheBehaviors.from_hash(hash["CacheBehaviors"]).cache_behaviors
    self.comment = hash["Comment"]
    self.logging = Logging.from_hash(hash["Logging"])
    self.price_class = hash["PriceClass"] || "PriceClass_All"
    self.enabled = (hash["Enabled"] || "false").to_bool
  end
end

Instance Method Details

#build_xml(xml) ⇒ Object

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

<CallerReference>unique description for this distribution config</CallerReference>
<Aliases>
  ... see Alias class
</Aliases>
<DefaultRootObject>URL for default root object</DefaultRootObject>
<Origins>
  <Quantity>number of origins</Quantity>
  <Items>
     ... see Origin class
  </Items>
</Origins>
<DefaultCacheBehavior>
    ... see CacheBehaviour class
</DefaultCacheBehavior>
<CacheBehaviors>
  <Quantity>number of cache behaviors</Quantity>
  <!-- Optional. Omit when Quantity = 0. -->
  <Items>
    ... see CacheBehaviour class
  </Items>
</CacheBehaviors>
<Comment>comment about the distribution</Comment>
<Logging>
  ... see Logging class
</Logging>
<PriceClass>maximum price class for the distribution</PriceClass>
<Enabled>true | false</Enabled>

</DistributionConfig>



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/cloudfront/helpers/download_distribution.rb', line 139

def build_xml(xml)
  check_configuration
  xml.DistributionConfig('xmlns' => "http://cloudfront.amazonaws.com/doc/#{Cloudfront::Utils::Api.version}/") {
    xml.CallerReference @caller_reference
    @aliases.build_xml(xml)
    xml.DefaultRootObject @default_root_object
    @origins_container.build_xml(xml)
    @default_cache_behavior.build_xml(xml)
    @behaviors.build_xml(xml)
    xml.Comment @comment
    @logging.build_xml(xml)
    xml.PriceClass @price_class
    xml.Enabled @enabled
  }
end

#validateObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/cloudfront/helpers/download_distribution.rb', line 45

def validate
  this = self

  # aliases and behaviors are internal structures.
  @aliases = Aliases.new do
    self.cnames = this.cnames
  end

  @behaviors = CacheBehaviors.new do
    self.cache_behaviors = this.cache_behaviors
  end

  @origins_container = Origins.new do
    self.origins = this.origins
  end
  
  # Error checking
  error_messages.push "The caller_reference shouldn't be empty" if @caller_reference.empty?
  @aliases.check_configuration
  @origins_container.check_configuration
  @default_cache_behavior.check_configuration
  # Making the default cache behavior to true
  @default_cache_behavior.is_default = true
  @behaviors.check_configuration
  @logging.check_configuration
  error_messages.push "price_class is invalid should be in #{Cloudfront::Utils::Util::PRICE_CLASS.join(', ')}" unless Cloudfront::Utils::Util::PRICE_CLASS.include?(@price_class)
  error_messages.push "enabled should be a boolean" unless !!@enabled == @enabled
end