Rails Camp Sydney 2015

I want to write down my thoughts on the last Rails Camp that I went to. So here goes. The Camp The camp was in Broken Bay, Hawkesbury River, Sydney. It was an awesome location. Very scenic with beaches and plenty of trails to hike on. But this being Sydney in the middle of winter it was cold at night (duh). The food was absolutely amazing for a camp. The slow cooked beef served on Sunday night was the highlight of the camp :P ...

June 23, 2015

Dropping HTTP Requests and Connection Efficiently on Nginx

location /banned/url { return 444; } From How nginx processes a request: …and a special nginx’s non-standard code 444 is returned that closes the connection. Basically, match the URL/pattern..etc that you want to handle and then do a return 444; It will make nginx drop the connection without even bothering to return a response to the client.

June 3, 2015

Note-to-self: Setting up a new mac for development

I recently got a new development machine (yay!). And went through the process of setting it up. Getting new toys are always fun. But setting them up from scratch can be a bit of a pain. So I am documenting all the steps I took as a reference for the future. Setup and enable Find my mac Enable FileVault Copy over personal ssh private keys Set proper permission on the keys (chmod 600 your_key.pem) Create home dir structure development Apps to install Chrome Setup Lastpass BetterSnaptool Evernote Homebrew Developer tools Git gem install git-smart ansible brew tap homebrew/services mysql phantomjs Cask Virtualbox Vagrant Github OSX Iterm2 Dash Download your needed doc sets sequelpro hipchat spotify teamviewer java (Oracle JRE) skype tunnelblick truecrypt iBrowse Rubymine RVM rvm list known rvm install ruby-2.2 gem install bundle Setup YADR (dotfile repo) https://github.com/skwp/dotfiles When cloning large Git repos from Github… things can get really really slow. If this happens just copy the repo over from the old machine using a USB drive. Old school I know. But it works. Hopefully the future me would find this helpful. Cheers!

May 18, 2015

Credit Card Number (with a separator in between) Validation Regex

The following will validate Visa and Master card numbers that’s in the following format ^(?:4[0-9]{3}(?:-[0-9]{4}){2}-[0-9](?:[0-9]{3})?|5[1-5][0-9]{2}(?:-[0-9]{4}){3})$ 4111-1111-1111-1111 (note the ‘-‘) Regex adapted from: http://www.regular-expressions.info/creditcard.html Tip: Also found this super AWESOME regex testing site: https://regex101.com/#javascript

May 11, 2015

Nginx, AWS ELB, DNS resolution, Nginx resolver directive and Black magic

We use Nginx to reverse proxy traffic to our app servers in a few places (on our Varnish servers we have a Nginx proxy that reverse proxy traffic to our backend servers and the backend servers them selves reverse proxy traffic to other app servers). And all our servers are hosted on AWS and they all sit behind ELBs. So just like a lot of people on the inter webs (look at the references section bellow) we found out that Nginx is only resolving the ELB DNS once on startup and then not refreshing it. This caused some of our servers to break because AWS changes IPs for ELBs on the fly. ...

May 8, 2015

Things to do after you buy a new gaming laptop

Please note that I am not a gamer(!!!). This is from the POV of a some what casual gamer who likes to game SOME of the time. Choose a good OS: I chose Windows 8.1 Pro because this machine’s main purpose was gaming. Might end up installing Ubuntu on the side so I can use this for development on the side. But that’s a big ‘might’. Decide on how to partition the disk(s). In my case I decided to have one big partition on my 250 SSD. Install Anti theft software. I installed Pray. Get the latest drivers in general. Update your graphics card drivers (duh!). Actually do a bit of research on the games you want to play and the current drivers. In my case a particular game I wanted to play didn’t work well with the latest drivers (so I should have gone with something older that worked fine with the game in question). Update SSD drivers: Mine came with a Samsung 840 Evo. So I downloaded and installed Samsung Magician. Which takes care of a lot of things. Including updating your SSD firmware if need be. If you are using a SSD, make sure you have TRIM turned on. That’s all that I can think up for now. Gonna add on to this post in the future if anything comes up. ...

May 8, 2015

Stub/Mock Rails view helpers in RSpec

This was not as easy to find on the inter webs as it should be. Example If you want to stub out a view helper (view_helper_method_x) in your view spec. allow(view).to receive(:view_helper_method_x) { return_value_goes_here_as_usual } The spec should look something like bellow # encoding: UTF-8 require 'spec_helper' describe ‘home/view.html.erb’ do before(:each) do allow(view).to receive(:view_helper_method_x) { true } end it ’should do blah blah' do render expect(rendered).to_not have_css ‘.test_id' end end That’s all folks!

December 18, 2014

Testing Rails Controller Helper Methods Using RSpec

If you are new to this. First read this article about Anonymous Controller. That should answer 98% of your questions. However, if you run in to problems the following tid bits of information might help ;) Summary of Anonymous Controller (code stolen from https://www.relishapp.com/rspec/rspec-rails/v/3-0/docs/controller-specs/anonymous-controller) class ApplicationController < ActionController::Base class AccessDenied < StandardError; end rescue_from AccessDenied, :with => :access_deniedprivate def access_denied redirect_to "/401.html" end end require "rails_helper" RSpec.describe ApplicationController do controller do def index raise ApplicationController::AccessDenied end end describe "handling AccessDenied exceptions" do it "redirects to the /401.html page" do get :index expect(response).to redirect_to("/401.html") end end end Gotchas Missing helper methods If RSpec is having problems finding your helper methods within the Anonymous controller (it shouldn’t, because by default the controller should inherit from the controller you are testing), try the following. ...

December 17, 2014

Varnish cache purging from dynamically instantiated EC2 instances behind a ELB using HTTP header based authentication

We use a Varnish cache server to do basic page caching for our Rails apps. The application instances are automatically instantiated by a ASG(auto scale group) and all these instances sit behind a ELB. Usually Varnish cache purge requests are only allowed from a few white listed IPs. Like bellow: acl purge { "localhost"; "192.168.55.0"/24; } sub vcl_recv { # allow PURGE from localhost and 192.168.55... if (req.method == "PURGE") { if (!client.ip ~ purge) { return(synth(405,"Not allowed.")); } return (purge); } } However, because an ELB doesn’t have a static IP (it changes with time, depending on various factors) and also because our application instances themselves are dynamically instantiated and have no static IPs associated with them, we can’t simply use a white list to allow PURGE requests from our app servers. ...

November 6, 2014

npm ERR! cb() never called! (on npm 1.4~)

If you are getting the bellow error on a recent version npm (1.4~) npm ERR! cb() never called! npm ERR! not ok code 0 Chances are you are facing this issue: npm shrinkwrap install - npm ERR! cb() never called! (Linux / OS X) https://github.com/npm/npm/issues/5920 Unfortunently there is no fix avialable as of yet (29th Oct 2014). The only solution seems to be rolling back to a old version of npm unaffected by the bug. ...

October 29, 2014