### Install and Set Up Sitemap Generator Source: https://github.com/kjvarga/sitemap_generator/blob/master/_autodocs/00_START_HERE.md Install the sitemap_generator gem and perform the initial setup for your project. This includes adding the gem, installing it, and configuring the sitemap file. ```bash bundle add sitemap_generator bundle exec rake sitemap:install # Edit config/sitemap.rb rake sitemap:refresh ``` -------------------------------- ### SimpleNamer Initialization Examples Source: https://github.com/kjvarga/sitemap_generator/blob/master/_autodocs/types.md Demonstrates two ways to initialize the SimpleNamer class: one with default-like options for sequential numbered files and another with custom options for a different file extension, starting number, and zero value. ```ruby # Default behavior: sitemap.xml.gz, sitemap1.xml.gz, sitemap2.xml.gz, ... namer = SitemapGenerator::SimpleNamer.new(:sitemap, start: 2, zero: 1) ``` ```ruby # Custom behavior: products.xml, products_1.xml, products_2.xml, ... namer = SitemapGenerator::SimpleNamer.new(:products, extension: '.xml', zero: '', start: 1) ``` -------------------------------- ### Install and Configure Source: https://github.com/kjvarga/sitemap_generator/blob/master/_autodocs/QUICK_START.md Install the gem using Bundler and run the install rake task to create the configuration file. ```bash bundle install bundle exec rake sitemap:install ``` -------------------------------- ### SimpleNamer Initialization Examples Source: https://github.com/kjvarga/sitemap_generator/blob/master/_autodocs/api-reference/simple_namer.md Demonstrates various ways to initialize SimpleNamer with default and custom options for filename generation. ```ruby # Default behavior namer = SitemapGenerator::SimpleNamer.new(:sitemap) # Generates: sitemap.xml.gz, sitemap1.xml.gz, sitemap2.xml.gz, ... ``` ```ruby # Custom start number namer = SitemapGenerator::SimpleNamer.new(:sitemap, start: 2, zero: 1) # Generates: sitemap1.xml.gz, sitemap2.xml.gz, sitemap3.xml.gz, ... ``` ```ruby # Custom extension namer = SitemapGenerator::SimpleNamer.new(:sitemap, extension: '.xml') # Generates: sitemap.xml, sitemap1.xml, sitemap2.xml, ... ``` ```ruby # Custom zero value namer = SitemapGenerator::SimpleNamer.new(:products, zero: '_index', extension: '.xml') # Generates: products_index.xml, products1.xml, products2.xml, ... ``` -------------------------------- ### Initialize S3Adapter with Credentials Source: https://github.com/kjvarga/sitemap_generator/blob/master/_autodocs/api-reference/adapters.md Instantiate the S3Adapter by providing AWS credentials and S3 bucket configuration directly. Ensure 'fog-aws' is installed. ```ruby adapter = SitemapGenerator::S3Adapter.new( aws_access_key_id: 'YOUR_KEY', aws_secret_access_key: 'YOUR_SECRET', fog_provider: 'AWS', fog_directory: 'my-bucket', fog_region: 'us-west-2' ) SitemapGenerator::Sitemap.adapter = adapter ``` -------------------------------- ### Capistrano Deployment Example Source: https://github.com/kjvarga/sitemap_generator/blob/master/_autodocs/configuration.md Example of how Capistrano automatically runs sitemap generation during deployment after integration. ```bash cap production deploy # Automatically runs: bundle exec rake sitemap:refresh ``` -------------------------------- ### Video Hash Example Source: https://github.com/kjvarga/sitemap_generator/blob/master/_autodocs/types.md This example demonstrates how to structure a Video Hash object with common fields. Ensure all required fields like thumbnail_loc, title, and description are provided. ```ruby { thumbnail_loc: 'https://example.com/thumb.jpg', title: 'Product Demo', description: 'A demonstration of our product', content_loc: 'https://example.com/video.mp4', duration: 600, publication_date: Date.today, rating: 4.5, view_count: 10000, family_friendly: true } ``` -------------------------------- ### Install Sitemap Generator Source: https://github.com/kjvarga/sitemap_generator/blob/master/_autodocs/README.md Use this Rake task to create the initial configuration file for sitemap generation. ```bash rake sitemap:install ``` -------------------------------- ### Advance Namer with Customization Source: https://github.com/kjvarga/sitemap_generator/blob/master/_autodocs/api-reference/simple_namer.md Demonstrates advancing the namer using `next` with custom `start` and `zero` options. The `zero` option defines the initial filename, and `start` defines the first incremented filename. ```ruby namer = SitemapGenerator::SimpleNamer.new(:sitemap, start: 1, zero: 'nil_marker') puts namer.to_s # => "sitemap.xml.gz" (zero) namer.next puts namer.to_s # => "sitemap1.xml.gz" (start) namer.next puts namer.to_s # => "sitemap2.xml.gz" # Chaining: puts namer.next.next.to_s # => "sitemap4.xml.gz" ``` -------------------------------- ### Interpreter Initialization and Configuration Source: https://github.com/kjvarga/sitemap_generator/blob/master/_autodocs/api-reference/interpreter.md Creates a new Interpreter instance and immediately evaluates a configuration block. This example sets a default host and adds two pages with specified priorities. ```ruby interpreter = SitemapGenerator::Interpreter.new( default_host: 'https://example.com' ) do add '/home', priority: 1.0 add '/about', priority: 0.8 end ``` -------------------------------- ### Install and Refresh Sitemaps in Rails Source: https://github.com/kjvarga/sitemap_generator/blob/master/_autodocs/configuration.md Commands to install the default configuration file and generate/ping sitemaps within a Rails application. ```bash bundle exec rake sitemap:install # Create config/sitemap.rb bundle exec rake sitemap:refresh # Generate and ping ``` -------------------------------- ### Install SitemapGenerator Gem Source: https://github.com/kjvarga/sitemap_generator/blob/master/README.md Install the gem using the standard Ruby package manager. ```ruby gem install 'sitemap_generator' ``` -------------------------------- ### Numbered from Start with No Zero File Source: https://github.com/kjvarga/sitemap_generator/blob/master/_autodocs/api-reference/simple_namer.md Initializes a namer where numbering starts from 1, and no 'zero' file (e.g., sitemap.xml.gz) is generated. The first file will be sitemap1. ```ruby namer = SitemapGenerator::SimpleNamer.new(:sitemap, zero: '', start: 1) # Generates: # sitemap1.xml.gz # sitemap2.xml.gz # sitemap3.xml.gz ``` -------------------------------- ### Check if at Start Position Source: https://github.com/kjvarga/sitemap_generator/blob/master/_autodocs/api-reference/simple_namer.md Illustrates the use of the `start?` method to determine if the namer is currently at the beginning of the sequence. It returns `true` when at the start and `false` otherwise. ```ruby namer = SitemapGenerator::SimpleNamer.new(:sitemap) puts namer.start? # => true namer.next puts namer.start? # => false namer.reset puts namer.start? # => true ``` -------------------------------- ### Initialize FogAdapter for Cloud Storage Source: https://github.com/kjvarga/sitemap_generator/blob/master/_autodocs/api-reference/adapters.md Configure and initialize the FogAdapter with your cloud storage credentials. Ensure you have the 'fog-core' gem and the specific provider gem (e.g., 'fog-aws') installed. ```ruby adapter = SitemapGenerator::FogAdapter.new( provider: 'AWS', aws_access_key_id: 'KEY', aws_secret_access_key: 'SECRET', region: 'us-west-2' ) SitemapGenerator::Sitemap.adapter = adapter ``` -------------------------------- ### Gem Installation Paths Source: https://github.com/kjvarga/sitemap_generator/blob/master/_autodocs/README.md Displays the default installation paths for the sitemap_generator gem, including its root directory, template files, and code locations. ```text Gem root: sitemap_generator/ Config template: templates/sitemap.rb Main code: lib/sitemap_generator/ Adapters: lib/sitemap_generator/adapters/ Rake tasks: lib/sitemap_generator/tasks.rb ``` -------------------------------- ### SimpleNamer Usage Example Source: https://github.com/kjvarga/sitemap_generator/blob/master/_autodocs/api-reference/simple_namer.md Demonstrates how to initialize and use SimpleNamer to generate sequential filenames for sitemaps and indexes. The `next` method must be called to advance the namer for subsequent filenames. ```ruby namer = SitemapGenerator::SimpleNamer.new(:sitemap, zero: 1, start: 2) # First sitemap gets the first name: location1 = SitemapGenerator::SitemapLocation.new( namer: namer, ... ) puts location1.filename # => "sitemap1.xml.gz" namer.next # Advance namer # Second sitemap gets the next name: location2 = SitemapGenerator::SitemapLocation.new( namer: namer, ... ) puts location2.filename # => "sitemap2.xml.gz" namer.next # Advance namer # Index gets a different name (from a different namer): index_namer = SitemapGenerator::SimpleNamer.new(:sitemap) # Fresh namer puts index_namer.to_s # => "sitemap.xml.gz" ``` -------------------------------- ### PageMap Data Object Example Source: https://github.com/kjvarga/sitemap_generator/blob/master/_autodocs/types.md An example illustrating how to structure custom metadata for a document, including its type, ID, and specific attributes like author, publish date, and language. ```ruby { dataobjects: [ { type: 'document', id: 'doc-123', attributes: [ { name: 'author', value: 'John Doe' }, { name: 'publish_date', value: '2025-06-23' }, { name: 'language', value: 'en' } ] } ] } ``` -------------------------------- ### Initialize S3Adapter with Environment Variables Source: https://github.com/kjvarga/sitemap_generator/blob/master/_autodocs/api-reference/adapters.md Instantiate the S3Adapter using environment variables for AWS credentials and S3 bucket configuration. Ensure 'fog-aws' is installed and the required environment variables are set. ```ruby # Set these environment variables: # AWS_ACCESS_KEY_ID=your_key # AWS_SECRET_ACCESS_KEY=your_secret # FOG_PROVIDER=AWS # FOG_DIRECTORY=my-bucket # FOG_REGION=us-west-2 adapter = SitemapGenerator::S3Adapter.new SitemapGenerator::Sitemap.adapter = adapter ``` -------------------------------- ### Install SitemapGenerator Gem Source: https://github.com/kjvarga/sitemap_generator/blob/master/README.md Install the sitemap_generator gem using the RubyGems package manager. This command is typically run before configuring or using the gem. ```bash gem install sitemap_generator ``` -------------------------------- ### Create Sitemap Index with Multiple Entries Source: https://github.com/kjvarga/sitemap_generator/blob/master/README.md This example demonstrates creating a sitemap index by adding multiple sitemap files using `add_to_index`. An index is always created unless `create_index` is set to `false`. ```ruby SitemapGenerator::Sitemap.default_host = "http://www.example.com" SitemapGenerator::Sitemap.create do add_to_index '/mysitemap1.xml.gz' add_to_index '/mysitemap2.xml.gz' # ... end ``` -------------------------------- ### Reset Namer to Start Source: https://github.com/kjvarga/sitemap_generator/blob/master/_autodocs/api-reference/simple_namer.md Shows how to reset the namer to its initial state using the `reset` method. This is useful after advancing the sequence multiple times. ```ruby namer = SitemapGenerator::SimpleNamer.new(:sitemap) namer.next.next.next puts namer.to_s # => "sitemap3.xml.gz" namer.reset puts namer.to_s # => "sitemap.xml.gz" ``` -------------------------------- ### Schedule Sitemap Generation with Cron Source: https://github.com/kjvarga/sitemap_generator/blob/master/_autodocs/QUICK_START.md This example shows how to schedule daily sitemap generation using cron. Ensure you are in the correct directory and use `bundle exec` to run the rake task. ```bash # crontab -e 0 2 * * * cd /var/www/myapp && bundle exec rake sitemap:refresh ``` -------------------------------- ### Run Interpreter with Default Config Source: https://github.com/kjvarga/sitemap_generator/blob/master/_autodocs/api-reference/interpreter.md Loads and evaluates the default sitemap configuration file located at `config/sitemap.rb`. This is the simplest way to start generating sitemaps. ```ruby SitemapGenerator::Interpreter.run ``` -------------------------------- ### Configure Sitemap Generator with Options Source: https://github.com/kjvarga/sitemap_generator/blob/master/_autodocs/QUICK_START.md Configure the sitemap generator with various options such as default host, public path, sitemaps path, compression, and verbosity. This example shows how to set these options during the `create` call. ```ruby SitemapGenerator::Sitemap.create( default_host: 'https://example.com', public_path: 'public/', sitemaps_path: 'sitemaps/', compress: true, verbose: true ) do add '/', priority: 1.0 Product.find_each { |p| add product_path(p) } end ``` -------------------------------- ### Write Sitemap with FileAdapter Source: https://github.com/kjvarga/sitemap_generator/blob/master/_autodocs/api-reference/adapters.md Example of writing sitemap data using the FileAdapter. This includes creating a SitemapLocation object and writing compressed XML data. Parent directories are created automatically, and gzip compression is applied if the filename ends with `.gz`. ```ruby adapter = SitemapGenerator::FileAdapter.new location = SitemapGenerator::SitemapLocation.new( host: 'https://example.com', public_path: 'public/', filename: 'sitemap.xml.gz' ) adapter.write(location, '...') # Creates file: public/sitemap.xml.gz (compressed) ``` -------------------------------- ### Navigate Previous Filename with Customization Source: https://github.com/kjvarga/sitemap_generator/blob/master/_autodocs/api-reference/simple_namer.md Shows how to use the `previous` method to go back in the filename sequence. It handles custom `start` and `zero` values and raises a `NameError` if attempting to go before the first name. ```ruby namer = SitemapGenerator::SimpleNamer.new(:sitemap, start: 2, zero: 1) namer.next.next # => sitemap3.xml.gz namer.previous puts namer.to_s # => "sitemap2.xml.gz" namer.previous puts namer.to_s # => "sitemap1.xml.gz" (back to zero value) namer.previous # => NameError: Already at the start of the series ``` -------------------------------- ### Get Full Sitemap URL Source: https://github.com/kjvarga/sitemap_generator/blob/master/_autodocs/api-reference/sitemap_location.md Returns the complete URL for accessing the sitemap file. Requires host to be set during initialization. ```ruby location.url # => "https://example.com/sitemaps/sitemap.xml.gz" ``` -------------------------------- ### Add Dynamic Content from Database Source: https://github.com/kjvarga/sitemap_generator/blob/master/_autodocs/00_START_HERE.md Integrate dynamic content from your database into the sitemap. This example iterates over products to add their paths and last modification times. ```ruby SitemapGenerator::Sitemap.create do add '/', priority: 1.0 Product.find_each do |product| add product_path(product), lastmod: product.updated_at, changefreq: 'weekly' end end ``` -------------------------------- ### Access LinkSet Instance and Create Sitemaps Source: https://github.com/kjvarga/sitemap_generator/blob/master/README.md Use `SitemapGenerator::Sitemap.create` with a block to access the LinkSet instance. Configure the namer to start sitemap numbering from a specific value. Add existing sitemap index files before generating new ones. ```ruby SitemapGenerator::Sitemap.default_host = "http://www.example.com" SitemapGenerator::Sitemap.namer = SitemapGenerator::SimpleNamer.new(:sitemap, :start => 4) SitemapGenerator::Sitemap.create do (1..3).each do |i| add_to_index "sitemap#{i}.xml.gz" end add '/home' add '/another' end ``` -------------------------------- ### Custom Configuration per Environment Source: https://github.com/kjvarga/sitemap_generator/blob/master/_autodocs/QUICK_START.md Configure sitemap generation differently based on the Rails environment. This example sets the default host using an environment variable and chooses the adapter (S3 or File) accordingly. ```ruby # config/sitemap.rb SitemapGenerator::Sitemap.default_host = "https://#{ENV['DOMAIN'] || 'example.com'}" # Set adapter based on environment if Rails.env.production? SitemapGenerator::Sitemap.adapter = SitemapGenerator::AwsSdkAdapter.new(ENV['AWS_BUCKET']) else SitemapGenerator::Sitemap.adapter = SitemapGenerator::FileAdapter.new end SitemapGenerator::Sitemap.create do add '/', priority: 1.0 Product.find_each { |p| add product_path(p) } end ``` ```bash DOMAIN=staging.example.com rake sitemap:refresh ``` -------------------------------- ### Typical Sitemap Generation with Rails Source: https://github.com/kjvarga/sitemap_generator/blob/master/_autodocs/api-reference/interpreter.md Configure sitemap content within a Rails application using `config/sitemap.rb`. This example shows setting the default host, adding static pages, and dynamically generating paths using Rails URL helpers and groups. ```ruby SitemapGenerator::Sitemap.default_host = 'https://example.com' SitemapGenerator::Sitemap.create do add '/', changefreq: 'daily', priority: 1.0 add '/about', changefreq: 'monthly' add '/contact', changefreq: 'yearly' # With Rails URL helpers: Product.find_each do |product| add product_path(product), lastmod: product.updated_at, changefreq: 'weekly' end # Using groups: group(filename: :news, sitemaps_path: 'news/') do NewsArticle.find_each do |article| add article_path(article), priority: 0.8, lastmod: article.published_at end end end ``` -------------------------------- ### Initialize LinkSet with Options Source: https://github.com/kjvarga/sitemap_generator/blob/master/_autodocs/api-reference/link_set.md Creates a new LinkSet instance, configuring output paths, host, compression, and verbosity. ```ruby link_set = SitemapGenerator::LinkSet.new( default_host: 'https://example.com', public_path: 'public/', sitemaps_path: 'sitemaps/', compress: true, verbose: true ) ``` -------------------------------- ### Basic Sitemap Generator Configuration with Fog Source: https://github.com/kjvarga/sitemap_generator/wiki/Generate-Sitemaps-on-read-only-filesystems-like-Heroku Configure SitemapGenerator to use the S3Adapter with Fog for storing sitemaps. This setup specifies the default host, a temporary public path, the S3 adapter, the remote sitemaps host, and a path within the S3 bucket. ```ruby # Set the host name for URL creation SitemapGenerator::Sitemap.default_host = "http://example.com" # pick a place safe to write the files SitemapGenerator::Sitemap.public_path = 'tmp/' # store on S3 using Fog (pass in configuration values as shown above if needed) SitemapGenerator::Sitemap.adapter = SitemapGenerator::S3Adapter.new # inform the map cross-linking where to find the other maps SitemapGenerator::Sitemap.sitemaps_host = "http://#{ENV['FOG_DIRECTORY']}.s3.amazonaws.com/" # pick a namespace within your bucket to organize your maps SitemapGenerator::Sitemap.sitemaps_path = 'sitemaps/' ``` -------------------------------- ### Initialize SitemapFile Source: https://github.com/kjvarga/sitemap_generator/blob/master/_autodocs/api-reference/sitemap_file.md Creates a new SitemapFile instance with a specified location. This instance is not yet written to disk. ```ruby location = SitemapGenerator::SitemapLocation.new( host: 'https://example.com', public_path: 'public/', sitemaps_path: 'sitemaps/', filename: 'sitemap.xml.gz' ) sitemap = SitemapGenerator::Builder::SitemapFile.new(location: location) ``` -------------------------------- ### Initialize SitemapLocation Source: https://github.com/kjvarga/sitemap_generator/blob/master/_autodocs/api-reference/sitemap_location.md Creates a new SitemapLocation instance with specified configuration options. Ensure required options like 'host' are provided. ```ruby location = SitemapGenerator::SitemapLocation.new( host: 'https://example.com', public_path: 'public/', sitemaps_path: 'sitemaps/', adapter: SitemapGenerator::FileAdapter.new, namer: SitemapGenerator::SimpleNamer.new(:sitemap), verbose: true ) ``` -------------------------------- ### Custom Start Position with Zero as Number Source: https://github.com/kjvarga/sitemap_generator/blob/master/_autodocs/api-reference/simple_namer.md Sets the starting number to 1 and uses '0' as the base name for the first file, resulting in 'sitemap0.xml.gz'. ```ruby namer = SitemapGenerator::SimpleNamer.new(:sitemap, zero: 0, start: 1) # Generates: # sitemap0.xml.gz # sitemap1.xml.gz # sitemap2.xml.gz ``` -------------------------------- ### Create a New SitemapUrl Instance Source: https://github.com/kjvarga/sitemap_generator/blob/master/_autodocs/api-reference/sitemap_url.md Instantiates a new SitemapUrl object with a given path and metadata options. Ensure the 'host' option is provided. ```ruby url = SitemapGenerator::Builder::SitemapUrl.new( '/products/widget', host: 'https://example.com', priority: 0.9, changefreq: 'daily', lastmod: Date.today ) ``` -------------------------------- ### Initialize WaveAdapter Source: https://github.com/kjvarga/sitemap_generator/blob/master/_autodocs/api-reference/adapters.md Initializes the adapter for uploading to a remote server using Wave or generic HTTP POST. Requires service name, API key, and host. ```ruby adapter = SitemapGenerator::WaveAdapter.new( service_name: 'my-service', api_key: 'secret-key', host: 'https://wave.example.com' ) SitemapGenerator::Sitemap.adapter = adapter ``` -------------------------------- ### SitemapGenerator::Builder::SitemapFile#new Source: https://github.com/kjvarga/sitemap_generator/blob/master/_autodocs/api-reference/sitemap_file.md Initializes a new SitemapFile instance. This object accumulates URL entries, converts them to XML, tracks file metadata, and prepares the sitemap for writing to disk. It should rarely be instantiated directly by users. ```APIDOC ## new(opts = {}) ### Description Creates a new SitemapFile instance. This object is responsible for accumulating URL entries, converting them to XML, tracking file metadata (link count, file size), and preparing the sitemap for writing to disk. It is typically created and managed by LinkSet and should rarely be instantiated directly. ### Method `new` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters - **opts** (Hash) - Configuration options. - **opts (location key)** (SitemapLocation/Hash) - A SitemapLocation instance or Hash of options to create one. See SitemapLocation documentation. ### Return Value A new SitemapFile instance (not yet written to disk). ### Exceptions None on initialization; exceptions raised during use. ### Example ```ruby location = SitemapGenerator::SitemapLocation.new( host: 'https://example.com', public_path: 'public/', sitemaps_path: 'sitemaps/', filename: 'sitemap.xml.gz' ) sitemap = SitemapGenerator::Builder::SitemapFile.new(location: location) ``` ``` -------------------------------- ### FileAdapter.new Source: https://github.com/kjvarga/sitemap_generator/blob/master/_autodocs/api-reference/adapters.md Initializes a new FileAdapter instance. This is the default adapter and writes sitemap files to the local filesystem. ```APIDOC ## FileAdapter.new ### Description Initializes a new FileAdapter instance. This is the default adapter and writes sitemap files to the local filesystem. ### Constructor ```ruby FileAdapter.new ``` No constructor arguments. ### Example ```ruby SitemapGenerator::Sitemap.adapter = SitemapGenerator::FileAdapter.new ``` ``` -------------------------------- ### Create a Basic Sitemap Source: https://github.com/kjvarga/sitemap_generator/blob/master/_autodocs/README.md Use the `SitemapGenerator::Sitemap.create` block to define your sitemap. Set the default host and add links with optional priorities. This is the entry point for generating sitemaps. ```ruby SitemapGenerator::Sitemap.default_host = 'https://example.com' SitemapGenerator::Sitemap.create do add '/', priority: 1.0 add '/products', priority: 0.8 Product.find_each { |p| add product_path(p) } end ``` -------------------------------- ### Image Hash Example Source: https://github.com/kjvarga/sitemap_generator/blob/master/_autodocs/types.md Provides an example of an Image Hash structure used for associating image metadata with a sitemap URL. This includes fields like location, caption, title, geographic location, and license. ```ruby { loc: 'https://example.com/images/product.jpg', caption: 'Product photo', title: 'Product', geo_location: 'San Francisco, CA', license: 'https://creativecommons.org/licenses/by/4.0/' } ``` -------------------------------- ### Example of SitemapFinalizedError Source: https://github.com/kjvarga/sitemap_generator/blob/master/_autodocs/errors.md Demonstrates raising SitemapFinalizedError when attempting to add a URL after finalization. ```ruby sitemap = SitemapGenerator::Builder::SitemapFile.new(location: loc) sitemap.add '/page1', host: 'https://example.com' sitemap.finalize! # This raises SitemapFinalizedError: sitemap.add '/page2', host: 'https://example.com' # => SitemapFinalizedError ``` -------------------------------- ### Get Link Count Source: https://github.com/kjvarga/sitemap_generator/blob/master/_autodocs/api-reference/link_set.md Retrieves the total number of links across all sitemaps in the index. ```ruby puts link_set.link_count # => 42500 ``` -------------------------------- ### Instantiating SitemapIndexLocation Source: https://github.com/kjvarga/sitemap_generator/blob/master/_autodocs/api-reference/sitemap_location.md Shows how to create an instance of SitemapIndexLocation with specific options, typically used internally by LinkSet for managing index files. ```ruby index_location = SitemapGenerator::SitemapIndexLocation.new( host: 'https://example.com', public_path: 'public/', create_index: :auto ) ``` -------------------------------- ### Handle Missing Gem Dependency with LoadError Source: https://github.com/kjvarga/sitemap_generator/blob/master/_autodocs/errors.md Illustrates LoadError when a required gem dependency for an adapter is not installed. ```ruby # Without fog-aws installed: SitemapGenerator::S3Adapter.new(...) # => LoadError: Error: `Fog::Storage` is not defined. # Please `require 'fog-aws'` - or another library that defines this class. ``` -------------------------------- ### Get Sitemap Directory Path Source: https://github.com/kjvarga/sitemap_generator/blob/master/_autodocs/api-reference/sitemap_location.md Returns the directory path where the sitemap file is located, excluding the filename. ```ruby location.directory # => "/home/user/myapp/public/sitemaps/" ``` -------------------------------- ### Create Sitemaps with LinkSet Source: https://github.com/kjvarga/sitemap_generator/blob/master/_autodocs/api-reference/link_set.md Use the `create` method to initialize a LinkSet and populate it with links using a block. This is the main entry point for sitemap generation. Options like `default_host` and `compress` can be set here. ```ruby SitemapGenerator::Sitemap.create( default_host: 'https://example.com', compress: true ) do add '/', changefreq: 'daily', priority: 1.0 add '/about', changefreq: 'monthly' add '/contact', changefreq: 'yearly' end ``` -------------------------------- ### start? Source: https://github.com/kjvarga/sitemap_generator/blob/master/_autodocs/api-reference/simple_namer.md Checks if the namer is currently at the first name in the sequence. Returns a boolean value indicating the current state. ```APIDOC ## start? ### Description Returns whether the namer is at the first name in the sequence. ### Return Value Boolean. ### Example ```ruby namer = SitemapGenerator::SimpleNamer.new(:sitemap) puts namer.start? # => true namer.next puts namer.start? # => false namer.reset puts namer.start? # => true ``` ``` -------------------------------- ### SitemapLocation Path Construction with Default Configuration Source: https://github.com/kjvarga/sitemap_generator/blob/master/_autodocs/api-reference/sitemap_location.md Demonstrates the default path construction for a SitemapLocation object, showing the resulting full path, path within the public directory, URL, and directory. ```ruby location = SitemapGenerator::SitemapLocation.new( host: 'https://example.com', public_path: '/var/www/app/public', sitemaps_path: 'sitemaps/', filename: 'sitemap.xml.gz' ) location.path # => "/var/www/app/public/sitemaps/sitemap.xml.gz" location.path_in_public # => "sitemaps/sitemap.xml.gz" location.url # => "https://example.com/sitemaps/sitemap.xml.gz" location.directory # => "/var/www/app/public/sitemaps/" ``` -------------------------------- ### Get Sitemap Filesize Source: https://github.com/kjvarga/sitemap_generator/blob/master/_autodocs/api-reference/sitemap_location.md Returns the size of the written sitemap file in bytes. Returns nil if the file does not exist. ```ruby size = location.filesize # => 5120 puts "#{size / 1024} KB" ``` -------------------------------- ### Get Maximum Sitemap Links Source: https://github.com/kjvarga/sitemap_generator/blob/master/_autodocs/api-reference/sitemap_file.md Returns the maximum number of links allowed in this sitemap, as defined by the location or the default value. ```ruby limit = sitemap.max_sitemap_links # => 50000 ``` -------------------------------- ### Configure GoogleStorageAdapter Source: https://github.com/kjvarga/sitemap_generator/blob/master/_autodocs/configuration.md Set up the GoogleStorageAdapter for uploading sitemaps to Google Cloud Storage. Provide the bucket name and Google Cloud project ID. ```ruby adapter = SitemapGenerator::GoogleStorageAdapter.new( 'my-bucket', project_id: 'my-gcp-project' ) SitemapGenerator::Sitemap.adapter = adapter ``` -------------------------------- ### new Source: https://github.com/kjvarga/sitemap_generator/blob/master/_autodocs/api-reference/sitemap_file.md Creates and returns a new SitemapFile instance. This new instance shares the same location options as the current sitemap but will have a new sequential filename generated by the namer. ```APIDOC ## new ### Description Creates a new SitemapFile instance with the same location options but a new sequential filename from the namer. ### Return Value A new SitemapFile instance. ### Example ```ruby first_sitemap = SitemapGenerator::Builder::SitemapFile.new(location) first_sitemap.add('/page1', host: 'https://example.com') second_sitemap = first_sitemap.new second_sitemap.add('/page2', host: 'https://example.com') ``` ``` -------------------------------- ### Initialize AwsSdkAdapter with Options Source: https://github.com/kjvarga/sitemap_generator/blob/master/_autodocs/api-reference/adapters.md Initialize the adapter with S3 bucket name and AWS configuration using options. This is the preferred method for setting region, access keys, and ACL. ```ruby adapter = SitemapGenerator::AwsSdkAdapter.new( 'my-bucket', region: 'us-west-2', access_key_id: ENV['AWS_KEY'], secret_access_key: ENV['AWS_SECRET'], acl: 'public-read' ) ``` -------------------------------- ### Standalone Ruby Sitemap Generation Source: https://github.com/kjvarga/sitemap_generator/blob/master/_autodocs/README.md Use this configuration for generating sitemaps in a standalone Ruby script. Ensure the 'sitemap_generator' gem is installed. ```ruby require 'sitemap_generator' SitemapGenerator::Sitemap.default_host = 'https://example.com' SitemapGenerator::Sitemap.create do add '/' add '/about' end ``` -------------------------------- ### SimpleNamer Class Source: https://github.com/kjvarga/sitemap_generator/blob/master/_autodocs/types.md The SimpleNamer class generates sequential sitemap filenames with customizable options for extensions, starting numbers, and initial values. ```APIDOC ## SimpleNamer Generates sequential sitemap filenames. **Definition:** `lib/sitemap_generator/simple_namer.rb` ```ruby class SimpleNamer def initialize(base, options = {}) # base: :sitemap or 'sitemap' (becomes the base of filenames) # options: { extension: '.xml.gz', start: 1, zero: nil } end def to_s # Current filename as string def reset # Reset to first name def start? # True if at first name def next # Advance to next name and return self def previous # Go to previous name and return self end ``` ### Initialization Options | Option | Type | Default | Description | |--------|------|---------|-------------| | extension | String | '.xml.gz' | File extension to append | | start | Integer | 1 | Starting number for sequential names | | zero | String/Integer | nil | Value appended for the first name (e.g., '' for base.xml.gz, '_index' for base_index.xml.gz) | **Example:** ```ruby # Default behavior: sitemap.xml.gz, sitemap1.xml.gz, sitemap2.xml.gz, ... namer = SitemapGenerator::SimpleNamer.new(:sitemap, start: 2, zero: 1) # Custom behavior: products.xml, products_1.xml, products_2.xml, ... namer = SitemapGenerator::SimpleNamer.new(:products, extension: '.xml', zero: '', start: 1) ``` ### Methods - `to_s()` — Returns current filename - `reset()` — Go back to first name - `start?()` — True if at first name - `next()` — Advance to next name, return self - `previous()` — Go to previous name, return self ``` -------------------------------- ### Set Options in `create` Call Source: https://github.com/kjvarga/sitemap_generator/blob/master/README.md Pass options directly to the `SitemapGenerator::Sitemap.create` method for a specific generation process. This is convenient when setting multiple options. ```ruby SitemapGenerator::Sitemap.create( :default_host => 'http://example.com', :sitemaps_path => 'sitemaps/') do add '/home' end ``` -------------------------------- ### Get Path Relative to Public Directory Source: https://github.com/kjvarga/sitemap_generator/blob/master/_autodocs/api-reference/sitemap_location.md Returns the sitemap's path relative to the public_path. This is useful for generating storage keys. ```ruby location.path_in_public # => "sitemaps/sitemap.xml.gz" ``` -------------------------------- ### SitemapUrl Initialization Source: https://github.com/kjvarga/sitemap_generator/blob/master/_autodocs/api-reference/sitemap_url.md Creates a new SitemapUrl instance with the given path and metadata. It supports various options for SEO, modification dates, and sitemap extensions. ```APIDOC ## new(path, options = {}) Creates a new SitemapUrl instance with the given path and metadata. ### Parameters | Parameter | Type | Required | Default | Description | |-----------|------|----------|---------|-------------| | path | String/SitemapFile | yes | — | URL path (e.g., `/products`) or a SitemapFile instance (from which host and lastmod are extracted) | | options | Hash | no | {} | Metadata for the URL | ### Options | Option | Type | Default | Description | |--------|------|---------|-------------| | host | String | required | Host including protocol, e.g., `http://example.com` | | priority | Float | 0.5 | SEO priority between 0.0 and 1.0 | | changefreq | String | 'weekly' | Update frequency: 'always', 'hourly', 'daily', 'weekly', 'monthly', 'yearly', 'never' | | lastmod | Time/Date/String | Time.now | Last modification date in ISO 8601 format or as a Date/Time object | | expires | Time/Date/String | nil | Expiration date (used in news sitemaps) | | images | Array | [] | Array of image metadata hashes for image sitemaps | | video | Hash/Array | nil | Video metadata; automatically converted to `:videos` array | | videos | Array | [] | Array of video metadata hashes for video sitemaps | | news | Hash | {} | News metadata for news sitemaps | | mobile | Boolean | false | Whether this URL is mobile-optimized | | alternate | Hash/Array | nil | Alternate language/media version; converted to `:alternates` | | alternates | Array | [] | Array of alternate version hashes | | pagemap | Hash | nil | Custom PageMap data | ### Return Value A new SitemapUrl instance (Hash subclass). ### Exceptions - `ArgumentError` — if unknown options are provided - `RuntimeError` — if no host is provided ### Example ```ruby url = SitemapGenerator::Builder::SitemapUrl.new( '/products/widget', host: 'https://example.com', priority: 0.9, changefreq: 'daily', lastmod: Date.today ) ``` ``` -------------------------------- ### News Sitemap Naming with Custom Extension Source: https://github.com/kjvarga/sitemap_generator/blob/master/_autodocs/api-reference/simple_namer.md Configures a namer for news sitemaps with a custom '.xml' extension. Numbering starts from 1 by default. ```ruby namer = SitemapGenerator::SimpleNamer.new(:news_sitemap, extension: '.xml') # Generates: # news_sitemap.xml # news_sitemap1.xml # news_sitemap2.xml ``` -------------------------------- ### Configure Google Cloud Storage Adapter with Environment Variables Source: https://github.com/kjvarga/sitemap_generator/blob/master/README.md Configure the GoogleStorageAdapter using environment variables for authentication. Ensure 'google/cloud/storage' is required. ```ruby SitemapGenerator::Sitemap.adapter = SitemapGenerator::GoogleStorageAdapter.new( bucket: 'name_of_bucket' ) ``` -------------------------------- ### LinkSet Initialization Source: https://github.com/kjvarga/sitemap_generator/blob/master/_autodocs/api-reference/link_set.md Creates a new LinkSet instance with configuration options. ```APIDOC ## new(options = {}) ### Description Creates a new `LinkSet` instance with configuration options. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters - **adapter** (Object) - Optional - Instance of an adapter class with a `write(location, data)` method for persisting sitemaps. Supports FileAdapter, S3Adapter, AwsSdkAdapter, WaveAdapter, FogAdapter, GoogleStorageAdapter, ActiveStorageAdapter - **default_host** (String) - Optional - Host including protocol for sitemap links, e.g., `http://example.com`. Required unless providing a host in each `add()` call - **public_path** (String/Pathname) - Optional - Default: `public/` - Full or relative path to the output directory for sitemaps - **sitemaps_host** (String) - Optional - Host for sitemap file URLs. If different from `default_host`, `include_index` is automatically disabled - **sitemaps_path** (String) - Optional - Path fragment relative to `public_path` where sitemaps are written, e.g., `sitemaps/` - **filename** (Symbol) - Optional - Default: `:sitemap` - Base filename for generated files (generates `sitemap.xml.gz`, `sitemap1.xml.gz`, etc.) - **include_index** (Boolean) - Optional - Default: `false` - Whether to add a link to the sitemap index in the sitemap itself - **include_root** (Boolean) - Optional - Default: `true` - Whether to automatically add the root path `/` to the sitemap - **search_engines** (Hash) - Optional - Default: `{}` - Hash mapping search engine names to ping URLs - **verbose** (Boolean) - Optional - Default: `false` - Whether to output summary information to STDOUT - **create_index** (Boolean/Symbol) - Optional - Default: `:auto` - When to create a sitemap index: `true` (always), `false` (never), `:auto` (only if multiple sitemaps) - **namer** (SimpleNamer) - Optional - Instance of SimpleNamer for custom filename generation - **compress** (Boolean/Symbol) - Optional - Default: `true` - Compression strategy: `true` (all), `false` (none), `:all_but_first` (skip first, compress rest) - **max_sitemap_links** (Integer) - Optional - Default: `50000` - Maximum links per sitemap file - **yield_sitemap** (Boolean) - Optional - Default: `nil` - Whether to yield the LinkSet to the block in `create()` ### Request Example ```ruby link_set = SitemapGenerator::LinkSet.new( default_host: 'https://example.com', public_path: 'public/', sitemaps_path: 'sitemaps/', compress: true, verbose: true ) ``` ### Response #### Success Response (200) None #### Response Example None ``` -------------------------------- ### Custom Sitemap Output Path Source: https://github.com/kjvarga/sitemap_generator/blob/master/_autodocs/QUICK_START.md Configure a custom path for sitemap files within the `create` block. This example places sitemaps in `public/static/`. ```ruby SitemapGenerator::Sitemap.create(sitemaps_path: 'static/') do add '/' end ``` -------------------------------- ### Create Sitemaps with Custom Configuration Source: https://github.com/kjvarga/sitemap_generator/blob/master/_autodocs/README.md Specify a custom configuration file path when running the sitemap creation task. ```bash rake sitemap:create CONFIG_FILE=config/sitemaps/main.rb ``` -------------------------------- ### Get Sitemap Index URL Source: https://github.com/kjvarga/sitemap_generator/blob/master/_autodocs/api-reference/link_set.md Returns the full URL to the sitemap index file. If `create_index` is false, it returns the URL of the first sitemap. ```ruby url = link_set.sitemap_index_url # => "https://example.com/sitemap.xml.gz" ``` -------------------------------- ### Get Last Modified Time Source: https://github.com/kjvarga/sitemap_generator/blob/master/_autodocs/api-reference/sitemap_file.md Returns the last modified time of the sitemap file on disk. Returns nil if no file has been reserved yet. ```ruby mtime = sitemap.lastmod # => 2025-06-23 14:30:45 +0000 ``` -------------------------------- ### Get Full Filesystem Path Source: https://github.com/kjvarga/sitemap_generator/blob/master/_autodocs/api-reference/sitemap_location.md Returns the absolute path to the sitemap file on the filesystem. Requires initialization with host, public_path, sitemaps_path, and filename. ```ruby location = SitemapGenerator::SitemapLocation.new( host: 'https://example.com', public_path: 'public/', sitemaps_path: 'sitemaps/', filename: 'sitemap.xml.gz' ) location.path # => "/home/user/myapp/public/sitemaps/sitemap.xml.gz" ``` -------------------------------- ### Check Sitemap Configuration File Source: https://github.com/kjvarga/sitemap_generator/blob/master/_autodocs/QUICK_START.md Verify that the `config/sitemap.rb` file exists and is readable by the system. ```bash ls -l config/sitemap.rb ``` -------------------------------- ### PageMap Custom Metadata Example Source: https://github.com/kjvarga/sitemap_generator/blob/master/_autodocs/api-reference/sitemap_url.md Configure custom metadata for a sitemap entry using the `pagemap` option. Each DataObject can have a type, ID, and an array of attributes. ```ruby pagemap: { dataobjects: [ { type: 'document', id: 'doc1', attributes: [ { name: 'author', value: 'John Doe' }, { name: 'publish_date', value: '2025-06-23' } ] } ] } ``` -------------------------------- ### Initialize AwsSdkAdapter in config/sitemap.rb Source: https://github.com/kjvarga/sitemap_generator/wiki/Generate-Sitemap-and-upload-to-s3-using-aws-sdk-gem Configure `SitemapGenerator` to use `AwsSdkAdapter` by providing your S3 bucket name and AWS credentials. ```ruby SitemapGenerator::Sitemap.adapter = SitemapGenerator::AwsSdkAdapter.new(, aws_access_key_id: , aws_secret_access_key: , aws_region: ) ``` -------------------------------- ### Index Plus Numbered Files Source: https://github.com/kjvarga/sitemap_generator/blob/master/_autodocs/api-reference/simple_namer.md Configures a namer to generate an '_index' file followed by numbered files starting from 1. Useful for sitemap index files. ```ruby namer = SitemapGenerator::SimpleNamer.new(:sitemap, zero: '_index', start: 1) # Generates: # sitemap_index.xml.gz # sitemap1.xml.gz # sitemap2.xml.gz ``` -------------------------------- ### FogAdapter Initialization Source: https://github.com/kjvarga/sitemap_generator/blob/master/_autodocs/api-reference/adapters.md Initializes the FogAdapter for uploading sitemaps to cloud storage services like AWS S3, Google Cloud Storage, etc., using the Fog gem. ```APIDOC ## FogAdapter Uploads to cloud storage services (AWS S3, Google Cloud Storage, Rackspace, etc.) using the Fog gem. **Location:** `lib/sitemap_generator/adapters/fog_adapter.rb` **Class:** `SitemapGenerator::FogAdapter` **Requires:** `gem 'fog-core'` and provider-specific gems (e.g., `fog-aws`) ### new(opts = {}) Initializes the adapter with Fog configuration. **Parameters** | Parameter | Type | Description | |-----------|------|-------------| | opts | Hash | Fog credentials and configuration passed to Fog::Storage.new | **Return Value** A new FogAdapter instance. **Example** ```ruby adapter = SitemapGenerator::FogAdapter.new( provider: 'AWS', aws_access_key_id: 'KEY', aws_secret_access_key: 'SECRET', region: 'us-west-2' ) SitemapGenerator::Sitemap.adapter = adapter ``` ``` -------------------------------- ### Generate Sequential Filenames Source: https://github.com/kjvarga/sitemap_generator/blob/master/_autodocs/api-reference/simple_namer.md Demonstrates how to generate sequential filenames using the `to_s` and `next` methods. The sequence starts with the base name and increments with each call to `next`. ```ruby namer = SitemapGenerator::SimpleNamer.new(:sitemap) namer.to_s # => "sitemap.xml.gz" namer.next namer.to_s # => "sitemap1.xml.gz" namer.next namer.to_s # => "sitemap2.xml.gz" ``` -------------------------------- ### Create a New Sitemap Source: https://github.com/kjvarga/sitemap_generator/blob/master/_autodocs/INDEX.md Use this method to initialize a new sitemap with optional configurations and a block for defining URLs. ```ruby SitemapGenerator::Sitemap.create(options, &block) ``` -------------------------------- ### Upload Sitemaps to S3 Source: https://github.com/kjvarga/sitemap_generator/blob/master/_autodocs/QUICK_START.md Configure the `AwsSdkAdapter` to upload generated sitemaps directly to an S3 bucket. Ensure the `aws-sdk-s3` gem is installed and configured with credentials or an IAM role. ```ruby require 'aws-sdk-s3' SitemapGenerator::Sitemap.default_host = 'https://example.com' # Configure S3 adapter SitemapGenerator::Sitemap.adapter = SitemapGenerator::AwsSdkAdapter.new( 'my-bucket', region: 'us-west-2' ) SitemapGenerator::Sitemap.create do add '/', priority: 1.0 Product.find_each { |p| add product_path(p) } end ``` ```ruby SitemapGenerator::Sitemap.adapter = SitemapGenerator::AwsSdkAdapter.new('my-bucket') ``` -------------------------------- ### Combine Local and Remote Adapters Source: https://github.com/kjvarga/sitemap_generator/blob/master/_autodocs/api-reference/adapters.md Create a dual adapter to write sitemaps to both a local filesystem and a remote storage service like S3. This ensures redundancy and accessibility. ```ruby class DualAdapter def initialize(local_adapter, remote_adapter) @local = local_adapter @remote = remote_adapter end def write(location, raw_data) @local.write(location, raw_data) @remote.write(location, raw_data) end end SitemapGenerator::Sitemap.adapter = DualAdapter.new( SitemapGenerator::FileAdapter.new, SitemapGenerator::AwsSdkAdapter.new('my-bucket') ) ``` -------------------------------- ### Reset and Reuse Namer Source: https://github.com/kjvarga/sitemap_generator/blob/master/_autodocs/api-reference/simple_namer.md Demonstrates resetting a SimpleNamer instance to its initial state. This is useful for starting a new sequence of sitemap files after a previous sequence has been generated or for testing purposes. ```ruby namer = SitemapGenerator::SimpleNamer.new(:sitemap) namer.next.next.next puts namer.to_s # => "sitemap3.xml.gz" namer.reset puts namer.to_s # => "sitemap.xml.gz" ``` -------------------------------- ### Standalone Sitemap Generation Source: https://github.com/kjvarga/sitemap_generator/blob/master/_autodocs/api-reference/interpreter.md Generate sitemaps independently of a Rails application. This example demonstrates initializing the Interpreter with a default host and defining sitemap entries, including an image sitemap. ```ruby require 'sitemap_generator' interpreter = SitemapGenerator::Interpreter.new( default_host: 'https://example.com' ) do add '/' add '/products' (1..1000).each do |i| add "/article/#{i}", lastmod: Date.today end group(filename: :images) do add '/gallery', images: [ { loc: '/img/pic1.jpg', caption: 'Picture 1' }, { loc: '/img/pic2.jpg', caption: 'Picture 2' } ] end end interpreter.sitemap.finalize! ``` -------------------------------- ### Interpreter.run Source: https://github.com/kjvarga/sitemap_generator/blob/master/_autodocs/api-reference/interpreter.md Loads and evaluates a sitemap configuration file using the default `SitemapGenerator::Sitemap` LinkSet instance. It accepts options to specify the configuration file path and other settings. ```APIDOC ## Class Method ### self.run(opts = {}, &block) Loads and evaluates a sitemap configuration file, using the default `SitemapGenerator::Sitemap` LinkSet instance. **Parameters** | Parameter | Type | Default | Description | |-----------|------|---------|-------------| | opts | Hash | {} | Options | | opts[:config_file] | String | `config/sitemap.rb` | Full path to the configuration file to load and evaluate | | other opts | — | — | Passed to `Interpreter.new()` | | block | Proc | nil | Optional block (not typically used with `run()`) | **Return Value** The Interpreter instance after evaluating the config file. **Exceptions** - File not found errors if config_file does not exist - Ruby syntax errors if the config file has invalid Ruby **Example** ```ruby # Load default config/sitemap.rb SitemapGenerator::Interpreter.run # Load from a custom path SitemapGenerator::Interpreter.run( config_file: 'config/sitemaps/main.rb' ) # Load and set options SitemapGenerator::Interpreter.run( config_file: 'config/sitemap.rb', default_host: 'https://example.com', verbose: true ) ``` ``` -------------------------------- ### Configure S3Adapter with Fog Options Source: https://github.com/kjvarga/sitemap_generator/wiki/Generate-Sitemaps-on-read-only-filesystems-like-Heroku Initialize the SitemapGenerator::S3Adapter with specific Fog configuration options. This method allows direct passing of credentials and bucket details, bypassing environment variables. ```ruby SitemapGenerator::Sitemap.adapter = SitemapGenerator::S3Adapter.new(fog_provider: 'AWS', aws_access_key_id: , aws_secret_access_key: , fog_directory: , fog_region: ) ```