Class: XmlSitemap::Map
- Inherits:
-
Object
- Object
- XmlSitemap::Map
- Includes:
- RenderEngine
- Defined in:
- lib/xml-sitemap/map.rb
Instance Attribute Summary collapse
-
#buffer ⇒ Object
readonly
Returns the value of attribute buffer.
-
#created_at ⇒ Object
readonly
Returns the value of attribute created_at.
-
#domain ⇒ Object
readonly
Returns the value of attribute domain.
-
#group ⇒ Object
readonly
Returns the value of attribute group.
-
#items ⇒ Object
readonly
Returns the value of attribute items.
-
#root ⇒ Object
readonly
Returns the value of attribute root.
Instance Method Summary collapse
-
#add(target, opts = {}) ⇒ Object
Adds a new item to the map.
-
#empty? ⇒ Boolean
Returns true if sitemap does not have any items.
-
#index_url(offset, secure) ⇒ Object
Get full url for index.
-
#initialize(domain, opts = {}) {|_self| ... } ⇒ Map
constructor
Initializa a new Map instance.
- #plain_index_url(secure) ⇒ Object
-
#render(method = :string) ⇒ Object
Render XML.
-
#render_to(path, options = {}) ⇒ Object
Render XML sitemap into the file.
-
#size ⇒ Object
Get map items count.
-
#url(path = '') ⇒ Object
Generate full url for path.
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)
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
#buffer ⇒ Object (readonly)
Returns the value of attribute buffer
6 7 8 |
# File 'lib/xml-sitemap/map.rb', line 6 def buffer @buffer end |
#created_at ⇒ Object (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 |
#domain ⇒ Object (readonly)
Returns the value of attribute domain
5 6 7 |
# File 'lib/xml-sitemap/map.rb', line 5 def domain @domain end |
#group ⇒ Object (readonly)
Returns the value of attribute group
9 10 11 |
# File 'lib/xml-sitemap/map.rb', line 9 def group @group end |
#items ⇒ Object (readonly)
Returns the value of attribute items
5 6 7 |
# File 'lib/xml-sitemap/map.rb', line 5 def items @items end |
#root ⇒ Object (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
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
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, ={}) overwrite = [:overwrite] == true || true compress = [:gzip] == true || false path = File.(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 |
#size ⇒ Object
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 |