Skip to content

Instantly share code, notes, and snippets.

@casper-rasmussen
Last active September 30, 2016 21:14
Show Gist options
  • Save casper-rasmussen/c0ae18fefbce9b07ff612371eb2d5827 to your computer and use it in GitHub Desktop.
Save casper-rasmussen/c0ae18fefbce9b07ff612371eb2d5827 to your computer and use it in GitHub Desktop.
/// <summary>
/// Best Bet Selector reacting on URLs that matches based on Segment
/// </summary>
public class DynamicPropertyBestBetSelector : IBestBetSelector, IDynamicData
{
/// <summary>
/// The Identity.ExternalId of the BestBet in the BestBetRepository, is stored as the IndexId
/// This links the indexed item (ElasticSearch) with the best bet in the BestBetRepository
///
/// </summary>
public string IndexId { get; set; }
public string Url { get; set; }
/// <summary>
/// Stores language for the external url best bet
///
/// </summary>
public string Language { get; set; }
public string Description
{
get
{
return string.Format("{0} ({1})", this.Url, this.IndexId);
}
}
public Identity Id { get; set; }
public Filter GetTargetSelectionFilter()
{
//Get the trip ID from our URL
string segment = this.GetSegmentFromUrl(this.Url)
return (Filter)new TermFilter("UrlSegment$$string", FieldFilterValue.Create(segment));
}
public bool Match(string documentId)
{
return this.IndexId.Equals(documentId, StringComparison.OrdinalIgnoreCase);
}
public string GetTargetKey()
{
return this.Url;
}
public IBestBetSelector Parse(string input, string language)
{
return new TripBestBetSelector()
{
Url = input,
Language = language
};
}
public bool SameTargetAs(IBestBetSelector selector)
{
TripBestBetSelector tripBestBetSelector = selector as TripBestBetSelector;
if (tripBestBetSelector != null && tripBestBetSelector.Url != null)
{
if (this.Url != null)
{
try
{
return Uri.Compare(new Uri(this.Url, UriKind.Absolute), new Uri(tripBestBetSelector.Url, UriKind.Absolute), UriComponents.AbsoluteUri, UriFormat.SafeUnescaped, StringComparison.Ordinal) == 0;
}
catch (ArgumentException)
{
return false;
}
catch (UriFormatException)
{
return false;
}
}
}
return false;
}
/// <summary>
/// Determine if the input is suited for being a Managed
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static bool Suitable(string input)
{
//Determine if the URL is suited to be managed by our Selector - e.g. based on structure or pattern
}
private string GetSegmentFromUrl(string input)
{
//Extract the segment that identifies our index item. Could also be a key or something similar.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment