Mercurial > altnet-hispano
comparison Agendas/trunk/src/Agendas.Twitter/TwitterPublicador.cs @ 122:17531db40d4e
Refactoring de la generación de mensajes en twitter
author | nelopauselli |
---|---|
date | Tue, 28 Jun 2011 19:25:22 -0300 |
parents | c3573defd18f |
children | 734d3f0853bf |
comparison
equal
deleted
inserted
replaced
121:683cc27450ce | 122:17531db40d4e |
---|---|
1 using System.Collections.Generic; | 1 using System.Collections.Generic; |
2 using System.Configuration; | 2 using System.Configuration; |
3 using System.Linq; | 3 using System.Linq; |
4 using AltNetHispano.Agendas.Domain; | 4 using AltNetHispano.Agendas.Domain; |
5 using AltNetHispano.Agendas.Twitter.Writers; | |
5 using Twitterizer; | 6 using Twitterizer; |
6 | 7 |
7 namespace AltNetHispano.Agendas.Twitter | 8 namespace AltNetHispano.Agendas.Twitter |
8 { | 9 { |
9 public class TwitterPublicador : IPublicador | 10 public class TwitterPublicador : IPublicador |
10 { | 11 { |
12 private readonly IDictionary<Accion, ITwitterWriter> _writers; | |
11 private readonly ITwitterAdapter _twitterAdapter; | 13 private readonly ITwitterAdapter _twitterAdapter; |
12 | 14 |
13 public TwitterPublicador() | 15 public TwitterPublicador() |
16 : this(new TwitterAdapter()) | |
14 { | 17 { |
15 _twitterAdapter = new TwitterAdapter(); | |
16 } | 18 } |
17 | 19 |
18 public TwitterPublicador(ITwitterAdapter twitterAdapter) | 20 public TwitterPublicador(ITwitterAdapter twitterAdapter) |
19 { | 21 { |
22 _writers = new Dictionary<Accion, ITwitterWriter> | |
23 { | |
24 {Accion.Proponer, new ProponerTwitterWriter()}, | |
25 {Accion.Agendar, new AgendarTwitterWriter()}, | |
26 {Accion.Confirmar, new ConfirmarTwitterWriter()}, | |
27 {Accion.Publicar, new PublicarTwitterWriter()} | |
28 }; | |
29 | |
20 _twitterAdapter = twitterAdapter; | 30 _twitterAdapter = twitterAdapter; |
21 } | 31 } |
22 | 32 |
23 public const int LIMITE_MENSAJE = 140; | 33 private const int LIMITE_MENSAJE = 140; |
24 | 34 |
25 private string ConsumerKey | 35 private static string ConsumerKey |
26 { | 36 { |
27 get { return ConfigurationManager.AppSettings["consumerKeyAppTwitt"]; } | 37 get { return ConfigurationManager.AppSettings["consumerKeyAppTwitt"]; } |
28 } | 38 } |
29 | 39 |
30 private string ConsumerSecret | 40 private static string ConsumerSecret |
31 { | 41 { |
32 get { return ConfigurationManager.AppSettings["consumerSecretAppTwitt"]; } | 42 get { return ConfigurationManager.AppSettings["consumerSecretAppTwitt"]; } |
33 } | 43 } |
34 | 44 |
35 | 45 |
36 private string AccessToken | 46 private static string AccessToken |
37 { | 47 { |
38 get { return ConfigurationManager.AppSettings["accessTokenAppTwitt"]; } | 48 get { return ConfigurationManager.AppSettings["accessTokenAppTwitt"]; } |
39 } | 49 } |
40 | 50 |
41 | 51 |
42 private string AccessTokenSecret | 52 private static string AccessTokenSecret |
43 { | 53 { |
44 get { return ConfigurationManager.AppSettings["accessTokenSecretAppTwitt"]; } | 54 get { return ConfigurationManager.AppSettings["accessTokenSecretAppTwitt"]; } |
45 } | 55 } |
46 | 56 |
47 public void Publicar(IEnumerable<Track> tracks) | 57 public void Publicar(IEnumerable<Track> tracks) |
59 track.LogAdd(new TrackLog(TrackLogPropietario.Twitter, message, track.Usuario, success)); | 69 track.LogAdd(new TrackLog(TrackLogPropietario.Twitter, message, track.Usuario, success)); |
60 } | 70 } |
61 } | 71 } |
62 } | 72 } |
63 | 73 |
64 private static string BuildTwitt(Track track) | 74 private string BuildTwitt(Track track) |
65 { | 75 { |
66 string body = string.Empty; | 76 ITwitterWriter writer; |
67 switch (track.Accion) | 77 if (!_writers.TryGetValue(track.Accion, out writer)) |
68 { | 78 return string.Empty; |
69 case Accion.Proponer: | 79 |
70 body = "Se ha propuesto un/a nuevo/a " + track.Evento.Tipo; | 80 string body = writer.Write(track); |
71 break; | |
72 case Accion.Agendar: | |
73 body = "Se ha agendando el evento" + track.Evento.Titulo; | |
74 if (track.Evento.Ponente.Twitter != null) | |
75 body += " con @" + track.Evento.Ponente.Twitter; | |
76 break; | |
77 case Accion.Confirmar: | |
78 if (track.Evento.Fecha.HasValue) | |
79 body = "Se confirma para el " + track.Evento.Fecha.Value.ToShortDateString() + " el evento " + track.Evento.Titulo; | |
80 break; | |
81 } | |
82 | 81 |
83 return body.Length > LIMITE_MENSAJE ? body.Substring(0, LIMITE_MENSAJE - 1) : body; | 82 return body.Length > LIMITE_MENSAJE ? body.Substring(0, LIMITE_MENSAJE - 1) : body; |
84 } | 83 } |
85 | 84 |
86 public bool Publicar(string status, out string message) | 85 public bool Publicar(string status, out string message) |