# HG changeset patch # User nelopauselli # Date 1312893508 10800 # Node ID fe47f11f5f2088ea890f7c4819d717670e293ad7 # Parent 49125b681709cabbcb62d84bce2a20b8df3659c4 Adapter para url shorten de google diff -r 49125b681709 -r fe47f11f5f20 Agendas/trunk/src/Agendas.Configurations/Agendas.Configurations.csproj --- a/Agendas/trunk/src/Agendas.Configurations/Agendas.Configurations.csproj Tue Aug 09 08:55:08 2011 -0300 +++ b/Agendas/trunk/src/Agendas.Configurations/Agendas.Configurations.csproj Tue Aug 09 09:38:28 2011 -0300 @@ -50,6 +50,7 @@ + diff -r 49125b681709 -r fe47f11f5f20 Agendas/trunk/src/Agendas.Configurations/GoogleConfigurationElement.cs --- a/Agendas/trunk/src/Agendas.Configurations/GoogleConfigurationElement.cs Tue Aug 09 08:55:08 2011 -0300 +++ b/Agendas/trunk/src/Agendas.Configurations/GoogleConfigurationElement.cs Tue Aug 09 09:38:28 2011 -0300 @@ -10,6 +10,11 @@ get { return base["calendar"] as CalendarConfigurationElement; } } + [ConfigurationProperty("shorten")] + public ShortenConfigurationElement Shorten + { + get { return base["shorten"] as ShortenConfigurationElement; } + } [ConfigurationProperty("enabled")] public bool Enabled { diff -r 49125b681709 -r fe47f11f5f20 Agendas/trunk/src/Agendas.Configurations/ShortenConfigurationElement.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Agendas/trunk/src/Agendas.Configurations/ShortenConfigurationElement.cs Tue Aug 09 09:38:28 2011 -0300 @@ -0,0 +1,13 @@ +using System.Configuration; + +namespace AltNetHispano.Agendas.Configurations +{ + public class ShortenConfigurationElement : ConfigurationElement + { + [ConfigurationProperty("urlAPI")] + public string UrlAPI + { + get { return base["urlAPI"] as string; } + } + } +} \ No newline at end of file diff -r 49125b681709 -r fe47f11f5f20 Agendas/trunk/src/Agendas.Google.Test/App.config --- a/Agendas/trunk/src/Agendas.Google.Test/App.config Tue Aug 09 08:55:08 2011 -0300 +++ b/Agendas/trunk/src/Agendas.Google.Test/App.config Tue Aug 09 09:38:28 2011 -0300 @@ -8,34 +8,7 @@ + - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff -r 49125b681709 -r fe47f11f5f20 Agendas/trunk/src/Agendas.Google.Test/GUrlShortenerTest.cs --- a/Agendas/trunk/src/Agendas.Google.Test/GUrlShortenerTest.cs Tue Aug 09 08:55:08 2011 -0300 +++ b/Agendas/trunk/src/Agendas.Google.Test/GUrlShortenerTest.cs Tue Aug 09 09:38:28 2011 -0300 @@ -1,4 +1,5 @@ -using AltNetHispano.Agendas.Google; +using System; +using AltNetHispano.Agendas.Google; using NUnit.Framework; namespace Agendas.Google.Test @@ -10,10 +11,12 @@ [Test] public void Obtener_Url_corta() { - var gUrlShortener = new GUrlShortener(); + var gUrlShortener = new GUrlShortenerAdapter(); var shortUrl = gUrlShortener.GetShortUrl("http://www.twitter.com"); + Console.WriteLine(shortUrl); + Assert.AreEqual("http://goo.gl/CbHS", shortUrl); } } diff -r 49125b681709 -r fe47f11f5f20 Agendas/trunk/src/Agendas.Google/Agendas.Google.csproj --- a/Agendas/trunk/src/Agendas.Google/Agendas.Google.csproj Tue Aug 09 08:55:08 2011 -0300 +++ b/Agendas/trunk/src/Agendas.Google/Agendas.Google.csproj Tue Aug 09 09:38:28 2011 -0300 @@ -58,7 +58,7 @@ - + diff -r 49125b681709 -r fe47f11f5f20 Agendas/trunk/src/Agendas.Google/GUrlShortener.cs --- a/Agendas/trunk/src/Agendas.Google/GUrlShortener.cs Tue Aug 09 08:55:08 2011 -0300 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,18 +0,0 @@ -using System; -//using GoogleAPI.UrlShortener; - -namespace AltNetHispano.Agendas.Google -{ - public class GUrlShortener - { - public string GetShortUrl(string url) - { - //TODO: Esto es utilizando la GoogleAPI.UrlShortener. - //var client = new UrlResource(); - //var response = client.Insert(new ShortenRequest { LongUrl = url }); - - //return response.Id; - throw new NotImplementedException(); - } - } -} \ No newline at end of file diff -r 49125b681709 -r fe47f11f5f20 Agendas/trunk/src/Agendas.Google/GUrlShortenerAdapter.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Agendas/trunk/src/Agendas.Google/GUrlShortenerAdapter.cs Tue Aug 09 09:38:28 2011 -0300 @@ -0,0 +1,48 @@ +using System.IO; +using System.Net; +using AltNetHispano.Agendas.Configurations; + +namespace AltNetHispano.Agendas.Google +{ + public class GUrlShortenerAdapter + { + private readonly ShortenConfigurationElement _config; + + public GUrlShortenerAdapter() + { + _config = AgendasConfigurationManager.Publicadores.Google.Shorten; + } + + public string GetShortUrl(string url) + { + WebRequest request = WebRequest.Create(_config.UrlAPI); + request.Method = WebRequestMethods.Http.Post; + request.ContentType = "application/json"; + + using(var writer = new StreamWriter(request.GetRequestStream())) + { + var json = string.Format("{{\"longUrl\": \"{0}\"}}", url); + writer.Write(json); + } + + var response = request.GetResponse(); + + using(var reader = new StreamReader(response.GetResponseStream())) + { + const string prefix = "\"id\": \""; + + var json = reader.ReadToEnd(); + + var start = json.IndexOf(prefix) + prefix.Length; + if (start>prefix.Length) + { + var length = json.IndexOf("\",", start) - start; + + if (length>0) + return json.Substring(start, length); + } + } + return string.Empty; + } + } +} \ No newline at end of file