# HG changeset patch # User juanjose.montesdeocaarbos # Date 1312975292 10800 # Node ID 523e3586b1c11f0a4f97c5498a15b42edae1240f # Parent 03cf6a377ee8c2d53220418a39c719999aed82e4# Parent 03c237cd5cc4a4ceb8c2c9ae970243c06b82b980 Combinar diff -r 03cf6a377ee8 -r 523e3586b1c1 Agendas/trunk/src/Agendas.Configurations/Agendas.Configurations.csproj --- a/Agendas/trunk/src/Agendas.Configurations/Agendas.Configurations.csproj Wed Aug 10 08:20:28 2011 -0300 +++ b/Agendas/trunk/src/Agendas.Configurations/Agendas.Configurations.csproj Wed Aug 10 08:21:32 2011 -0300 @@ -50,6 +50,7 @@ + diff -r 03cf6a377ee8 -r 523e3586b1c1 Agendas/trunk/src/Agendas.Configurations/GoogleConfigurationElement.cs --- a/Agendas/trunk/src/Agendas.Configurations/GoogleConfigurationElement.cs Wed Aug 10 08:20:28 2011 -0300 +++ b/Agendas/trunk/src/Agendas.Configurations/GoogleConfigurationElement.cs Wed Aug 10 08:21:32 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 03cf6a377ee8 -r 523e3586b1c1 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 Wed Aug 10 08:21:32 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 03cf6a377ee8 -r 523e3586b1c1 Agendas/trunk/src/Agendas.Google.Test/App.config --- a/Agendas/trunk/src/Agendas.Google.Test/App.config Wed Aug 10 08:20:28 2011 -0300 +++ b/Agendas/trunk/src/Agendas.Google.Test/App.config Wed Aug 10 08:21:32 2011 -0300 @@ -8,34 +8,7 @@ + - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff -r 03cf6a377ee8 -r 523e3586b1c1 Agendas/trunk/src/Agendas.Google.Test/GUrlShortenerTest.cs --- a/Agendas/trunk/src/Agendas.Google.Test/GUrlShortenerTest.cs Wed Aug 10 08:20:28 2011 -0300 +++ b/Agendas/trunk/src/Agendas.Google.Test/GUrlShortenerTest.cs Wed Aug 10 08:21:32 2011 -0300 @@ -1,20 +1,42 @@ -using AltNetHispano.Agendas.Google; +using System; +using System.Net; +using AltNetHispano.Agendas.Google; using NUnit.Framework; namespace Agendas.Google.Test { [TestFixture] - [Explicit] public class GUrlShortenerTest { [Test] - public void Obtener_Url_corta() + 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); } + + [Test] + public void Acceder_a_url_corta() + { + var gUrlShortener = new GUrlShortenerAdapter(); + + var shortUrl = gUrlShortener.GetShortUrl("http://altnethispano.org"); + + Console.WriteLine(shortUrl); + + var request = WebRequest.Create(shortUrl); + var response = request.GetResponse() as HttpWebResponse; + + Assert.IsNotNull(response); + Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); + + Assert.AreEqual(new Uri("http://altnethispano.org/Inicio.aspx"), response.ResponseUri); + + } } } diff -r 03cf6a377ee8 -r 523e3586b1c1 Agendas/trunk/src/Agendas.Google/Agendas.Google.csproj --- a/Agendas/trunk/src/Agendas.Google/Agendas.Google.csproj Wed Aug 10 08:20:28 2011 -0300 +++ b/Agendas/trunk/src/Agendas.Google/Agendas.Google.csproj Wed Aug 10 08:21:32 2011 -0300 @@ -58,7 +58,7 @@ - + diff -r 03cf6a377ee8 -r 523e3586b1c1 Agendas/trunk/src/Agendas.Google/GUrlShortener.cs --- a/Agendas/trunk/src/Agendas.Google/GUrlShortener.cs Wed Aug 10 08:20:28 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 03cf6a377ee8 -r 523e3586b1c1 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 Wed Aug 10 08:21:32 2011 -0300 @@ -0,0 +1,57 @@ +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() as HttpWebResponse; + + if (response != null && response.StatusCode == HttpStatusCode.OK) + { + var stream = response.GetResponseStream(); + + if (stream != null) + { + using (var reader = new StreamReader(stream)) + { + 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