Sponsored by Gluten Free Waffles and Sliced Bananas

Friday, June 24, 2016

Webpack, stylelint, and sass-loader: Fixing block-closing-brace-newline-before and indentation warnings

8:18 PM Posted by Tyson Nero No comments
If you're using stylelint and sass-loader in your webpack.config, you may be running into issues with faulty block-closing-brace-newline-before and indentation warnings being reported and your transpiled CSS looking like the following:
.btn {
   font-size: 12px; }
If this is that case, you are mostly likely dealing with an issue from outputStylenested a default option set on node-sass. This issue can be found on Github: libsass doesn't preserve new lines in nested style - https://github.com/sass/libsass/issues/552
To avoid this, set the outputStyle: expanded option in your webpack.config and your CSS will output normally:
.btn {
   font-size: 12px; 
}
Simply add the following to your webpack.config:
sassLoader: {
  outputStyle: 'expanded'
}
More documentation can be found below if needed:
https://github.com/jtangelder/sass-loader
https://github.com/sass/node-sass 

Tuesday, August 19, 2014

Unit Testing Mongoose Model Pre-validate Hooks (Middleware) using Mocha

8:35 AM Posted by Tyson Nero No comments
Unit testing Mongoose models is a very trivial task in general. Where it is not entirely that difficult to implement, trying to understand the ins and outs is tedious especially when there is little documentation or code examples of such. In this article, I am going to show how you can unit test "pre-validate" hooks on Mongoose models using Mocha and Chai.

Here is the model we will be testing:


This model will have  pre-validate hook for checking the password strength. Note: this code uses the OWASP Password Strength Test plugin. Notice that in this snippet, we are actually invalidating the document (this.invalidate()). This is the key part.


In using the pattern of the pre-validate hook actually invalidating the document, the unit tests become quite easy. In the tests, we will simply call validate() on the model, then check if the model has a populated errors array.

Unit testing Mongoose pre-validate hooks should be as simple as that.

Wednesday, May 28, 2014

How I Got Node.js and MongoDB running on Android

1:44 PM Posted by Tyson Nero , , , 6 comments
This past Memorial Day Weekend, I gave myself a personal challenge (derived from a project at work) of deploying a Node.js application using MongoDB to an Android device. After a quick Google search, I realized that such a task was not a simple one, but rather it was going to take a lot of research, trial and error, and not a lot of sleep.

For starters, neither Node.js or MongoDB have support for Android. Even more frustrating, they both do not have installs for ARM processors. My first step was to figure out if anyone had already installed Node.js on Android, and if so, how they did it. The first article I came across with this claim was by Masashi Katsumata: Node.js on Android. Based on the directions and comments, I believed this strategy would work and devised a plan:
  1. Buy an inexpensive tablet
  2. Root the device
  3. Install Debian
  4. Install Node.js
  5. Install MongoDB
  6. Deploy and run the application
Buy an Inexpensive Tablet

My next step was finding a cheap tablet. My choices at hand were Walmart and Best Buy. After visiting them both, I picked up a $70 Visual Land Prestige Pro 7D from Best Buy. Now, I was ready to rock, but truly didn't realize what I was in store for.
http://www.tabletroms.com/forums/pro-7d/6932-%5Btut%5D-how-root-vl-prestige-pro-7d-easiest-method.html

Root the Device (Visual Land Prestige Pro 7D)

With such an obscure device, there was not a lot of support for figuring out how to root it. I first came across a video claiming the Visual Land tablets come rooted but not advertised as such. I downloaded Root Checker, and quickly realized that was not true. Luckily, TabletRoms.com had a couple forum threads dedicated to rooting my specific device. The first tutorial was using Vroot, however, I would not recommend this method as it's reported to install adware, some Chinese apps, and modify the Super User. The other option was to follow a tutorial using Cydia Impactor. After following the instructions, I ran Root Checker again, and this time, the device was successfully rooted. I also installed several helpful tools listed below which made this whole process much simpler.

Instructions:
  1. Download Cydia Impactor 
  2. Follow rooting instructions here: http://www.tabletroms.com/forums/pro-7d/6932-%5Btut%5D-how-root-vl-prestige-pro-7d-easiest-method.html
Other Recommended Installs:
Install Debian

To install Debian Kit, I first downloaded the Debian Kit app on Google play. This doesn't actually install Debian Kit, but runs pre-checks and supplies the needed files and instructions. From there, I followed a couple of tutorials from the author and successfully installed Debian. To my demise though, I began running out of disk space as I progressed, and needed to re-install using a partitioned SD card instead.

Instructions:
  1. Download Debian Kit app on Google play
  2. Format and partition and SD card giving yourself ample space for the Debian install (recommended)
    1. Here's a good tutorial: How to Partition your Android MicroSD Card
  3. Install Debian Kit (read all)
    1. Basic instructions: http://sven-ola.dyndns.org/repo/debian-kit-en.html
    2. Instructions (includes SD card): http://www.doviak.net/debian-kit/HowTo-SvenOla-DebianKit.pdf
    3. See step 2 here: http://masashi-k.blogspot.com/2013/08/nodejs-on-android.html
Install Node.js

Firstly, there is Android build support starting in Node v0.11.3 (beta):
https://github.com/joyent/node/commit/5e4e8ec429381a8d1eebe31647e9daaaae42c54b

Regardless, versions before 0.11.3 do  support building Node for ARM architecture.

I came across a tutorial using Node Version Manager to install the beta version with Android support: https://gist.github.com/jtenner/79bb5350c318f7623ca7. However, it failed to work on my device most likely because nvm didn't compile for ARM.

Following this tutorial: http://masashi-k.blogspot.com/2013/08/nodejs-on-android.html, I opted to build and install an earlier version of Node. Since I was dealing with an ARM processor, the install had to be configured with special arguments. I also ran into a strange error to do with "blx" requiring minor modification of Node source code. Luckily, the tutorial addressed both problems, and Node.js was finally built and installed.

Instructions:
  1. Follow step 3 here: http://masashi-k.blogspot.com/2013/08/nodejs-on-android.html
    1. Note the steps for ARM architecture
      #> ./configure --without-snapshot --dest-cpu=arm --dest-os=linux
  2. If you get this error "For thumb inter-working we require an architecture which supports blx",  you must customize the macro-assembler-arm.cc file.
    Try either of these options:
    1. http://www.yoovant.com/install-node-0-10-on-arm-based-board/
    2. http://www.yoovant.com/how-install-node-js-0-8-arm-based-sbc-fox-board-g20/

Install MongoDB

This is where the nightmare officially began. Not only can MongoDB not be installed on ARM architecture but it cannot be built for it either. There's an existing issue created in Mongo's official tracker, however it's currently unassigned: https://jira.mongodb.org/browse/SERVER-1811. Reading the comments, however, revealed some clues including several forks of Mongo that supposedly build and install for ARM:
After building and installing the first three above, waiting 3-4 hrs for each, and them failing somewhere in the process, I was ready to give up. Then, I came across one final article: Building MongoDB on Raspberry Pi. With mongo-nonx86, I was able to compile and install flawlessly making my application stack complete.

Instructions:
  1. Read the comments on Mongo's open issue for ARM support
    https://jira.mongodb.org/browse/SERVER-1811
  2. Choose a fork to build and install (follow respective instructions):
    1. https://github.com/RickP/mongopi
    2. https://github.com/wtfuzz/mongo-arm
    3. https://github.com/skrabban/mongo-nonx86 (worked for me)
  3. Or, use these pre-compiled binaries: https://github.com/Barryrowe/mongo-arm

Deploy and Run the Application

Getting the application to the device is rather easy:

  1. Copy the application files to a free partition on the SD Card 
  2. USB transfer
  3. Git clone (if publicly available)

It is recommended to deploy files that were freshly pulled from source control before npm install has been run. Native dependencies are built for the platform during install time. If you copy a native module built on Windows to Linux for instance, you will have issues during run-time.

Once the files are on your device, execute npm install and make sure everything completes.

Finally, fire up your node server making sure any database connections are successful.

Thursday, January 9, 2014

Setting up Google Voicemail on the Nexus 5 (Sprint) to Fix Notifications

8:24 AM Posted by Tyson Nero , , No comments
With the release of the Nexus 5, we have seen a few major issues one of which being that voicemail notifications do not show on the phone after new voicemails have been received. This problem (reported here: http://productforums.google.com/forum/#!topic/nexus/WDTLO-IoJeU%5B126-150-false%5D) seems to be affecting multiple carriers without a solid fix in place.

Some Nexus 5 users have stated that the issue was resolved in the 4.4.1 Android update while others have been able to factory reset their phones to resolve the problem. However, the majority of users have been left in limbo.

One viable option is to allow Google to host your voicemail via Google Voice. If your not familiar with Google Voice, here are a list of features: http://www.google.com/googlevoice/about.html. You can either fully integrate the complete service with your phone, or simply make use of the voicemail features. We are going to walk through setting up voicemail alone using the Sprint network.

Setting Up Google voicemail on the Nexus 5
  1. Login to your Google Voice account (https://www.google.com/voice‎), click the gear icon, and click Settings.
  2. Under the Phones tab, click the Add (another) phone link and walk through the steps as prompted.
  3. Disable call forwarding in case it was already enabled. 
    1. Go back to the Phones tab if you have been navigated away and click the Edit button under your phone.
    2. Beside the Forwarding Options label, click the Deactivate link and follow the instructions within the popup.
  4. Install Google Voice from the Play Store on your Nexus 5 if not already installed.
  5. Open Google Voice on your Nexus 5, and select "skip" if prompted to integrate with your carrier.
  6. Follow the steps to setup Google voicemail, and make sure you select "don't use Google Voice to make any calls" when prompted. 
  7. You will be prompted to configure your phone by dialing a certain code (this is for conditional call forwarding). 
  8. Go back to your Google Voice account and check everything is setup.
Issues with Dial Pad

The dial page should call your Google voicemail when holding down the 1 key. If you experience issues with your dial pad calling your carrier voicemail instead, follow these steps.
  1. Tap the phone icon to open your Dialer. 
  2. Tap the setting icon in the bottom right hand of the screen, and select Settings > Call settings
  3. Under Other Call Settings, select Voicemail
  4. Select Service, and confirm Google Voice is selected
  5. Select Setup then tap the Voicemail number
  6. Follow the instructions, and most importantly hit OK
  7. You should be prompted that the number has been updated
  8. Go to the dial pad, hold the 1 key, and verify it calls Google voicemail

Tuesday, April 2, 2013

ASP.NET Web API, Autofac & Self-Hosting

8:40 PM Posted by Tyson Nero , , , , 5 comments

Well Structured Projects


Separation of concerns comes into play even when laying out the projects in your code solution. For instance, you may have separate projects for your contract classes (DTOs), data layer, service layer, and sometimes even finer granularity among those.

When building such projects using ASP.NET's Web API for example, you may be tempted to lump everything into a single MVC 4 project, but each code solution has different needs and requirements, and even this type of solution can be broken out into multiple projects.

Self-Hosted Web API w/ external Controllers


Let's say you are building an IIS hosted Web API solution. Mainly, all of the clients connecting to your API will be web applications using AJAX. However, your API is also expected to have the ability to expose itself to other .NET clients who may be using the HttpClient object to perform operations. In this case, you may decide to break your solution out into 2 separate projects (I'm simplifying for brevity):
  1. Web Host Project (Empty MVC 4 Project)
  2. API/Service Project (Class Library)
The Web Host Project will be that which actually hosts the service, and the API/Service Project will be the implementation.

I highly recommend taking some time to review the PingYourPackage open source project on GitHub for a reference to such an implementation.


Implementing a Self-Hosted Web API with external assembly dependencies


API Host


Create a Console Application project to host the Web API by following this tutorial: Self-Host a Web API.


Controllers & Service Implementation


Create a new Class Library project for your controllers and service implementation
Implement a controller that inherits ApiController (ProductsController.cs)

Reference AutoFac.WebApi


Add an AutoFac.WebApi reference to your your Class Library project. This can be done via NuGet.



Wire Up AutoFac


You basically have two options here...

1) Add the AutoFac.WebApi dependency registration code to Program.cs.

2) Or, optionally create a configuration class (AutoFacConfig.cs). This will most likely be created in your service implementation project so any host could use it. Be sure to register your controller within this code.

With this, you will be able to call AutoFacConfig.Initialize(config) within your host.


Final Implementation


Friday, March 22, 2013

Event Namespacing with jQuery

2:21 PM Posted by Tyson Nero No comments
It's a standard practice to bind and unbind events using general event types such as "click". You would probably do something like this.


However, there are cases, especially with third party libraries for instance, where event collisions could occur. Let's say you bring in a plugin that does some type of magic on a button using click events. You too want to do your own magic and also bind to the click event. Time passes, and for some reason, the plugin gets destroyed and unbinds the click event. Not only has it cleaned up itself, but your event handlers are gone too!

The solution is event namespacing, which is simply adding class names to your events:


Event namespacing comes in extra handy when writing your own plugins. Remember, there are many other plugins out there, and just like writing C# code, you want to define unique namespaces that will hopefully not collide with other libraries.

Organization Just Doesn't End With Code

10:49 AM Posted by Tyson Nero No comments

This morning, as I reached into my dishwasher to put some more items in, I noticed a broken glass. It seems all the items on the top shelf weren't loaded in an organized manner, and a coffee mug handle had been pushed into a glass when an unnamed person tried to overload the area.

Well, I proceeded to remove the broken glass and reorganize the top shelf to avoid any other catastrophes of such magnitude. Then, I noticed my finger was bleeding and the situation was brought to a whole other level.

Several minutes later, my unnamed spouse entered the room to notice the broken glass, a reorganized dishwasher, and some bloodshed on a few other items in the dishwasher. With a quick glance she turned to me and said:

“Maybe if you hadn't reorganized the dishwasher, you wouldn't have got blood on everything.”