Participating in the White House’s Summer Jobs+ Code Sprint

Two weeks ago I read about the Code Sprint being put forth by the White House asking developers to write cool applications demonstrating their new Summer Jobs+ API. The sprint officially ends tomorrow, April 16th, but regretfully I won’t be submitting an application. This post is a story about the application would have been if I did have a submission.

The Summer Jobs+ Code Sprint

Two weeks ago, I read the post on the White House’s blog challenging developers to participate in the code sprint. And it’s not like I make a habit of reading a blog about the residence of the first family and looking for programmer stuff–I had been directed there by an article about the code sprint from the Associated Press.

My coworkers helped me brainstorm the initial idea for the proejct, which was to connect the job listings from the Summer Jobs+ Bank with the NYC public transportation information available through NYC OpenData. I wanted to make the assumption that there would be youths that would not have access to a car and needed information about job listings that would be “within walking distance of your house” or “this is the bus route you would take”.

I know it seems a little weird for a dude based out of Kansas City, Missouri who has never even been to New York to select this kind of project. However, I was already aware of NYC’s efforts to provide a great deal of city information to the public through the OpenData project, including up-to-date statistics on bus and subway routes. New York City seemed like a good city to set up a demonstration and, if the idea had traction, more cities could be added over time.

The idea for the site was meant to be simple. You would enter your address and a keyword or two about the kind of job you were looking for. The server would pull the matching listings from the Summer Jobs+ Bank and annotate them with recommendations about how to reach the listing based on NYC’s OpenData information.

The working title for the application was NY Jobs+.

Designing my Application

I set out to write a demo application to see what it would look like. This gave me an opportunity to practice jQuery and JavaScript, as I do very little front-end web development at work. I also took the opportunity to try out PHP. Even though I work professionally in a Microsoft stack, I chose PHP for the demo because I wanted to learn something new and I wanted the final result to be a site I could easily zip up and email to somebody without a lot of fuss about deployments. The code sprint lasted for two weeks (it had been extended from one and I feel that even two was too short), but there were only four days that I worked on it in, the evenings after I got off of work.

The goal of the first evening was to authorize successfully and be able to make a basic request and print out the raw JSON to the screen. I didn’t anticipate much trouble implementing their authorization scheme, as it is similar to APIs I’ve integrated with at work. Though I was certain that I had followed the specification to the letter, I was always getting error responses from the server. Exhausted from debugging the issue, I emailed the site administrator. Turns out the API had been taken off-line for an update.

The next day, my site came to life because the API was back online. The Department of Labor’s documentation is well-formatted when it comes to formatting the request, but there is absolutely nothing about the payload you get back. So the second evening was spent figuring out how to parse the resulting JSON for some pieces of information that I wanted to display and render it on my page as a table.

By the end of the third evening, I had a style sheet and could search for jobs in New York based on a keyword entered by the user, displaying the results in a template-driven accordion widget using jQuery UI.

On the fourth day, I wanted to render the location of the job listing on a map. That was, unfortunately, when the whole thing began falling apart. While the schema for the job listings states that the jobLocation is a required field, the actual data only provides the City, State, and Zip Code. No street address. The data wasn’t actually there. The entire premise of the application was not achievable.

Web application screenshot

Retrospective

What could I have done differently? Surely more due diligence while requirements gathering to confirm that the information I needed would be present. But in my own defense, the schema does contain entries for street addresses. Heck, the schema has entries for GPS coordinates!

Even though I didn’t accomplish the goal I wanted to with this project, it’s not a complete loss. At work, I mostly deal with server-side code in C#. So I got a chance to refresh my memory on web development and I got the chance to explore technologies I’m not as familiar with, such as jQuery and PHP.

I’m making the source code available as an attachment at the bottom of this post.

To run it, you’ll need a web server that supports PHP (The built-in server, started from the command-line switch, “php.exe -S localhost:80” works just fine for testing) and to enable the cURL extension in your PHP configuration file if it isn’t already enabled.

Attached Source Code

NY Jobs Plus


Toying with DynamicObject: Reading XML

I’ve always been interested in using language tools in ways that can foster communication. One such tool which I’ve been looking for excuses to apply at work has been Dynamic Objects. DynamicObject is a class introduced in .Net 4.0 that lets you specify dynamic behavior for run-time.

Communication in .Net 4

The new dynamic features adds a lot of behavior to C# which I’d previously looked to languages like Boo for. I’ll get into some of the pros and cons that go along with this style in a moment. First, let’s look at some XML.

<?xml version="1.0" encoding="utf-8" ?>
<file>
  <header>
    <title>Welcome</title>
  </header>
  <message>
    <body>
      Hello, World!
    </body>
    <parameter>Param1</parameter>
    <parameter>Param2</parameter>
    <parameter>Param3</parameter>
  </message>
</file>

This is not a very sophisticated document. If it looks like I threw it together in a minute, well, that is precisely what I did! We can assume this is not an XML document which is going to see a lot of future development added around it in the future, but I can still achieve expressive code and minimal syntax in C# using dynamics.

Exploring DynamicObject

The syntax I want is to access the elements of the document as properties but without making it strongly typed. For instance,

var body = file.message.body;

I can derive DynamicObject and override the TryGetMember (which is called when the dynamic object tries to access a property) so that it looks at the XElement’s descendants for elements with a matching name. The first thing I need to do is create the dynamic object using an XElement as the source.

/// <summary>
/// A class for processing XML nodes using dynamics.
/// </summary>
public class DynamicXElement : DynamicObject
{
    #region Constructor

    public DynamicXElement(XElement element)
    {
        this.element = element;
    }

    #endregion

    #region Fields

    private readonly XElement element;

    #endregion

I simply use the constructor to feed an XElement to my dynamic object. Afterwards, I need to override TryGetMember to implement the custom behavior.

    #region Overrides

    public override bool TryGetMember(GetMemberBinder binder, out object result)
    {
        var elements = this.element.Descendants(binder.Name);
        int count = elements.Count();

        if (count == 0)
        {
            result = this.GetElementMember(binder.Name);
            return true;
        }

        if (count == 1)
        {
            result = new DynamicXElement(elements.First());
            return true;
        }

        result = new List<DynamicXElement>(elements.Select(x => new DynamicXElement(x)));
        return true;
    }

    /// <summary>
    /// For clarity when working with the element.
    /// </summary>
    public override string ToString()
    {
        return this.element.ToString();
    }

    #endregion

Two things I want to note: if zero elements are returned I try to resolve the property by calling a member on the XElement object itself (will show that code in a moment) and if more than one element is returned then I copy everything over to a new list of dynamic elements. When resolving a property to the XElement class, I knew that I could use Reflection to get the member and invoke it, but I preferred to explore the technique while sticking with the runtime binding features introduced in .Net 4.0. Instead of invoking a member, the runtime binder is more about targeting a call site.

    /// <summary>
    /// If a member isn't a descendant of the node, we can attempt to find
    /// a member with the same name on the underlying XElement element.
    /// </summary>
    private object GetElementMember(string memberName)
    {
        var elementBinder =
            Binder.GetMember(
                CSharpBinderFlags.None,
                memberName,
                typeof(XElement),
                new[] { CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null) });

        var callsite = CallSite<Func<CallSite, XElement, object>>.Create(elementBinder);
        return callsite.Target(callsite, this.element);
    }

Harnessing code like this, we’ve definitely peeled back the cover of how the runtime binder operates, but I would avoid dipping into this class too often. It’s not intended for application code and may be more susceptible to breaking changes with new releases as a result.

Reading the XML Document with Dynamics

The DynamicXElement class is written, but what has it achieved? It doesn’t do anything we couldn’t already do with XElement, but from the application code perspective, the syntax is much cleaner. However, we’ve lost the compile-time checking we would have had if we had generated an XSD and a class for the document.

Enough of the DynamicXElement class has been written to allow us to read data out of the file. The following sample demonstrates the usage:

    class Program
    {
        static void Main()
        {
            var doc = XDocument.Load("Sample.xml");
            dynamic file = new DynamicXElement(doc.Root);

            Console.WriteLine("ToString():");
            Console.WriteLine(file.ToString());

            Console.WriteLine("Title:");
            Console.WriteLine(file.header.title.Value);

            Console.WriteLine("Body:");
            Console.WriteLine(file.message.body.Value);

            Console.WriteLine("Parameters:");
            Console.WriteLine(file.message.parameter.Count);

            foreach (dynamic parameter in file.message.parameter)
                Console.WriteLine(parameter.Value);

            Console.Read();
        }
    }

When executed, the program returns the following console output:

ToString():
<file>
  <header>
    <title>Welcome</title>
  </header>
  <message>
    <body>
      Hello, World!
    </body>
    <parameter>Param1</parameter>
    <parameter>Param2</parameter>
    <parameter>Param3</parameter>
  </message>
</file>
Title:
Welcome
Body:

      Hello, World!

Parameters:
3
Param1
Param2
Param3

In closing, I could see myself using something like this on the job if I had to parse an XML response from a web service with an inconsistent, noisy, or poorly defined schema. Especially if I had several web methods to call and each one had a unique schema for every request, response, and/or possible error messages. JSON web services, in particular, tend to evolve the response based on how you format the request. Dynamics are a cool concept, but they haven’t revolutionized the way I code yet.


Using Abstract Override to require a readable ToString Method

If you have a type that you know is going to show up in a log file, an easy way to ensure the instance will produce useful output to the logs is to override ToString() and put the results in the log. But what if you’re responsible for the base class and someone else may be writing the implementation? You can’t force them to override ToString(), can you?

public abstract class Customer : ICustomer
{
    public abstract override string ToString();
}

public class Person : Customer
{
    public string FirstName { get; set; }
    public string LastName { get; set; }

    public override string ToString()
    {
        return LastName + ", " + FirstName;
    }
}

public class Organization : Customer
{
    public string Name { get; set; }
    public bool IsNonProfit { get; set; }

    public override string ToString()
    {
        string organizationType = IsNonProfit ? "non-profit" : "for profit";
        return Name + ", a " + organizationType + " organization";
    }
}

What I like about this solution is that the code communicates the message: disregard the default implementation of ToString() and require that the concrete class implement it again. You must name your object instances. In the past, I have created a separate abstract property in my classes to give it a name; such as OperationName, TransactionName, ContactName, etc. I didn’t realize at the time but I was just giving my classes two ways to say the same thing and they weren’t always in tune with each other.