Class: XmlSitemap::Map

Inherits:
Object
  • Object
show all
Includes:
RenderEngine
Defined in:
lib/xml-sitemap/map.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(domain, opts = {}) {|_self| ... } ⇒ Map

Initializa a new Map instance

domain - Primary domain for the map (required) opts - Map options

opts - Automatic homepage creation. To disable set to false. (default: true) opts - Force HTTPS for all items. (default: false) opts - Set default lastmod timestamp for items (default: current time) opts - Group name for sitemap index. (default: sitemap) opts - Force all links to fall under the main domain.

You can add full urls (not paths) if set to false. (default: true)

Yields:

  • (_self)

Yield Parameters:

Raises:

  • (ArgumentError)

23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/xml-sitemap/map.rb', line 23

def initialize(domain, opts={})
  @domain     = domain.to_s.strip
  raise ArgumentError, 'Domain required!' if @domain.empty?

  @created_at = opts[:time]   || Time.now.utc
  @secure     = opts[:secure] || false
  @home       = opts.key?(:home) ? opts[:home] : true
  @root       = opts.key?(:root) ? opts[:root] : true
  @group      = opts[:group] || "sitemap"
  @items      = []

  self.add('/', :priority => 1.0) if @home === true

  yield self if block_given?
end

Instance Attribute Details

#bufferObject (readonly)

Returns the value of attribute buffer


6
7
8
# File 'lib/xml-sitemap/map.rb', line 6

def buffer
  @buffer
end

#created_atObject (readonly)

Returns the value of attribute created_at


7
8
9
# File 'lib/xml-sitemap/map.rb', line 7

def created_at
  @created_at
end

#domainObject (readonly)

Returns the value of attribute domain


5
6
7
# File 'lib/xml-sitemap/map.rb', line 5

def domain
  @domain
end

#groupObject (readonly)

Returns the value of attribute group


9
10
11
# File 'lib/xml-sitemap/map.rb', line 9

def group
  @group
end

#itemsObject (readonly)

Returns the value of attribute items


5
6
7
# File 'lib/xml-sitemap/map.rb', line 5

def items
  @items
end

#rootObject (readonly)

Returns the value of attribute root


8
9
10
# File 'lib/xml-sitemap/map.rb', line 8

def root
  @root
end

Instance Method Details

#add(target, opts = {}) ⇒ Object

Adds a new item to the map

target - Path or url opts - Item options

opts - Lastmod property of the item opts - Update frequency. opts - Item priority. opts - Skip time validation if want to insert raw strings.

Raises:

  • (RuntimeError)

49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/xml-sitemap/map.rb', line 49

def add(target, opts={})
  raise RuntimeError, 'Only up to 50k records allowed!' if @items.size > 50000
  raise ArgumentError, 'Target required!' if target.nil?
  raise ArgumentError, 'Target is empty!' if target.to_s.strip.empty?

  url = process_target(target)

  if url.length > 2048
    raise ArgumentError, "Target can't be longer than 2,048 characters!"
  end

  opts[:updated] = @created_at unless opts.key?(:updated)
  item = XmlSitemap::Item.new(url, opts)
  @items << item
  item
end

#empty?Boolean

Returns true if sitemap does not have any items


74
75
76
# File 'lib/xml-sitemap/map.rb', line 74

def empty?
  @items.empty?
end

#index_url(offset, secure) ⇒ Object

Get full url for index


86
87
88
# File 'lib/xml-sitemap/map.rb', line 86

def index_url(offset, secure)
  "#{secure ? 'https' : 'http'}://#{@domain}/#{@group}-#{offset}.xml"
end

#plain_index_url(secure) ⇒ Object


90
91
92
# File 'lib/xml-sitemap/map.rb', line 90

def plain_index_url(secure)
  "#{secure ? 'https' : 'http'}://#{@domain}/#{@group}.xml"
end

#render(method = :string) ⇒ Object

Render XML

method - Pick a render engine (:builder, :nokogiri, :string).

Default is :string

99
100
101
102
103
104
105
106
107
108
# File 'lib/xml-sitemap/map.rb', line 99

def render(method = :string)
  case method
    when :nokogiri
      render_nokogiri
    when :builder
      render_builder
    else
      render_string
  end
end

#render_to(path, options = {}) ⇒ Object

Render XML sitemap into the file

path - Output filename options - Options hash

options - Overwrite the file contents (default: true) options - Gzip file contents (default: false)


118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/xml-sitemap/map.rb', line 118

def render_to(path, options={})
  overwrite = options[:overwrite] == true || true
  compress  = options[:gzip]      == true || false

  path = File.expand_path(path)
  path << ".gz" unless path =~ /\.gz\z/i if compress

  if File.exists?(path) && !overwrite
    raise RuntimeError, "File already exists and not overwritable!"
  end

  File.open(path, 'wb') do |f|
    unless compress
      f.write(self.render)
    else
      gz = Zlib::GzipWriter.new(f)
      gz.write(self.render)
      gz.close
    end
  end
end

#sizeObject

Get map items count


68
69
70
# File 'lib/xml-sitemap/map.rb', line 68

def size
  @items.size
end

#url(path = '') ⇒ Object

Generate full url for path


80
81
82
# File 'lib/xml-sitemap/map.rb', line 80

def url(path='')
  "#{@secure ? 'https' : 'http'}://#{@domain}#{path}"
end