-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
+semver:breaking - introduces further abstractions and customisations
- Loading branch information
Showing
5 changed files
with
102 additions
and
51 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
src/Sitemapify/Providers/Impl/AbstractSitemapContentProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Sitemapify.Models; | ||
|
||
namespace Sitemapify.Providers.Impl | ||
{ | ||
public abstract class AbstractSitemapContentProvider<T> : ISitemapContentProvider | ||
{ | ||
public IEnumerable<SitemapUrl> GetSitemapUrls(Uri baseUrl) | ||
{ | ||
var overrideBaseUrl = GetBaseUrl(baseUrl); | ||
var entries = GetSitemapEntries(); | ||
if (entries == null) | ||
{ | ||
return Enumerable.Empty<SitemapUrl>(); | ||
} | ||
|
||
return entries | ||
.Where(WhereCore) | ||
.Select(c => CreateSitemapUrl(c, overrideBaseUrl)) | ||
.ToList(); | ||
} | ||
|
||
/// <summary> | ||
/// Returns a <see cref="SitemapUrl"/> composed from <see cref="entry"/>. | ||
/// </summary> | ||
/// <param name="entry">An object that is expected to be represented in the sitemap</param> | ||
/// <param name="baseUri">The host of the request made to the sitemap.xml</param> | ||
/// <returns></returns> | ||
protected abstract SitemapUrl CreateSitemapUrl(T entry, Uri baseUri); | ||
|
||
private bool WhereCore(T candidate) | ||
{ | ||
if (candidate == null) | ||
{ | ||
return false; | ||
} | ||
return Where(candidate); | ||
} | ||
|
||
/// <summary> | ||
/// Allows custom logic to be used to determine whether a node should be included in the sitemap, this occurs after all built-in determinations | ||
/// </summary> | ||
/// <param name="candidate"></param> | ||
/// <returns></returns> | ||
protected virtual bool Where(T candidate) | ||
{ | ||
return true; | ||
} | ||
|
||
/// <summary> | ||
/// Must return an absolute uri, otherwise the sitemap will be returned blank | ||
/// </summary> | ||
/// <param name="baseUri"></param> | ||
/// <returns></returns> | ||
protected virtual Uri GetBaseUrl(Uri baseUri) | ||
{ | ||
return baseUri; | ||
} | ||
|
||
/// <summary> | ||
/// Retrieves a list of <see cref="T"/> containing entities that should be included in the sitemap | ||
/// </summary> | ||
/// <returns></returns> | ||
protected abstract IEnumerable<T> GetSitemapEntries(); | ||
|
||
public virtual bool Cacheable { get; } = true; | ||
public virtual DateTime CacheUntil { get; } = DateTime.UtcNow.AddHours(1); | ||
} | ||
} |
19 changes: 13 additions & 6 deletions
19
src/Sitemapify/Providers/Impl/EmptySitemapContentProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,25 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Sitemapify.Models; | ||
|
||
namespace Sitemapify.Providers.Impl | ||
{ | ||
public class EmptySitemapContentProvider : ISitemapContentProvider | ||
public class EmptySitemapContentProvider : AbstractSitemapContentProvider<object> | ||
{ | ||
public virtual IEnumerable<SitemapUrl> GetSitemapUrls(Uri baseUrl) | ||
protected override SitemapUrl CreateSitemapUrl(object entry, Uri baseUri) | ||
{ | ||
var ub = new UriBuilder(baseUrl) {Path = "/"}; | ||
yield return SitemapUrl.Create(ub.ToString()); | ||
var ub = new UriBuilder(baseUri) | ||
{ | ||
Path = "/" | ||
|
||
}; | ||
return SitemapUrl.Create(ub.Uri.ToString(), changeFreq: SitemapChangeFrequency.Never); | ||
} | ||
|
||
public virtual bool Cacheable { get; } = true; | ||
public virtual DateTime CacheUntil { get; } = DateTime.UtcNow.AddHours(1); | ||
protected override IEnumerable<object> GetSitemapEntries() | ||
{ | ||
return Enumerable.Empty<object>(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters