jueves, 29 de mayo de 2014

Decorator Pattern in C#

I'm reading C# 3.0 Design Patterns by Judith Bisop
I've understood the decorator pattern.
Here is my little example for extending StreamReder class by password, when it reads.
using System;
using System.IO;

namespace Decorator
{
 public class LoginStream
 {
  private StreamReader  _streamReader;
  private string _password;

  public LoginStream (StreamReader streamReader, string password){
   _streamReader = streamReader;
   _password = password;
  }

  public string Read (string password){
   if (_password == password) {
    return _streamReader.ReadToEnd ();
   }
   throw new Exception ("Ivalid Password");
  }

 }
}
And the tests:
using System;
using System.IO;
using Decorator;
using NUnit.Framework;

namespace DecoratorTests
{
 [TestFixture()]
 public class Test
 {
  [Test()]
  public void LoginStream_Read_return_string_when_has_a_valid_password ()
  {
   var stream = new StreamReader("/home/luis/tmp/Prueba/Prueba.sln");
   var password = "password";
   var loginStream = new LoginStream(stream, password);

   var result = loginStream.Read(password);

   Assert.IsNotEmpty (result);
  }

  [Test()]
  [ExpectedException(typeof(Exception))]
  public void LoginStraeam_Read_return_exception_when_has_an_invalid_password ()
  {
   var stream = new StreamReader("path");
   var password = "password";
   var loginStream = new LoginStream(stream, password);

   loginStream.Read("invalidPassword");

   Assert.Fail ("Exception expected");
  }
 }
}


lunes, 31 de marzo de 2014

Export html Table to csv

Some time ago a collegue in Adesis, he needed to export html table to csv file.
I´ve dicovered some new music
So this the solution. It works
    



    
    
    
 
     
    
 Export To Excel



Titulo tabla
Nombre Provincia Telefono/Fax URL Marcas
Aistribuidor Nombre Tadrid 200 000 000 www.paginaweb.com marcas
Bistribuidor Nombre Sadrid 500 000 000 www.paginaweb.com marcas
Cistribuidor Nombre Sadrid 300 000 000 www.paginaweb.com marcas
Distribuidor Nombre Madrid 900 000 000 www.paginaweb.com marcas

viernes, 21 de marzo de 2014

martes, 28 de enero de 2014

Enable CORS in ASP.NET WebAPI

In Global.asax.cs file

public class WebApiApplication : System.Web.HttpApplication
{
    protected void Application_BeginRequest(object sender, EventArgs e)
    {
        HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
        HttpContext.Current.Response.AppendHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.
        HttpContext.Current.Response.AppendHeader("Pragma", "no-cache"); // HTTP 1.0.
        HttpContext.Current.Response.AppendHeader("Expires", "0"); // Proxies.

        HttpContext.Current.Response.AppendHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.
        HttpContext.Current.Response.AppendHeader("Pragma", "no-cache"); // HTTP 1.0.
        HttpContext.Current.Response.AppendHeader("Expires", "0"); // Proxies.

        if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
        {
            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");
            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Content-Range, Content-Disposition, Content-Description");
            HttpContext.Current.Response.End();
        }
    }
}

martes, 7 de enero de 2014

Start with nodejs server

var http = require('http');
var mime = require('mime');
var url = require('url');
var fs = require('fs');
var _base = './';

http.createServer(function(req, res){
 pathname = _base + req.url;
 console.log('path name: ' + pathname);
 fs.stat(pathname, function(err, stats){
  if(err){
   res.writeHead(404);
   res.write('Bad request 404 \n');
   res.end();
  }else if(stats.isFile()){
   var type = mime.lookup(pathname);
   console.log('mime-type: ' + type);
   res.setHeader('Content-Type', type);
   res.statusCode = 200;
   var file = fs.createReadStream(pathname);
   file.on('open', function(data){
    file.pipe(res);
   });
   file.on('error', function(err){
    console.log('error open the file: ' + err);
   })
  }else{
   res.writeHead(403);
   res.write('Directory access is forbidden');
   res.end();
  }
 });
}).listen(1024);
console.log('server running on 1024');