Cymen's Blog

Author Archive

Day three at 8th Light

leave a comment

I completed the koans yesterday however I wasn’t happy with a few solutions. I reviewed one of these with Doug and we tried to come up with a cleaner solution however it didn’t really seem possible. Or at least nothing cleaner came from either of our minds. This is normally where I move on and let the subconscious take a crack at it.

The next assignment is to work on a ruby implementation of tic tac toe that is:

  • fully tested
  • uses classes that follow the single responsibility principle (SRP)
  • can be played on the console
  • can choose who goes first (computer or human)
  • uses MiniMax algorithm for the computer player

Written by Cymen

February 3rd, 2012 at 11:53 am

Posted in 8th Light,Residence

Second day at 8th Light

leave a comment

Today I’m a bit off! I got in a bit later than planned and fumbled the morning greeting. I think everyone is supposed to say hi to each other. I like that. But I felt shy and wasn’t sure if I should interrupt and say hi. Tomorrow I’ll get it right!

I’m continuing to work on the ruby koans. Right now, I’m at 159/280 after just finishing figuring out how to raise the exceptions in triangle.rb when the triangle lengths are invalid. I ended up going to the stackoverflow link mentioned in about_triangle_project_2.rb. This is going a bit slower than I expected however I definitely see the value as I’ll be coming back to this when I hit the corner cases in ruby and can’t remember the correct behavior.

Written by Cymen

February 2nd, 2012 at 2:11 pm

Posted in 8th Light,Residence

First day at 8th Light

leave a comment

I started as a resident today at 8th Light under Doug Bradbury. The day started with some quick paperwork, siting in on a weekly project meeting and then starting to work on these ruby koans. Doug brought me a sub from a local indie sub shop (can’t remember the name off hand but I’ll be back update: JP Graziano) and then I continued in the afternoon with the ruby koans. We also went over the reading list: continue the overview parts of Agile Software Development while skipping the case studies unless interested and start with Clean Code.

It turns out we have a slight fruit fly problem in the downtown office. As we have one at home too I went out and brought some apple cider vinegar and some cups and have placed them around the office. The trick I’m using at home is to put a tiny bit of dish soap in to act as a surfactant. A surfactant lowers the surface tension of a liquid which means the fruit flies can’t “walk on water” so to speak and will fall in and drown when they attempt to drink the vinegar. The sharp smell may also attract them.

Written by Cymen

February 1st, 2012 at 2:46 pm

Posted in 8th Light,Residence

Reading List for 8th Light Fellowship

leave a comment

I’ve created a list at Amazon.com for the books to be used during my fellowship at 8th light: 8th Light – Book List.

Written by Cymen

January 26th, 2012 at 3:47 pm

Posted in 8th Light,Residence

Transition: Webitects to 8th Light

leave a comment

I’m starting an 8th Light fellowship with Doug Bradbury on February 1st, 2012! I am very excited to learn more about test driven development, software design and agile methodology by practice as an apprentice.

I am going to miss the team at Webitects. For the past two years, I’ve worked on a number of interesting and challenging projects. It has been a really good experience and I will definitely stay in touch.

Written by Cymen

January 4th, 2012 at 10:36 am

Posted in 8th Light,Residence

ASP.NET MVC and DropDownList: One approach…

leave a comment

One thing that I’m not particularly fond of in ASP.NET MVC is figuring out where to put additional data one needs for views. If one is using a “model per view” approach it is simple — stick it in that model. Otherwise you can choose to add it to a model that won’t always need it or pass it via ViewBag/ViewState. I currently lean towards the last option.

The other annoyance is how best to transform a list of objects into a IEnumerable<SelectListItem> collection cleanly with the special case of handling a default value. I have started to approach this with extension methods and am very happy with how it is working.

Transforming your List<MyObject> to IEnumerable<SelectListItem>

Our current data tier has a manager class for each object type. When that object type is going to be used in a select list, I add an extension to IEnumerable<MyObject> like so:

    public static class MyObjectManagerExtensions
    {
        public static IEnumerable AsSelectList(this IEnumerable list, int? value)
        {
            return (from item in list
                    select new SelectListItem
                    {
                        Selected = value.HasValue && item.Id == value.Value,
                        Text = item.Name,
                        Value = item.Id.ToString()
                    });
        }
    }

To support the option of a default value, I have an extension method that applies to IEnumerable:

    public static class SelectListExtension
    {
        public static IEnumerable WithDefault(this IEnumerable list, string defaultLabel = "Select one...", string value = "")
        {
            return (new[] { new SelectListItem { Text = defaultLabel, Value = value } }).Concat(list);
        }
    }

Here is an example of an actually call to this methods in:

ViewBag.MyObject = myObjectManager
                       .GetAllMyObjectBy(isForFoo: true, orderBy: MyObjectColumns.Name)
                       .AsSelectList(myObjectId)
                       .WithDefault();

Written by Cymen

November 18th, 2011 at 8:55 am

Posted in ASP.NET MVC

SSL Certificates, OCSP and CRLs: How to troubleshoot

leave a comment

Recently, I ran into an interesting problem: a website that my workplace was going to start hosting was about to be cut over however the SSL certificate was failing. By disabling OCSP in Firefox, I determined it was likely due to the certificate authority (GoDaddy) revoking the certificate.

The warning displayed by Google Chrome is:

The server’s security certificate is revoked!
You attempted to reach www.xyz.org, but the certificate that the server presented has been revoked by its issuer. This means that the security credentials the server presented absolutely should not be trusted. You may be communicating with an attacker. You should not proceed.

The warning displayed by Mozilla Firefox is:

Secure Connection Failed

An error occurred during a connection to www.xyz.org.
Peer’s Certificate has been revoked.

(Error code: sec_error_revoked_certificate)

  • The page you are trying to view can not be shown because the authenticity of the received data could not be verified.
  • Please contact the web site owners to inform them of this problem. Alternatively, use the command found in the help menu to report this broken site.

At first, we thought there was perhaps an issue with the intermediary certificate bundle. However that bundle matched GoDaddy’s current bundle so it was time to take a closer look at the certificate. Firefox wasn’t much help however Chrome could display all the metadata attached to the failing certificate. The two essential parts for to check the status for OCSP are the serial number and the CRL list.

The CRL list was located at http://crl.godaddy.com/gds1-18.crl and I was able to retrieve this file however I couldn’t find a decent way to view a CRL file on Windows in a searchable way quickly. I found that openssl can dump a CRL to text and I did so with the GoDaddy CRL using this command:

openssl crl -inform DER -in gds1-58.crl -text > list.txt

I then opened up list.txt in a text editor and was able to find a revocation entry similar to this one:

    Serial Number: 049A85BD85BE83
        Revocation Date: Oct 26 18:56:06 2011 GMT
        CRL entry extensions:
            X509v3 CRL Reason Code:
                Cessation Of Operation

Of course, if you have login access to the registrar for the certificate it may be quicker to simply login and see if there is some sort of mention on the account about the failing certificate. But if you need to determine why a certificate is failing OCSP hopefully the above will help. I suspect there are additional CRL to check however I’m not sure how to determine what they are.

Written by Cymen

November 17th, 2011 at 9:07 am

Posted in Internet

jQuery JCrop plugin, Chrome/Webkit: fails to initialize after the first attempt

leave a comment

I recently ran into a bug when using recent versions of Chrome and the jQuery JCrop plugin. I am adding some HTML to a jQuery UI modal window that contains the image I want to crop. I am initializing JCrop using the onload event of the image within this HTML. That worked fine in the past but stopped working sometime in the past year or so.

According to issue 7731 on chromium, the cause of this problem is that Webkit is more strict — the onload event is only triggered the first time the image is loaded. So the issue was the onload event triggered immediately when the modal window was shown — it triggered before the image was displayed (except for the first time). This is a tricky thing to work around. I verified that this was my issue by appending to the image URL the current timestamp (so + ‘?’ + new Date().getTime()) in order to force the browser to reload the image each time. That did fix the problem but it also introduced UI lag as the image had to be fetched each time a crop was attempted.

I put in this short term fix: put my JCrop initialization code in a function named crop and then (still bound to the load event), attempt to initialize it. If the image isn’t loaded, try again in 25ms up to 5 times. I can tell if the image is loaded by checking for a height > 0. The code:

    $('#cropbox').load(function (event) {
        var boxHeight = Math.floor($('#crop').closest('.ui-dialog').height() * 0.8);

        var $img = $(event.target);
        var productDiv = $('div#' + id);
        var sku = $('div.sku', productDiv).text();
        var product = productsData[sku];
        var height, width;

        // put crop loading code into function -- see comment below about chrome hack
        var crop = function () {
            height = $img.height();
            width = $img.width();

            var jcrop = $.Jcrop(
                ...
                });

            ...
        }

        // Ugly hack for chrome -- the load event triggers before the image is actually displayed/in DOM
        // but only on crop attempts after the initial one. One way to detect this is to check if the image
        // height is 0 -- if so, retry.
        // Update: issue probably this: http://code.google.com/p/chromium/issues/detail?id=7731#c12

        var worked = false;
        var attempts = 0;
        var attempt = function () {
            if (worked) return;

            height = $img.height();
            if (typeof height === 'number' && height > 0) {
                crop();
                worked = true;
            }
            else {
                attempts++;
                if (attempts < 5) {
                    // try again in 25 milliseconds
                    setTimeout(attempt, 25);
                }
                else {
                    alert('Bug with cropping image.');
                }
            }
        }

        attempt();
    });

I reported this problem in issue 63 for the JCrop plugin. Hopefully, there is a better way to do this however if you need a quick work around now...

Written by Cymen

November 10th, 2011 at 10:35 am

SQL Server – Ranking names for search results by position of query within name

leave a comment

SQL Server using PATINDEX() and LEFT()

When searching names there are some assumptions we can make (based on first and last name being in separate columns):

  • A match in the last name is more important than a match in the first name
  • The position of the match within the last name is important: an earlier match is a better match
  • The first name should still be search
  • If present, a middle name is least important

It is possible to do this with SQL Server using the following proprietary extensions:

  • PATINDEX(needle, haystack): returns position of needle within haystack and (unfortunately in our use case) 0 if not present.
  • LEFT(string, count): returns substring of string up to length of count (note: will truncate string if length greater than count!)
SELECT TOP 10 firstName + ' ' + middleName + ' ' + lastName
FROM Member
WHERE [firstName] + ' ' + [middleName] + ' ' + [lastName] LIKE @query
ORDER BY
  PATINDEX (
    @query,
    LEFT([lastName] + '                                                                                          ', 90)
    + LEFT([firstName] + '                                                                                          ', 90)
    + [middleName]
  ),
  [lastName],
  [firstName],
  [middleName]
  -- Note: the ' .... ' above is a string of spaces of length 90

We are making a big assumption: none of the name fields will have a length > 90. You may need to adjust this value for your use case. The reason we need to do this is that PATINDEX() will return 0 if the value is not present so we can’t simply due a nice ORDERBY PATINDEX(@query, lastName), PATINDEX(@query, firstName), PATINDEX(@query, middleName). Instead, we have to concatenate the name fields into one long string but pad them so that variable length of the names will not affect the rank they are put in.

MySQL using LOCATE() and LEFT()

The same method should work in MySQL using the LOCATE() and LEFT() functions. Both appear to be identical in usage to the SQL Server functions.

Written by Cymen

October 19th, 2011 at 11:23 am

Mime types for ASP.NET

leave a comment

One of the annoyances working on the Windows/IIS stack is that getting mime types is a pain. They are located in multiple places and there is no really ideal “best practice” method to get mime types without what I consider overly-complicated solutions. In light of this observation I wrote a basic C# program that fetches the mime.types file from the Apache project and converts it to a C# Dictionary keyed by file extension. It is a basic program but might be useful for others wondering why in the world this is so complicated.

ApacheMimeTypesToDotNet on github

The output looks like this: ApacheMimeTypes.cs

using System;
using System.Collections.Generic;

namespace ApacheMimeTypes
{
	class Apache
	{
		public static Dictionary MimeTypes = new Dictionary
		{
			{ "123", "application/vnd.lotus-1-2-3" },
			{ "3dml", "text/vnd.in3d.3dml" },
			{ "3g2", "video/3gpp2" },
			{ "3gp", "video/3gpp" },
			{ "7z", "application/x-7z-compressed" },
			{ "aab", "application/x-authorware-bin" },
			{ "aac", "audio/x-aac" },
			{ "aam", "application/x-authorware-map" },
			{ "aas", "application/x-authorware-seg" },
			{ "abw", "application/x-abiword" },
			{ "ac", "application/pkix-attr-cert" },
			{ "acc", "application/vnd.americandynamics.acc" },
			{ "ace", "application/x-ace-compressed" },
			{ "acu", "application/vnd.acucobol" },
			{ "acutc", "application/vnd.acucorp" },
			{ "adp", "audio/adpcm" },
			{ "aep", "application/vnd.audiograph" },
			{ "afm", "application/x-font-type1" },
			{ "afp", "application/vnd.ibm.modcap" },
			{ "ahead", "application/vnd.ahead.space" },
			{ "ai", "application/postscript" },
			{ "aif", "audio/x-aiff" },
			{ "aifc", "audio/x-aiff" },
			{ "aiff", "audio/x-aiff" },
			{ "air", "application/vnd.adobe.air-application-installer-package+zip" },
			{ "ait", "application/vnd.dvb.ait" },
			{ "ami", "application/vnd.amiga.ami" },
			{ "apk", "application/vnd.android.package-archive" },
			{ "application", "application/x-ms-application" },
			{ "apr", "application/vnd.lotus-approach" },
			{ "asc", "application/pgp-signature" },
			{ "asf", "video/x-ms-asf" },
			{ "asm", "text/x-asm" },
			{ "aso", "application/vnd.accpac.simply.aso" },
			{ "asx", "video/x-ms-asf" },
			{ "atc", "application/vnd.acucorp" },
			{ "atom", "application/atom+xml" },
			{ "atomcat", "application/atomcat+xml" },
			{ "atomsvc", "application/atomsvc+xml" },
			{ "atx", "application/vnd.antix.game-component" },
			{ "au", "audio/basic" },
			{ "avi", "video/x-msvideo" },
			{ "aw", "application/applixware" },
			{ "azf", "application/vnd.airzip.filesecure.azf" },
			{ "azs", "application/vnd.airzip.filesecure.azs" },
			{ "azw", "application/vnd.amazon.ebook" },
			{ "bat", "application/x-msdownload" },
			{ "bcpio", "application/x-bcpio" },
			{ "bdf", "application/x-font-bdf" },
			{ "bdm", "application/vnd.syncml.dm+wbxml" },
			{ "bed", "application/vnd.realvnc.bed" },
			{ "bh2", "application/vnd.fujitsu.oasysprs" },
			{ "bin", "application/octet-stream" },
			{ "bmi", "application/vnd.bmi" },
			{ "bmp", "image/bmp" },
			{ "book", "application/vnd.framemaker" },
			{ "box", "application/vnd.previewsystems.box" },
			{ "boz", "application/x-bzip2" },
			{ "bpk", "application/octet-stream" },
			{ "btif", "image/prs.btif" },
			{ "bz", "application/x-bzip" },
			{ "bz2", "application/x-bzip2" },
			{ "c", "text/x-c" },
			{ "c11amc", "application/vnd.cluetrust.cartomobile-config" },
			{ "c11amz", "application/vnd.cluetrust.cartomobile-config-pkg" },
			{ "c4d", "application/vnd.clonk.c4group" },
			{ "c4f", "application/vnd.clonk.c4group" },
			{ "c4g", "application/vnd.clonk.c4group" },
			{ "c4p", "application/vnd.clonk.c4group" },
			{ "c4u", "application/vnd.clonk.c4group" },
			{ "cab", "application/vnd.ms-cab-compressed" },
			{ "car", "application/vnd.curl.car" },
			{ "cat", "application/vnd.ms-pki.seccat" },
			{ "cc", "text/x-c" },
			{ "cct", "application/x-director" },
			{ "ccxml", "application/ccxml+xml" },
			...
		};
	}
}

Written by Cymen

September 14th, 2011 at 12:28 pm