Module: SiteMaps

Defined in:
lib/site_maps.rb,
lib/site_maps/cli.rb,
lib/site_maps/runner.rb,
lib/site_maps/process.rb,
lib/site_maps/railtie.rb,
lib/site_maps/version.rb,
lib/site_maps/notification.rb,
lib/site_maps/configuration.rb,
lib/site_maps/sitemap_reader.rb,
lib/site_maps/sitemap_builder.rb,
lib/site_maps/notification/bus.rb,
lib/site_maps/atomic_repository.rb,
lib/site_maps/primitives/output.rb,
lib/site_maps/notification/event.rb,
lib/site_maps/incremental_location.rb,
lib/site_maps/runner/event_listener.rb

Defined Under Namespace

Modules: Adapters, Builder, Notification, Primitives Classes: AtomicRepository, CLI, Configuration, IncrementalLocation, Railtie, Runner, SitemapBuilder, SitemapReader

Constant Summary collapse

MAX_LENGTH =
{
  links: 50_000,
  images: 1_000,
  news: 1_000
}
MAX_FILESIZE =

bytes

50_000_000
DEFAULT_LOGGER =
Logger.new($stdout)
Error =
Class.new(StandardError)
AdapterNotFound =
Class.new(Error)
AdapterNotSetError =
Class.new(Error)
FileNotFoundError =
Class.new(Error)
FullSitemapError =
Class.new(Error)
ConfigurationError =
Class.new(Error)
Process =
Concurrent::ImmutableStruct.new(:name, :location_template, :kwargs_template, :block) do
  def id
    @id ||= SecureRandom.hex(4)
  end

  def location(**kwargs)
    return unless location_template

    location_template % keyword_arguments(kwargs)
  end

  def call(builder, **kwargs)
    return unless block

    block.call(builder, **keyword_arguments(kwargs))
  end

  def static?
    !dynamic?
  end

  def dynamic?
    kwargs_template.is_a?(Hash) && kwargs_template.any?
  end

  def keyword_arguments(given)
    (kwargs_template || {}).merge(given || {})
  end
end
VERSION =
"0.0.1.beta2"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.current_adapterObject (readonly)

Returns the value of attribute current_adapter.



46
47
48
# File 'lib/site_maps.rb', line 46

def current_adapter
  @current_adapter
end

.loggerObject



104
105
106
# File 'lib/site_maps.rb', line 104

def logger
  @logger ||= DEFAULT_LOGGER
end

Class Method Details

.config {|@config| ... } ⇒ Object Also known as: configure

Yields:



67
68
69
70
71
# File 'lib/site_maps.rb', line 67

def config
  @config ||= Configuration.new
  yield(@config) if block_given?
  @config
end

.generate(config_file: nil, **options) ⇒ Runner

Load and prepare a runner with the current adapter Note that it won’t start running until you call ‘#run` on the runner

Example:

SiteMaps.generate(config_file: "config/site_maps.rb", max_threads: 10)
  .enqueue_all
  .run

You may also enqueue processes manually, specially those that are dynamic

Example:

SiteMaps.generate(config_file: "config/site_maps.rb", max_threads: 10)
  .enqueue(:monthly, year: 2020, month: 1)
  .enqueue(:monthly, year: 2020, month: 2)
  .enqueue_remaining # Enqueue all other non-enqueued processes
  .run

Parameters:

  • config_file (String) (defaults to: nil)

    The path to a configuration file

  • options (Hash)

    Options to pass to the runner

Returns:

  • (Runner)

    An instance of the runner

Raises:



94
95
96
97
98
99
100
101
102
# File 'lib/site_maps.rb', line 94

def generate(config_file: nil, **options)
  if config_file
    @current_adapter = nil
    load(config_file)
  end
  raise AdapterNotSetError, "No adapter set. Use SiteMaps.use to set an adapter" unless current_adapter

  Runner.new(current_adapter, **options)
end

.use(adapter, **options, &block) ⇒ Object

Returns An instance of the adapter.

Parameters:

  • adapter (Class, String, Symbol)

    The name of the adapter to use

  • options (Hash)

    Options to pass to the adapter. Note that these are adapter-specific

  • block (Proc)

    A block to pass to the adapter

Returns:

  • (Object)

    An instance of the adapter



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/site_maps.rb', line 53

def use(adapter, **options, &block)
  adapter_class = if adapter.is_a?(Class) # && adapter < Adapters::Adapter
    adapter
  else
    const_name = Primitives::String.new(adapter.to_s).classify
    begin
      Adapters.const_get(const_name)
    rescue NameError
      raise AdapterNotFound, "Adapter #{adapter.inspect} not found"
    end
  end
  @current_adapter = adapter_class.new(**options, &block)
end