Class: Skypager::Site
- Inherits:
-
Object
- Object
- Skypager::Site
- Defined in:
- lib/skypager/site.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
Returns the value of attribute config.
-
#name ⇒ Object
Returns the value of attribute name.
-
#options ⇒ Object
Returns the value of attribute options.
Class Method Summary collapse
- .directory ⇒ Object
- .directory=(value) ⇒ Object
- .find_site_by_dropbox(uid, path_prefix) ⇒ Object
- .find_sites_for_dropbox_uid(uid) ⇒ Object
- .requiring_build ⇒ Object
- .sites ⇒ Object
Instance Method Summary collapse
- #after_setup ⇒ Object
- #aliases ⇒ Object
- #bucket(create = false) ⇒ Object
- #bucket_host ⇒ Object
- #bucket_hostname ⇒ Object
- #bucket_key ⇒ Object
- #bucket_name ⇒ Object
- #bucket_url ⇒ Object
- #build_command ⇒ Object
-
#build_manifest ⇒ Object
TODO Let’s store build manifests in their own files.
- #cdn ⇒ Object
- #cname_value ⇒ Object
- #custom_domain ⇒ Object
- #custom_domain? ⇒ Boolean
- #delete! ⇒ Object
- #deploy(build_dir) ⇒ Object
- #deploy_options ⇒ Object
- #deploy_options_config_string ⇒ Object
- #description ⇒ Object
- #dns_domain ⇒ Object
- #dns_domain_records ⇒ Object
- #fetch(key, &block) ⇒ Object
- #get(attribute) ⇒ Object
- #get!(attribute) ⇒ Object
- #has_cdn? ⇒ Boolean
- #has_cname_record? ⇒ Boolean
- #hostname ⇒ Object
- #hostname=(value) ⇒ Object
-
#initialize(name, options = {}) ⇒ Site
constructor
A new instance of Site.
- #load_config ⇒ Object
- #paths_to_invalidate(app) ⇒ Object
- #prepare_deploy(build_dir) ⇒ Object
- #rebuild_manifest(build_dir) ⇒ Object
- #remove_build_manifest ⇒ Object
- #require_build!(state = true) ⇒ Object
- #requires_build!(state = true) ⇒ Object
- #requires_build? ⇒ Boolean
- #root ⇒ Object
- #save! ⇒ Object
- #set(attribute, value) ⇒ Object
- #setup_cname ⇒ Object
- #syncables ⇒ Object
- #to_config ⇒ Object
- #use_cdn? ⇒ Boolean
Constructor Details
#initialize(name, options = {}) ⇒ Site
Returns a new instance of Site.
54 55 56 57 58 59 60 61 62 |
# File 'lib/skypager/site.rb', line 54 def initialize(name, = {}) @name = name.to_s.downcase @options = @config = .delete(:config) @config ||= self.class.directory[@name] ||= {name: @name} @config = @config.to_mash if @config.is_a?(Hash) @config.reverse_merge!([:deploy_options]) if [:deploy_options] end |
Instance Attribute Details
#config ⇒ Object
Returns the value of attribute config.
49 50 51 |
# File 'lib/skypager/site.rb', line 49 def config @config end |
#name ⇒ Object
Returns the value of attribute name.
49 50 51 |
# File 'lib/skypager/site.rb', line 49 def name @name end |
#options ⇒ Object
Returns the value of attribute options.
49 50 51 |
# File 'lib/skypager/site.rb', line 49 def @options end |
Class Method Details
.directory ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/skypager/site.rb', line 9 def directory unless Skypager.config.home_config.key?('sites_directory') self.directory = {} end @directory ||= begin val = Skypager.config.home_config['sites_directory'] ||= {}.to_mash val = val.to_mash if val.is_a?(Hash) val end end |
.directory=(value) ⇒ Object
4 5 6 7 |
# File 'lib/skypager/site.rb', line 4 def directory= value value = value.to_mash if value.respond_to?(:to_mash) @directory = Skypager.config.home_config['sites_directory'] = {}.to_mash end |
.find_site_by_dropbox(uid, path_prefix) ⇒ Object
40 41 42 43 44 45 46 |
# File 'lib/skypager/site.rb', line 40 def find_site_by_dropbox uid, path_prefix find_sites_for_dropbox_uid(uid).find do |site| Array(site.syncables.dropbox.paths).any? do |path| path == path_prefix || path.match(/^#{ path_prefix }/) end end end |
.find_sites_for_dropbox_uid(uid) ⇒ Object
29 30 31 32 33 34 35 36 37 38 |
# File 'lib/skypager/site.rb', line 29 def find_sites_for_dropbox_uid uid configs = sites.select do |site| syncables = site.syncables dropbox = syncables.dropbox ||= {}.to_mash uid.to_s.length > 0 && dropbox.uid.to_s == uid.to_s end configs.map {|cfg| Skypager::Site.new(cfg.name, config: cfg) } end |
.requiring_build ⇒ Object
25 26 27 |
# File 'lib/skypager/site.rb', line 25 def requiring_build sites.select {|site| site.requires_build == "yes" } end |
.sites ⇒ Object
21 22 23 |
# File 'lib/skypager/site.rb', line 21 def sites directory.values end |
Instance Method Details
#after_setup ⇒ Object
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 |
# File 'lib/skypager/site.rb', line 258 def after_setup if use_cdn? && !has_cdn? Skypager.amazon.create_cdn_for(self) end if Skypager.config.dnsimple_setup? setup_cname() unless custom_domain? else puts "You will need to setup the DNS for this bucket yourself." if has_cdn? puts "You will want to configure the DNS for #{ custom_domain }" puts "Create a CNAME and point to #{ cname_value }" else puts "Create a CNAME for #{ cname_value } on #{ Skypager.config.domain } and point to #{ bucket_host }" end end end |
#aliases ⇒ Object
364 365 366 367 368 |
# File 'lib/skypager/site.rb', line 364 def aliases if custom_domain [custom_domain, "www.#{ custom_domain }"] end end |
#bucket(create = false) ⇒ Object
155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/skypager/site.rb', line 155 def bucket(create=false) return @bucket if @bucket val = .fetch(:bucket_name) { bucket_hostname } b = Skypager.amazon.find_bucket_by_name(val) rescue nil if create && !b b = Skypager.amazon.create_bucket(val) end @bucket = b end |
#bucket_host ⇒ Object
332 333 334 |
# File 'lib/skypager/site.rb', line 332 def bucket_host URI.parse(bucket_url).host end |
#bucket_hostname ⇒ Object
151 152 153 |
# File 'lib/skypager/site.rb', line 151 def bucket_hostname "#{@config[:name] || @name}.#{ Skypager.config.domain }" end |
#bucket_key ⇒ Object
310 311 312 |
# File 'lib/skypager/site.rb', line 310 def bucket_key fetch(:bucket_key) { bucket && bucket.key } end |
#bucket_name ⇒ Object
314 315 316 |
# File 'lib/skypager/site.rb', line 314 def bucket_name bucket_key end |
#bucket_url ⇒ Object
328 329 330 |
# File 'lib/skypager/site.rb', line 328 def bucket_url @bucket_url ||= fetch(:bucket_url) { Skypager.amazon.website_url_for(bucket) } end |
#build_command ⇒ Object
64 65 66 |
# File 'lib/skypager/site.rb', line 64 def build_command "bundle exec middleman build" end |
#build_manifest ⇒ Object
TODO Let’s store build manifests in their own files
233 234 235 |
# File 'lib/skypager/site.rb', line 233 def build_manifest @build_manifest ||= get!(:build_manifest) || {} end |
#cdn ⇒ Object
358 359 360 361 362 |
# File 'lib/skypager/site.rb', line 358 def cdn @cdn ||= Skypager.amazon.cdn.distributions.find do |cdn| cdn.comment == description end end |
#cname_value ⇒ Object
340 341 342 343 344 345 346 |
# File 'lib/skypager/site.rb', line 340 def cname_value if custom_domain && has_cdn? cdn.domain else name end end |
#custom_domain ⇒ Object
318 319 320 321 322 |
# File 'lib/skypager/site.rb', line 318 def custom_domain value = get!(:custom_domain) value = .custom_domain if value.nil? || value.to_s.length > 0 value end |
#custom_domain? ⇒ Boolean
289 290 291 |
# File 'lib/skypager/site.rb', line 289 def custom_domain? "#{get!(:custom_domain) || .custom_domain}".to_s.length > 0 end |
#delete! ⇒ Object
72 73 74 75 76 |
# File 'lib/skypager/site.rb', line 72 def delete! directory = self.class.directory directory.delete(name) save! end |
#deploy(build_dir) ⇒ Object
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/skypager/site.rb', line 194 def deploy(build_dir) target = bucket || Skypager.amazon.s3_bucket if target.to_s.match(/there is no bucket named/i) puts "== The S3 Bucket is not setup for this site." puts "==== Please run 'bundle exec skypager setup deployment' to begin this process" return end payload = prepare_deploy(build_dir) if payload.empty? puts "== Nothing to deploy" end payload.each do |hash| body = hash[:node].read if target.files.create(key: hash[:key], body: body, public: true) rel = hash[:key] puts "== Deployed #{ body.length } bytes to #{rel}" build_manifest[rel] = hash[:digest] end end set(:build_manifest, build_manifest) payload.length end |
#deploy_options ⇒ Object
137 138 139 140 141 |
# File 'lib/skypager/site.rb', line 137 def .fetch(:deploy_options) do (config. || {}).to_mash end end |
#deploy_options_config_string ⇒ Object
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 |
# File 'lib/skypager/site.rb', line 293 def custom_domain = get!(:custom_domain) cfg = [] if custom_domain cfg.push(":custom_domain => '#{ custom_domain }'") cfg.push(":use_cdn => true") else cfg.push(":domain => '#{ Skypager.config.domain }'") end cfg.push(":bucket_name => '#{ bucket_name }'") "\ndeploy_to(:amazon, #{cfg.join(", ")})\n" end |
#description ⇒ Object
348 349 350 |
# File 'lib/skypager/site.rb', line 348 def description "#{ name }.#{ Skypager.config.domain }" end |
#dns_domain ⇒ Object
277 278 279 |
# File 'lib/skypager/site.rb', line 277 def dns_domain Skypager.dns.domain end |
#dns_domain_records ⇒ Object
281 282 283 |
# File 'lib/skypager/site.rb', line 281 def dns_domain_records Skypager.dns.domain_records end |
#fetch(key, &block) ⇒ Object
324 325 326 |
# File 'lib/skypager/site.rb', line 324 def fetch(key, &block) @config.fetch(key, &block) end |
#get(attribute) ⇒ Object
133 134 135 |
# File 'lib/skypager/site.rb', line 133 def get attribute @config[attribute.to_sym] end |
#get!(attribute) ⇒ Object
123 124 125 126 127 128 129 130 131 |
# File 'lib/skypager/site.rb', line 123 def get! attribute val = get(attribute) if val.nil? || val.to_s.length == 0 nil else val end end |
#has_cdn? ⇒ Boolean
352 353 354 355 356 |
# File 'lib/skypager/site.rb', line 352 def has_cdn? @cdn_distribution ||= Skypager.amazon.cdn.distributions.find do |cdn| cdn.comment == description end end |
#has_cname_record? ⇒ Boolean
285 286 287 |
# File 'lib/skypager/site.rb', line 285 def has_cname_record? get!(:has_cname) end |
#hostname ⇒ Object
147 148 149 |
# File 'lib/skypager/site.rb', line 147 def hostname custom_domain || bucket_hostname end |
#hostname=(value) ⇒ Object
143 144 145 |
# File 'lib/skypager/site.rb', line 143 def hostname= value set(:hostname, value) end |
#load_config ⇒ Object
88 89 90 91 92 |
# File 'lib/skypager/site.rb', line 88 def load_config if existing = self.class.directory[name] @config = existing.to_mash end end |
#paths_to_invalidate(app) ⇒ Object
254 255 256 |
# File 'lib/skypager/site.rb', line 254 def paths_to_invalidate(app) app.sitemap.resources.map {|r| "/#{r.request_path}" } end |
#prepare_deploy(build_dir) ⇒ Object
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/skypager/site.rb', line 169 def prepare_deploy(build_dir) @deploy_targets = [] upload = lambda do |node| next if node.to_s == ".DS_Store" if node.directory? Array(node.children).each(&upload) elsif node.file? rel = node.relative_path_from(build_dir).to_s previous = build_manifest[rel] current = Digest::MD5.hexdigest(node.read) if current != previous @deploy_targets << {key: rel, node: node, digest: current} end end end build_dir.children.each(&upload) @deploy_targets end |
#rebuild_manifest(build_dir) ⇒ Object
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
# File 'lib/skypager/site.rb', line 237 def rebuild_manifest(build_dir) walker = lambda do |node| next if node.to_s == ".DS_Store" if node.directory? Array(node.children).each(&walker) elsif node.file? rel = node.relative_path_from(build_dir).to_s digest = Digest::MD5.hexdigest(node.read) build_manifest[rel] = digest end end build_dir.children.each(&walker) set(:build_manifest, build_manifest) end |
#remove_build_manifest ⇒ Object
227 228 229 |
# File 'lib/skypager/site.rb', line 227 def remove_build_manifest set(:build_manifest, @build_manifest = {}) end |
#require_build!(state = true) ⇒ Object
103 104 105 |
# File 'lib/skypager/site.rb', line 103 def require_build!(state=true) requires_build!(state) end |
#requires_build!(state = true) ⇒ Object
107 108 109 110 |
# File 'lib/skypager/site.rb', line 107 def requires_build!(state=true) state = !!(state) ? "yes" : "no" set(:requires_build, state) end |
#requires_build? ⇒ Boolean
112 113 114 115 |
# File 'lib/skypager/site.rb', line 112 def requires_build? value = get!(:requires_build) value == "yes" end |
#root ⇒ Object
84 85 86 |
# File 'lib/skypager/site.rb', line 84 def root @config[:root].to_s.length > 0 && Pathname(@config[:root]) end |
#save! ⇒ Object
78 79 80 81 82 |
# File 'lib/skypager/site.rb', line 78 def save! directory = Skypager.config.home_config["sites_directory"] || {} directory[name] = to_config Skypager.config.save_home_config end |
#set(attribute, value) ⇒ Object
117 118 119 120 121 |
# File 'lib/skypager/site.rb', line 117 def set attribute, value @config[attribute.to_sym] = value save! self end |
#setup_cname ⇒ Object
336 337 338 |
# File 'lib/skypager/site.rb', line 336 def setup_cname Skypager.dns.setup_cname(cname_value, bucket_host) end |
#syncables ⇒ Object
68 69 70 |
# File 'lib/skypager/site.rb', line 68 def syncables config[:syncables].try(:to_mash) end |
#to_config ⇒ Object
99 100 101 |
# File 'lib/skypager/site.rb', line 99 def to_config config end |
#use_cdn? ⇒ Boolean
223 224 225 |
# File 'lib/skypager/site.rb', line 223 def use_cdn? .fetch(:custom_domain).to_s.length > 0 || (get(:use_cdn) == true || get(:use_cdn) == "yes" || get(:use_cdn) == "true") end |