Class: StaticdUtils::Sitemap

Inherits:
Object
  • Object
show all
Defined in:
lib/staticd_utils/sitemap.rb

Overview

Manifest for Staticd releases.

A Sitemap consist of an associative array representing each resources of a site release. Each entry consist of the sha1 digest of the resource content and the complete HTTP path this resource must be available to.

Example:

sitemap = StaticdUtils::Sitemap.create("/tmp/my_website")
sitemap.to_h
# => {
       "058ec3fa8aab4c0ccac27d80fd24f30a8730d3f6"=>"/index.html",
       "92136ff551f50188f46486ab80db269eda4dfd4e"=>"/hello/world.html"
     }

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(map) ⇒ Sitemap

Create a sitemap from an associative array.

The associative array must have the folowing structure:

  • Key: the sha1 of the ressource

  • Value: the HTTP path of the resource

Example:

sitemap = Sitemap.new({
  058ec3fa8aab4c0ccac27d80fd24f30a8730d3f6: "hi.html"
})


68
69
70
# File 'lib/staticd_utils/sitemap.rb', line 68

def initialize(map)
  @map = map
end

Class Method Details

.create(path) ⇒ Object

Create a sitemap from a directory content.

It register each files digest and path inside the sitemap.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/staticd_utils/sitemap.rb', line 25

def self.create(path)
  map = {}
  if File.directory?(path)
    Dir.chdir(path) do
      Dir["**/*"].each do |object|
        if File.file?(object)
          sha1 = Digest::SHA1.hexdigest(File.read(object))
          map[sha1] = "/#{object}"
        end
      end
    end
  end
  new(map)
end

.open(yaml) ⇒ Object

Create a sitemap from a YAML string.

The YAML string must reflect the sitemap associative array structure.

Example:

yaml = "---\n058ec3fa8aab4c0ccac27d80fd24f30a8730d3f6: \"/hi.html\"\n"
sitemap = StaticdUtils::Sitemap.open(yaml)


47
48
49
# File 'lib/staticd_utils/sitemap.rb', line 47

def self.open(yaml)
  new(YAML.load(yaml))
end

.open_file(path) ⇒ Object

Create a sitemap from a YAML file.

The YAML file must reflect the sitemap associative array structure.



54
55
56
# File 'lib/staticd_utils/sitemap.rb', line 54

def self.open_file(path)
  open(File.read(path))
end

Instance Method Details

#digestsObject

View all sha1 digest of the sitemap.



78
79
80
# File 'lib/staticd_utils/sitemap.rb', line 78

def digests
  @map.map { |sha1, path| sha1 }
end

#each_resourcesObject

Iterate over each resources of the sitemap.



83
84
85
# File 'lib/staticd_utils/sitemap.rb', line 83

def each_resources
  @map.each { |sha1, path| yield sha1, path }
end

#routesObject

View all HTTP path of the sitemap.



73
74
75
# File 'lib/staticd_utils/sitemap.rb', line 73

def routes
  @map.map { |sha1, path| path }
end

#to_hObject



87
88
89
# File 'lib/staticd_utils/sitemap.rb', line 87

def to_h
  @map
end

#to_memory_fileObject

Export the sitemap to a YAML file stored into memory.



96
97
98
# File 'lib/staticd_utils/sitemap.rb', line 96

def to_memory_file
  StaticdUtils::MemoryFile.new(StringIO.new(to_yaml))
end

#to_yamlObject



91
92
93
# File 'lib/staticd_utils/sitemap.rb', line 91

def to_yaml
  @map.to_yaml
end