Class: Sitemapper::Map

Inherits:
Object
  • Object
show all
Defined in:
lib/sitemapper/map.rb

Constant Summary collapse

SCHEMA =

:nodoc:

'http://www.sitemaps.org/schemas/sitemap/0.9'
INDENT =

:nodoc:

-1 #:nodoc:

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ Map

Initialize a map

  • file is the path to the (new or existent) sitemap.xml


25
26
27
28
29
30
# File 'lib/sitemapper/map.rb', line 25

def initialize(file)
  @builder = REXML::Document.new(File.exists?(file)? File.read(file) : nil)
  @locker = Mutex.new
  @file = file
  initialize_map
end

Class Method Details

.site_root ⇒ Object

Returns the site root (previously defined with site_root=)



11
12
13
# File 'lib/sitemapper/map.rb', line 11

def self.site_root
  @@site_root || 'http://www.example.com/'
end

.site_root=(root) ⇒ Object

Set the site root for the generated URLs



18
19
20
# File 'lib/sitemapper/map.rb', line 18

def self.site_root=(root)
  @@site_root = root
end

Instance Method Details

#map_path(path, opts = {}) ⇒ Object

Map the given path

path is the path to be mapped opts is a hash containing options for this url (see map_url)



60
61
62
# File 'lib/sitemapper/map.rb', line 60

def map_path(path, opts={})
  map_url(URI.join(Map.site_root, path), opts)
end

#map_url(loc, opts = {}) ⇒ Object

Map the given localization

loc is the URL to me mapped opts is a hash with the following parameters:

  • url or path is the complete URL or path (ex.: /articles/rails-doesnt-scale) [required]
  • lastmod a date object to the last modification [optional]
  • changefreq the frequency of change (:daily, :monthly, :yearly) [optional]
  • priority the priority of the URL (0..1) [optional]

See http://www.sitemaps.org/protocol.php



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/sitemapper/map.rb', line 43

def map_url(loc, opts={})
  lastmod, changefreq, priority = extract_options(opts)
  @locker.synchronize do
    url = get_url(loc) || @builder.root.add_element('url')
    (url.elements['loc'] || url.add_element('loc')).text = loc
    (url.elements['lastmod'] || url.add_element('lastmod')).text = lastmod.strftime('%Y-%m-%d') if lastmod
    (url.elements['changefreq'] || url.add_element('change_freq')).text = changefreq.to_s if changefreq
    (url.elements['priority'] || url.add_element('priority')).text = '%.2f' % priority if priority

    write_file
  end
end

#map_urls ⇒ Object

:nodoc:



64
65
66
# File 'lib/sitemapper/map.rb', line 64

def map_urls #:nodoc:
  # TODO: method to add various URLs and just write in the end
end

#unmap_path(loc) ⇒ Object

Unmap the given path

  • loc is the Path t be unmaped


83
84
85
# File 'lib/sitemapper/map.rb', line 83

def unmap_path(loc)
  unmap_url(URI.join(Map.site_root, loc))
end

#unmap_url(loc) ⇒ Object

Unmap the given localization (loc)

  • loc is the URL to be unmaped


71
72
73
74
75
76
77
78
# File 'lib/sitemapper/map.rb', line 71

def unmap_url(loc)
  @locker.synchronize do
    url = get_url(loc)
    url.remove if url

    write_file
  end
end