martes, 26 de mayo de 2015

El Libro del Día: AngularJS by Example

El Libro del Día: 2015-05-26

Titulo: AngularJS by Example
Autor: Chandermani
Editorial: Packt
Nro Paginas: 454

Capítulos:
Chapter 1: Getting Started
Chapter 2: Building Our First App – 7 Minute Workout
Chapter 3: More AngularJS Goodness for 7 Minute Workout
Chapter 4: Building Personal Trainer
Chapter 5: Adding Data Persistence to Personal Trainer
Chapter 6: Working with Directives
Chapter 7: Testing the AngularJS App
Chapter 8: Handling Common Scenarios

Descarga:
AngularJS_by_Example

lunes, 25 de mayo de 2015

El Libro del Día: Expert TSQL Window Functions in SQL Server

El Libro del Día: 2015-05-25

Titulo: Expert TSQL Window Functions in SQL Server
Autor: Kathi Kellenberger, Clayton Groom
Editorial: Apress
Nro Paginas: 140

Capítulos:
Chapter 1: Looking Through the Window
Chapter 2: Discovering Ranking Functions
Chapter 3: Summarizing with Window Aggregates
Chapter 4: Tuning for Better Performance
Chapter 5: Calculating Running and Moving Aggregates
Chapter 6: Adding Frames to the Window
Chapter 7: Taking a Peek at Another Row
Chapter 8: Understanding Statistical Functions
Chapter 9: Time Range Calculations and Trends

Descarga:
Expert_TSQL_Window_Functions_in_SQL_Server

viernes, 22 de mayo de 2015

El Libro del Día: Microsoft Azure SQL Database Step by Step

El Libro del Día: 2015-05-22

Titulo: Microsoft Azure SQL Database Step by Step
Autor: Leonard G. Lobel, Eric D. Boyd
Editorial: Microsoft Press
Nro Paginas: 401

Capítulos:
CHAPTER 1 Getting started with Microsoft Azure SQL Database
CHAPTER 2 Configuration and pricing
CHAPTER 3 Differences between SQL Server and Microsoft Azure SQL Database
CHAPTER 4 Migrating databases
CHAPTER 5 Security and backup
CHAPTER 6 Cloud reporting
CHAPTER 7 Microsoft Azure SQL Data Sync
CHAPTER 8 Designing and tuning for scalability and high performance
CHAPTER 9 Monitoring and management
CHAPTER 10 Building cloud solutions

Descarga:
Microsoft_Azure_SQL_Database_Step_by_Step

jueves, 21 de mayo de 2015

El Libro del Día: Custom SharePoint Solutions with HTML and JavaScript

El Libro del Día: 2015-05-21

Titulo: Custom SharePoint Solutions with HTML and JavaScript
Autor: Brandon Atkinson
Editorial: Apress
Nro Paginas: 240

Capítulos:
Chapter 1: Setting Up Your Environment
Chapter 2: Introduction to SharePoint Front-End Solutions
Chapter 3: Content Editor and Script Editor Web Parts
Chapter 4: Building Custom Web Parts Using HTML and JavaScript
Chapter 5: Using JSLink to Style List Web Parts
Chapter 6: Content Search Web Part and Display Templates
Chapter 7: Working with SharePoint’s Built-in JavaScript Functions and Properties
Chapter 8: Working with the JavaScript Object Model
Chapter 9: Working with the REST API

Descarga:
Custom_SharePoint_Solutions_HTML_JavaScript

miércoles, 20 de mayo de 2015

El Libro del Día: SignalR Blueprints

El Libro del Día: 2015-05-20

Titulo: SignalR Blueprints
Autor: Einar Ingebrigtsen
Editorial: Packt
Nro Paginas: 244

Capítulos:
Chapter 1: The Primer
Chapter 2: Overheating the Discussion
Chapter 3: Extra! Extra! Read All About It!
Chapter 4: Can You Measure It?
Chapter 5: What Line of Business Are You In?
Chapter 6: An Architectural Taste
Chapter 7: The Three Screens - Mobile First
Chapter 8: Putting the X in .NET - Xamarin
Chapter 9: Debugging or Troubleshooting
Chapter 10: Hosting and Deployment

Descarga:
SignalR_Blueprints

martes, 19 de mayo de 2015

El Demo del Día: Obtener el Tipo de Cambio de la Pagina de la SUNAT

Obtener el Tipo de Cambio de la Pagina de la SUNAT

Después de mucho tiempo de no publicar un Demo, vamos a compartir un pedido de una persona que necesitaba ingresar a la pagina de la SUNAT diariamente y copiar el tipo de cambio para ingresarlo en su sistema, lo ideal seria tener un Servicio Web de uso libre, pero ante la falta de este o si existe necesitaríamos las credenciales, les dejo esta solución.

Requerimiento

Obtener diariamente en forma automática el tipo de cambio de la SUNAT publicado en la siguiente dirección: http://www.sunat.gob.pe/cl-at-ittipcam/tcS01Alias

Solución

Vamos a crear una aplicación Windows en C# .NET que use la clase HttpClient para hacer una llamada asíncrona a la dirección de la pagina, bajarla a memoria, crear una cadena con el contenido y extraer la parte que corresponde al tipo de cambio del ultimo día: las 3 ultimas celdas de la ultima fila de la segunda tabla de la página.

Restricción

La aplicación podría fallar en un futuro si la SUNAT cambia su contenido agregando o quitando tablas y habría que hacer los ajustes respectivos al código, pero aun así es una ayuda por mientras.

Crear una Aplicación Windows Forms en C#

Abrir Visual Studio y crear una aplicación Windows Forms en C# llamada "TipoCambioSUNAT", cambiarle de nombre al formulario por "frmSUNAT" y realizar el diseño similar a la figura mostrada:


Hacer una referencia a la librería: "System.Net.Http.dll" y escribir el siguiente código en el formulario:

using System;
using System.Collections.Generic; //List
using System.Text; //Encoding
using System.Windows.Forms;
using System.Net.Http; //HttpClient, HttpResponseMessage
using System.IO; //File, MemoryStream

namespace TipoCambioSUNAT
{
    public partial class frmSUNAT : Form
    {
        public frmSUNAT()
        {
            InitializeComponent();
        }

        private void btnConsultar_Click(object sender, EventArgs e)
        {
            HttpClient cliente = new HttpClient();
            cliente.BaseAddress = new Uri("http://www.sunat.gob.pe/");
            HttpResponseMessage rpta = cliente.GetAsync("cl-at-ittipcam/tcS01Alias").Result;
            if (rpta != null&&rpta.IsSuccessStatusCode)
            {
                string contenido = "";
                using(MemoryStream ms= (MemoryStream)
                rpta.Content.ReadAsStreamAsync().Result)
                {
                    byte[] buffer = ms.ToArray();
                    contenido = Encoding.UTF8.GetString(buffer);
                    contenido = contenido.ToLower();
                }
                if (contenido.Length > 0)
                {
                    File.WriteAllText("Sunat.txt", contenido);
                    int posInicioT1= contenido.IndexOf("<table");
                    int posFinT1 = contenido.IndexOf("</table");                  
                    if (posInicioT1 > -1&&posFinT1>-1)
                    {
                        int posInicioT2 = contenido.IndexOf("<table",posInicioT1+1);
                        int posFinT2 = contenido.IndexOf("</table", posFinT1 + 1);
                        string tabla = contenido.Substring(posInicioT2, posFinT2 - posInicioT2 + 8);
                        File.WriteAllText("Tabla.txt", tabla);
                        posInicioT1 = 0;
                        tabla = tabla.Replace("</strong>", "");
                        List<string> valores = new List<string>();
                        for (int i = 1; i < 4; i++)
                        {
                            posInicioT1 = tabla.LastIndexOf("</td");
                            if (posInicioT1 > -1)
                            {
                                tabla = tabla.Substring(0,posInicioT1).Trim();
                                posFinT1 = tabla.LastIndexOf(">");
                                if (posFinT1 > -1)
                                {
                                    valores.Add(tabla.Substring(posFinT1 + 1,
                                    tabla.Length - posFinT1 -1).Trim());
                                }
                            }
                        }
                        if (valores.Count > 0)
                        {
                            txtVenta.Text = valores[0];
                            txtCompra.Text = valores[1];
                            txtFecha.Text = valores[2];
                        }
                    }                  
                }
            }
        }
    }
}

Nota: En este Demo los datos de la consulta van a 3 TextBoxs pero pueden ir directamente a la BD en vez de mostrarse en pantalla.

Ejecutar y Probar la Aplicación Windows Forms

Grabar la aplicación y pulsar F5 para ejecutarla, luego clic al botón "Consultar" y se mostrarán los datos del último día, el tipo de cambio compra y el tipo de cambio venta, tal como se muestra en la siguiente figura:


Comentarios

Muchas veces queremos obtener un cierto dato de una pagina y tenemos que entrar manualmente, en este post hemos visto como usando el método "GetAsync" de la clase "HttpClient" del Namespace "System.Net.Http" podemos hacerlo vía código.

En este caso lo hemos aplicado a la pagina de la SUNAT, la cual se analizó su HTML y se observó que tiene varias tablas, la segunda corresponde a la tabla con los datos de los tipos de cambios de la última semana, para lo cual se obtuvo la última fila y de esta las 3 últimas celdas.

No se hizo mediante XML porque el HTML estaba mal formado y no generaría bien el XML DOM, es por eso que trabajamos con texto puro y substrings.

Este Demo lo terminamos en colaboración con mis alumnos del Taller de los Domingos a los cuales les doy las gracias por estar siempre pendientes de aprender y querer mejorar.

Si alguien de ustedes tiene alguna URL de algún Servicio Web que consideren útil compartir por ejemplo el del Tipo de Cambio o Consulta de RUC de la SUNAT, DNI de la RENIEC, ONPE, etc. seria bueno hacer otro post pero ya usando Servicios.

Descarga del Código
DemoDia_TipoCambioSUNAT

El Libro del Día: Adaptive Code via C#

El Libro del Día: 2015-05-19

Titulo: Adaptive Code via C#
Autor: Gary McLean Hall
Editorial: Microsoft Press
Nro Paginas: 433

Capítulos:
PART I AN AGILE FOUNDATION
CHAPTER 1 Introduction to Scrum
CHAPTER 2 Dependencies and layering
CHAPTER 3 Interfaces and design patterns
CHAPTER 4 Unit testing and refactoring
PART II WRITING SOLID CODE
CHAPTER 5 The single responsibility principle
CHAPTER 6 The open/closed principle
CHAPTER 7 The Liskov substitution principle
CHAPTER 8 Interface segregation
CHAPTER 9 Dependency injection
PART III ADAPTIVE SAMPLE
CHAPTER 10 Adaptive sample: Introduction
CHAPTER 11 Adaptive sample: Sprint 1
CHAPTER 12 Adaptive sample: Sprint 2
Appendix A: Adaptive tools
Appendix B: GitHub code samples

Descarga:
Adaptive_Code_via_C#