Class: SiteMaps::Middleware
- Inherits:
-
Object
- Object
- SiteMaps::Middleware
- Defined in:
- lib/site_maps/middleware.rb
Constant Summary collapse
- DEFAULT_X_ROBOTS_TAG =
"noindex, follow"- DEFAULT_CACHE_CONTROL =
"public, max-age=3600"- URLSET_XSL_PATH =
"/_sitemap-stylesheet.xsl"- INDEX_XSL_PATH =
"/_sitemap-index-stylesheet.xsl"
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app, adapter: nil, public_prefix: nil, storage_prefix: nil, x_robots_tag: DEFAULT_X_ROBOTS_TAG, cache_control: DEFAULT_CACHE_CONTROL) ⇒ Middleware
constructor
Both options accept a callable (0-arg or 1-arg receiving env), which is useful in multi-tenant setups where the prefix depends on the current request/site.
Constructor Details
#initialize(app, adapter: nil, public_prefix: nil, storage_prefix: nil, x_robots_tag: DEFAULT_X_ROBOTS_TAG, cache_control: DEFAULT_CACHE_CONTROL) ⇒ Middleware
Both options accept a callable (0-arg or 1-arg receiving env), which is useful in multi-tenant setups where the prefix depends on the current request/site.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/site_maps/middleware.rb', line 31 def initialize( app, adapter: nil, public_prefix: nil, storage_prefix: nil, x_robots_tag: DEFAULT_X_ROBOTS_TAG, cache_control: DEFAULT_CACHE_CONTROL ) @app = app @adapter = adapter @public_prefix = public_prefix @storage_prefix = storage_prefix @x_robots_tag = x_robots_tag @cache_control = cache_control end |
Instance Method Details
#call(env) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/site_maps/middleware.rb', line 47 def call(env) path = env["PATH_INFO"] if xsl_request?(path) serve_xsl(path) elsif path.end_with?(".xml", ".xml.gz") pub_prefix = resolve_value(@public_prefix, env) sto_prefix = resolve_value(@storage_prefix, env) # Strip public prefix (nil = no match when prefix is configured but doesn't match) stripped = strip_prefix(path, pub_prefix) # Prepend storage prefix to get the internal path used for adapter lookups internal_path = stripped && prepend_prefix(stripped, sto_prefix) # Only resolve the adapter (potentially expensive: DB lookup, callable) when # the path already looks like a sitemap file and passed prefix checks. current_adapter = resolve_adapter(env) if internal_path if current_adapter && sitemap_request?(internal_path, current_adapter) serve_sitemap(internal_path, current_adapter, pub_prefix: pub_prefix, sto_prefix: sto_prefix) else @app.call(env) end else @app.call(env) end end |