Class: Darkext::SitemapBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/darkext/sitemap_generator.rb

Constant Summary collapse

FORUM_CLASS_DEFAULT =
'forumtitle'
TOPIC_CLASS_DEFAULT =
'topictitle'
FORUM_PRIORITY_DEFAULT =
0.8
TOPIC_PRIORITY_DEFAULT =
0.5
PAGE_PRIORITY_DEFAULT =
0.1
FORUM_CHANGE_FREQ_DEFAULT =
'daily'
TOPIC_CHANGE_FREQ_DEFAULT =
'hourly'
PAGE_CHANGE_FREQ_DEFAULT =
'monthly'

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ SitemapBuilder

Returns a new instance of SitemapBuilder.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/darkext/sitemap_generator.rb', line 17

def initialize(opts={})
  defaults = {
    :forum => {
      :class => FORUM_CLASS_DEFAULT,
      :priority => FORUM_PRIORITY_DEFAULT,
      :change_freq => FORUM_CHANGE_FREQ_DEFAULT
    },
    :topic => {
      :class => TOPIC_CLASS_DEFAULT,
      :priority => TOPIC_PRIORITY_DEFAULT,
      :change_freq => TOPIC_CHANGE_FREQ_DEFAULT
    },
    :page => {
      :priority => PAGE_PRIORITY_DEFAULT,
      :change_freq => PAGE_CHANGE_FREQ_DEFAULT
    }
  }
  defaults.deep_merge!(opts)

  @options = defaults

  @forums = Array.new
  @topics = Array.new
  @pages = Array.new
end

Instance Method Details

#index(url, target = $stdout) ⇒ Object



43
44
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
73
74
75
76
# File 'lib/darkext/sitemap_generator.rb', line 43

def index(url, target = $stdout)
  @pages << url

  root = Hpricot(open(url))
  (root/"a.#{@options.nested_find(:forum,:class)}").each do |link|
    index_forum(link.attributes['href'].to_s)
  end

  xml = Builder::XmlMarkup.new(:target => $stdout, :indent => 1)
  xml.instruct!
  xml.urlset "xmlns" => "http://www.sitemaps.org/schemas/sitemap/0.9" do
    @pages.each do |page|
      xml.url do
        xml.loc(page)
        xml.changefreq(@options.nested_find(:page,:change_freq))
        xml.priority(@options.nested_find(:page,:priority))
      end
    end
    @forums.each do |forum|
      xml.url do
        xml.loc(forum)
        xml.changefreq(@options.nested_find(:forum,:change_freq))
        xml.priority(@options.nested_find(:forum,:priority))
      end
    end
    @topics.each do |topic|
      xml.url do
        xml.loc(topic)
        xml.changefreq(@options.nested_find(:topic,:change_freq))
        xml.priority(@options.nested_find(:topic,:priority))
      end
    end
  end
end