public void StoreTest() { var urlStore = new UrlStore(); var uri = new Uri("http://byterot.blogspot.com"); urlStore.Store(uri); Assert.True(urlStore.Exists(uri)); }
public static string ShortenUrl(this string longUrl) { var datastore = new UrlStore(); var shortUrlPath = longUrl.GetHashCode().ToString(); datastore.SaveUrl(longUrl, shortUrlPath); return shortUrlPath; }
private Negotiator ShortenUrl(UrlStore urlStore) { string longUrl = Request.Form.url; var shortUrl = ShortenUrl(longUrl); urlStore.SaveUrl(longUrl, shortUrl); return View["shortened_url", new { Request.Headers.Host, ShortUrl = shortUrl }]; }
public void StoreTest() { var urlStore = new UrlStore(); var uri = new Uri("http://byterot.blogspot.com"); urlStore.Store(uri); Assert.True(urlStore.Exists(uri)); }
private Response ShortenUrl(UrlStore urlStore) { string longUrl = Request.Form.url; var shortUrl = ShortenUrl(longUrl); urlStore.SaveUrl(longUrl, shortUrl); return(View["shortened_url", new { Request.Headers.Host, ShortUrl = shortUrl }]); }
public ApiModule(UrlStore urlStore) : base("/api") { Get["/items"] = parameters => GetSavedItems(urlStore); Get["/stats"] = parameters => GetAllStats(urlStore); Get["/{shorturl}/stats"] = parameters => GetShortUrlStats(parameters, urlStore); Get["/{shorturl}/content"] = parameters => GetShortUrlContent(parameters, urlStore); }
public ShortUrlModule(UrlStore urlStore) { Get["/"] = _ => View["index.html"]; Post["/"] = _ => ShortenUrl(urlStore); Get["/{shorturl}"] = param => { string shortUrl = param.shorturl; return(Response.AsRedirect(urlStore.GetUrlFor(shortUrl.ToString()), Nancy.Responses.RedirectResponse.RedirectType.Temporary)); }; }
public ShortUrlModule(UrlStore urlStore) { Get["/"] = _ => View["index.html"]; Post["/"] = _ => ShortenUrl(urlStore); Get["/{shorturl}"] = param => { string shortUrl = param.shorturl; return Response.AsRedirect(urlStore.GetUrlFor(shortUrl.ToString())); }; }
public void QueueTest() { var urlStore = new UrlStore(); var uri = new Uri("http://byterot.blogspot.com"); urlStore.Enqueue(uri); Uri uri2 = null; Assert.True(urlStore.TryDequeue(out uri2)); Assert.NotNull(uri2); Assert.Equal(uri.ToString(), uri2.ToString()); }
public void QueueTest() { var urlStore = new UrlStore(); var uri = new Uri("http://byterot.blogspot.com"); urlStore.Enqueue(uri); Uri uri2 = null; Assert.True(urlStore.TryDequeue(out uri2)); Assert.NotNull(uri2); Assert.Equal(uri.ToString(), uri2.ToString()); }
private dynamic GetAllStats(UrlStore urlStore) { var stats = urlStore.GetAllStats(); var jsonBytes = Encoding.UTF8.GetBytes(stats.ToJson()); return(new Response { ContentType = "application/json", Contents = s => s.Write(jsonBytes, 0, jsonBytes.Length) }); }
private dynamic GetLongUrl(dynamic parameters, UrlStore urlStore) { string shortUrl = parameters.shorturl; //* var bsonHeaders = new BsonDocument(); foreach (String key in Request.Headers.Keys) { IEnumerable <string> values = Request.Headers[key]; int count = values.Count(); if (count == 1) { bsonHeaders.Add(key, values.First()); } else { BsonArray bsonArray = new BsonArray(values); bsonHeaders.Add(key, bsonArray); } } //*/ var logRequest = new BsonDocument { { "shortUrl", shortUrl }, { "userHostAddress", GetUserHostAddress() }, { "headers", bsonHeaders }, { "userAgent", Request.Headers.UserAgent }, { "referrer", Request.Headers.Referrer }, { "timestamp", DateTime.UtcNow }, { "statusCode", 200 } }; string longUrl = urlStore.GetUrlForNav(shortUrl.ToString(), logRequest); if (String.IsNullOrEmpty(longUrl)) { return(View["404.html"]); } else { return(Response.AsRedirect(longUrl)); } }
private dynamic GetShortUrlStats(dynamic parameters, UrlStore urlStore) { string shortUrl = parameters.shorturl; var stats = urlStore.GetStatsFor(shortUrl); var json = stats.ToJson(); //return Negotiate.WithModel(result); var jsonBytes = Encoding.UTF8.GetBytes(json); return(new Response { ContentType = "application/json", Contents = s => s.Write(jsonBytes, 0, jsonBytes.Length) }); }
public ShortUrlModule(UrlStore urlStore) { Get["/"] = _ => View["index.html"]; Post["/"] = _ => ShortenAndSaveUrl(urlStore); Post["/new"] = _ => ShortenAndSaveUrl(urlStore); Post["/test"] = parameters => TestMethod(parameters); Get["/{shorturl}"] = parameters => GetLongUrl(parameters, urlStore); Get["/items"] = parameters => GetSavedItems(urlStore); Get["/stats"] = parameters => GetAllStats(urlStore); Get["/{shorturl}/stats"] = parameters => GetShortUrlStats(parameters, urlStore); Get["/{shorturl}/content"] = parameters => GetShortUrlContent(parameters, urlStore); Get["/cleanup"] = _ => CleanupCollections(urlStore); }
private Negotiator ShortenAndSaveUrl(UrlStore urlStore) { string longUrl = Request.Form.url; if (longUrl == null) { var newUrl = this.Bind <NewUrl>(); longUrl = newUrl.Url; } if (longUrl == null) { return(Negotiate.WithModel(new { Message = "Url parameter is null" })); } var shortUrl = ShortenUrl(longUrl); if (urlStore.GetUrlFor(shortUrl) == null) { urlStore.SaveUrl(longUrl, shortUrl); } return(Negotiate.WithModel(new { ShortUrl = ("http://" + Request.Headers.Host + "/" + shortUrl) })); }
private dynamic CleanupCollections(UrlStore urlStore) { urlStore.ClearCollections(); return(View["404.html"]); }
private dynamic GetShortUrlContent(dynamic parameters, UrlStore urlStore) { return(Negotiate.WithModel(new { Message = "Calling method GetShortUrlContent" })); }