xml-sitemap 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README +37 -0
- data/lib/xml-sitemap.rb +58 -0
- metadata +68 -0
data/README
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
== About XmlSitemap
|
2
|
+
|
3
|
+
XmlSitemap is a gem to build website XML sitemaps.
|
4
|
+
|
5
|
+
== How To Use
|
6
|
+
|
7
|
+
require 'rubygems'
|
8
|
+
require 'xml-sitemap'
|
9
|
+
|
10
|
+
== Example
|
11
|
+
|
12
|
+
# Somewhere in controller
|
13
|
+
|
14
|
+
pages = Page.all(:order => [:updated_at.desc] # DM model
|
15
|
+
|
16
|
+
@map = XmlSitemap::Map.new('somedomain.com') do |m|
|
17
|
+
m.add(:url => '/', :period => :daily, :priority => 1.0)
|
18
|
+
m.add(:url => '/contractors', :period => :daily, :priority => 1.0)
|
19
|
+
pages.each do |p|
|
20
|
+
m.add(
|
21
|
+
:url => url_for_page(p),
|
22
|
+
:updated => p.updated_at,
|
23
|
+
:priority => 0.5,
|
24
|
+
:period => :never
|
25
|
+
)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
# ... then render it in your view
|
30
|
+
<%= @map.render %>
|
31
|
+
|
32
|
+
== Options
|
33
|
+
|
34
|
+
:url - page path, relative to domain (ex.: /test), String.
|
35
|
+
:period - freqchange attribute, Symbol, :none, :never, :always, :hourly, :daily, :weekly, :monthly, :yearly
|
36
|
+
:priority - priority attribute, Float class,(0.0..1.0)
|
37
|
+
:updated - (optional) last_update attribute, Time class
|
data/lib/xml-sitemap.rb
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
module XmlSitemap
|
2
|
+
PERIODS = [:none, :always, :hourly, :daily, :weekly, :monthly, :yearly, :never]
|
3
|
+
|
4
|
+
class Item
|
5
|
+
attr_reader :path
|
6
|
+
attr_reader :updated
|
7
|
+
attr_reader :priority
|
8
|
+
attr_reader :changefreq
|
9
|
+
|
10
|
+
def initialize(opts={})
|
11
|
+
@path = opts[:url] if opts.key?(:url)
|
12
|
+
@updated = opts[:updated] || Time.now
|
13
|
+
@priority = opts[:priority] || 0.5
|
14
|
+
@changefreq = opts[:period] || :never
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
class Map
|
19
|
+
attr_reader :domain, :items
|
20
|
+
attr_reader :buffer
|
21
|
+
|
22
|
+
# Creates new Map class for specified domain
|
23
|
+
def initialize(domain)
|
24
|
+
@domain = domain
|
25
|
+
@items = []
|
26
|
+
yield self if block_given?
|
27
|
+
end
|
28
|
+
|
29
|
+
# Yields Map class for easier access
|
30
|
+
def generate
|
31
|
+
raise ArgumentError, 'Block required' unless block_given?
|
32
|
+
yield self
|
33
|
+
end
|
34
|
+
|
35
|
+
# Add new item to sitemap list
|
36
|
+
def add(opts)
|
37
|
+
@items << XmlSitemap::Item.new(opts)
|
38
|
+
end
|
39
|
+
|
40
|
+
# Render XML
|
41
|
+
def render
|
42
|
+
return '' if @items.size == 0
|
43
|
+
output = []
|
44
|
+
output << '<?xml version="1.0" encoding="UTF-8"?>'
|
45
|
+
output << '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'
|
46
|
+
@items.each do |item|
|
47
|
+
output << '<url>'
|
48
|
+
output << "<loc>http://#{@domain}#{item.path.to_s}</loc>"
|
49
|
+
output << "<lastmod>#{item.updated.to_date}</lastmod>"
|
50
|
+
output << "<changefreq>#{item.changefreq.to_s}</changefreq>"
|
51
|
+
output << "<priority>#{item.priority.to_s}</priority>"
|
52
|
+
output << '</url>'
|
53
|
+
end
|
54
|
+
output << '</urlset>'
|
55
|
+
return output.join("\n")
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
metadata
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: xml-sitemap
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 21
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 1.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Dan Sosedoff
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-06-18 00:00:00 -05:00
|
19
|
+
default_executable:
|
20
|
+
dependencies: []
|
21
|
+
|
22
|
+
description: Easy XML sitemap generation for Ruby/Rails application
|
23
|
+
email: dan.sosedoff@gmail.com
|
24
|
+
executables: []
|
25
|
+
|
26
|
+
extensions: []
|
27
|
+
|
28
|
+
extra_rdoc_files: []
|
29
|
+
|
30
|
+
files:
|
31
|
+
- README
|
32
|
+
- lib/xml-sitemap.rb
|
33
|
+
has_rdoc: true
|
34
|
+
homepage: http://sosedoff.com/
|
35
|
+
licenses: []
|
36
|
+
|
37
|
+
post_install_message:
|
38
|
+
rdoc_options: []
|
39
|
+
|
40
|
+
require_paths:
|
41
|
+
- lib
|
42
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
43
|
+
none: false
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
hash: 3
|
48
|
+
segments:
|
49
|
+
- 0
|
50
|
+
version: "0"
|
51
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
hash: 3
|
57
|
+
segments:
|
58
|
+
- 0
|
59
|
+
version: "0"
|
60
|
+
requirements: []
|
61
|
+
|
62
|
+
rubyforge_project:
|
63
|
+
rubygems_version: 1.3.7
|
64
|
+
signing_key:
|
65
|
+
specification_version: 3
|
66
|
+
summary: No-dependency XML sitemap generator. Can be used for any Ruby application (Rails/Merb/Sinatra).
|
67
|
+
test_files: []
|
68
|
+
|