Toby's Ramblings http://tosbourn.com The personal blog of Toby Osbourn Tue, 28 Oct 2014 23:10:01 +0000 en-US hourly 1 http://wordpress.org/?v=4.0 Podcasts I am currently enjoying http://tosbourn.com/podcasts-currently-enjoying/ http://tosbourn.com/podcasts-currently-enjoying/#comments Tue, 28 Oct 2014 23:07:11 +0000 http://tosbourn.com/?p=2369 I have been really enjoying reading various folks on Twitter talking about their current podcast subscriptions. Namely Ian Robinson’s and Chris Laughlin’s. I wanted in! Podcasts I am currently enjoying Smart Passive Income — A podcast on the topic of making money online. Pat Flynn is a very smart man and is very easy to listen to. He […]

The post Podcasts I am currently enjoying appeared first on Toby's Ramblings.

]]>
I have been really enjoying reading various folks on Twitter talking about their current podcast subscriptions. Namely Ian Robinson’s and Chris Laughlin’s.

I wanted in!

Podcasts I am currently enjoying

Do you listen to podcasts? I would love your recommendations, please either comment, or better yet create your own post and link me up!

Share this on

The post Podcasts I am currently enjoying appeared first on Toby's Ramblings.

]]>
http://tosbourn.com/podcasts-currently-enjoying/feed/ 1
Great content has no shelf life http://tosbourn.com/great-content-shelf-life/ http://tosbourn.com/great-content-shelf-life/#comments Mon, 27 Oct 2014 17:55:07 +0000 http://tosbourn.com/?p=2367 Earlier today I was reading a great post on creativity, it was thought provoking, insanely relevant and it was written 55 years ago by Isaac Asimov. You will be surprised to know that Asimov didn’t write this thinking it would make a great blog post some day, sit on the first page of some major link […]

The post Great content has no shelf life appeared first on Toby's Ramblings.

]]>
Earlier today I was reading a great post on creativity, it was thought provoking, insanely relevant and it was written 55 years ago by Isaac Asimov.

You will be surprised to know that Asimov didn’t write this thinking it would make a great blog post some day, sit on the first page of some major link sharing websites and get hundreds or thousands of shares across various social networks.

He didn’t even write it to a mass audience, he wrote it for a small team of scientists.

This is clearly an extreme example, there are few that could get their point across just like Asimov but the point I would like to make is that if you invest in good content it can reap rewards for years to come.

Can you say the same about your marketing or SEO campaign? You cannot. I have made the case against evergreen content before, but there are clearly some benefits.

Share this on

The post Great content has no shelf life appeared first on Toby's Ramblings.

]]>
http://tosbourn.com/great-content-shelf-life/feed/ 0
Please stop using features by testing for browser http://tosbourn.com/please-stop-using-features-testing-browser/ http://tosbourn.com/please-stop-using-features-testing-browser/#comments Mon, 27 Oct 2014 09:18:45 +0000 http://tosbourn.com/?p=2364 Today I had to debug some code that up until fairly recently was working 100%. The issue was the code (some jQuery) was checking to see if the browser was Firefox and if it was it should use some Firefox specific code; if ($.browser.mozilla) { e.mozSlice(a,b) } Up until version 30 of Firefox this code […]

The post Please stop using features by testing for browser appeared first on Toby's Ramblings.

]]>
Today I had to debug some code that up until fairly recently was working 100%.

The issue was the code (some jQuery) was checking to see if the browser was Firefox and if it was it should use some Firefox specific code;

if ($.browser.mozilla) { e.mozSlice(a,b) }

Up until version 30 of Firefox this code would have worked, but mozSlice hasn’t been needed for some time and was finally removed as of Firefox 30.

Now, there are a few ways around this, a really naive way would be to add in some version checking as well, so if the browser is Firefox and the version is < 30 use mozSlice, else use slice.

These type of checks are really redundant and in 6 months time you will revisit the code and wonder what was so special about version 30, instead you can make your code much more clear by testing instead for the feature itself.

if (undefined !== e.mozSlice) { e.mozSlice(a, b) }

This code says, if we have access to mozSlice, lets use it.

This means that first of all if Internet Explorer decided to implement the mozSlice function instead of Slice this would work, it also means that if Firefox decided to drop mozSlice (which they have) nothing would break.

Share this on

The post Please stop using features by testing for browser appeared first on Toby's Ramblings.

]]>
http://tosbourn.com/please-stop-using-features-testing-browser/feed/ 0
Kill off any process on a particular port – An explaination http://tosbourn.com/kill-process-particular-port-explaination/ http://tosbourn.com/kill-process-particular-port-explaination/#comments Sun, 26 Oct 2014 20:50:22 +0000 http://tosbourn.com/?p=2358 For what feels like forever I have had to manually search for and then copy and paste code something like; lsof -wni tcp:5000 | awk 'NR>1{kill -9 $2}' So much so that recently I created a gist so at least I had some control of it and had a central place I could search. I […]

The post Kill off any process on a particular port – An explaination appeared first on Toby's Ramblings.

]]>
For what feels like forever I have had to manually search for and then copy and paste code something like;

lsof -wni tcp:5000 | awk 'NR>1{kill -9 $2}'

So much so that recently I created a gist so at least I had some control of it and had a central place I could search.

I wanted to explain the three main parts to this, partly to help out folk who are lost and partly because I am not too sure myself what all the flags are doing.

First, let me break this down into pseudocode;

  1. Get a list of all processes using port 5000
  2. Tidy up the list we receive to give us just the ID of the processes
  3. Kill the processes

These seem like sensible things to do, so let me talk about each one of them in detail.

Get a list of all processes using a port number

The first part of our one liner is actually the most alien to me. Lets start by finding out what the heck lsof stands for, then we will worry about the flags and the input.

man lsof

Running the man command brings up the manual for a given program, in our case lsof. Immediately we have learned something, it stands of list open files (ls being the command to list files in Unix like operating systems).

At first I thought this seemed very odd, I mean we want to kill stuff on a port, is a port a file? Reading the description gives us (bolding mine);

An open file may be a regular file, a directory, a block special file, a character special file, an executing text reference, a library, a stream or a net-
work file (Internet socket, NFS file or UNIX domain socket.) A specific file or all the files in a file system may be selected by path.

Cool, so that makes a little more sense, but what about the -wni flags we pass into it?

Again man helps us there, reading through the documentation you can find out all about the flags you can use.

  • w — Disables warning messages.
  • n — Inhibits  the  conversion  of network numbers to host names for network files.
  • i — selects the listing of files any of whose Internet address matches the address specified in i. As is in our case, if no address is specified, this option selects the listing of all Internet and x.25 (HP-UX) network files.

Finally, after these flags we pass in our actual input, tcp:5000 – all this is doing is say look for open files on the tcp port 5000.

So what happens when we actually run this command when we have something running on port 5000, I am going to spin up a rails server on that port and run the command;

COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
ruby 80077 tobyosbourn 11u IPv4 0xadc692f51faed72f 0t0 TCP *:commplex-main (LISTEN)

For our purposes the output gives us the Process ID, which is what we want, but for the sake of completeness lets talk about everything here;

  • COMMAND — The command using port 5000, in our case, ruby
  • PID — The Process ID
  • USER — The User who ran the command
  • FD — The File Descriptor Number for the file. In our case 11u, the ‘u’ part means there is read and write access on the file.
  • TYPE — The type of node associated with the file, since it is a networking file ours is IPv4
  • DEVICE — This can mean many things, but since we are dealing with a socket this is the base address of the device.
  • SIZE/OFF — This is either the size of the file or the size of the file offset in bytes
  • NODE — In our case this is the internet protocol being used (TCP)
  • NAME — Again this can have many meanings depending on the type of file, for us this is the nice name of port 5000 (something I didn’t know!)

Tidy up the output from lsof

As you can see there is a lot of pretty useful output we get when running such a simple command, but for our purposes we only care about the PID, this is where awk comes in.

awk is a program for pattern scanning and processing text, it is crazy powerful and the learning curve isn’t as steep as you might imagine.

Lets start by simplifying our command, so instead of;

awk 'NR>1{kill -9 $2}'

We have;

awk 'NR>1{print $2}'

That might still look as clear as mud to you, but let me explain from left to right;

NR>1 says, ignore any line number less than 1, we count from 0 so what this means is, don’t worry about the titles in our output.

{} says do the command inside me once per line

print $2 is a bash command, this is saying print whatever is stored in $2. The magic part is that awk has seen that the output has a clear boundary between words so will only print the second word it sees, which in our case is the PID.

Running this print command means the output that was;

COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
ruby 80077 tobyosbourn 11u IPv4 0xadc692f51faed72f 0t0 TCP *:commplex-main (LISTEN)

Becomes;

80077

Excellent! This is exactly what we want!

Kill the processes

Well, it is *almost* what we want, in our last set of examples we changed our code to print the PID we want. Lets be honest, we don’t actually care what the PID is, we just want the process to be killed!

That is when kill -9 comes in.

The flag 9 references the signal that we will send to the process and we pass in the process ID within that process group we want to kill.

The signals you can send are;

  • 1 — HUP (hang up)
  • 2 — INT (interrupt)
  • 3 — QUIT (quit)
  • 6 — ABRT (abort)
  • 9 — KILL (non-catchable, non-ignorable kill)
  • 14 — ALRM (alarm clock)
  • 15 — TERM (software termination signal)

I hope this has helped clear up what is actually happening when you run that command!

Share this on

The post Kill off any process on a particular port – An explaination appeared first on Toby's Ramblings.

]]>
http://tosbourn.com/kill-process-particular-port-explaination/feed/ 0
Space Invaders http://tosbourn.com/space-invaders/ http://tosbourn.com/space-invaders/#comments Mon, 13 Oct 2014 19:06:24 +0000 http://tosbourn.com/?p=2353 I love this, I am guessing you have played space invaders before and you are aware how the game works. As you shoot the invaders they gradually get faster and faster. It was a nice game play mechanic as it made the level progressively harder the closer you got to finishing it. Well apparently this […]

The post Space Invaders appeared first on Toby's Ramblings.

]]>
spinv_1

I love this, I am guessing you have played space invaders before and you are aware how the game works.

As you shoot the invaders they gradually get faster and faster. It was a nice game play mechanic as it made the level progressively harder the closer you got to finishing it.

Well apparently this wasn’t ever actually programmed in! In the original game hardware struggled to keep up with all the graphics on screen, as you removed some of those assets the hardware was able to render stuff faster and so things started to speed up.

The hardware that he had assembled was not powerful enough to display graphics in color and the enemies moved slower than he had intended. Fortunately he discovered that the more enemies that were removed from play, the faster the processor could render those that remained. Rather than attempt to design the game to compensate for the speed increase, he opted to leave it in as a challenging gameplay mechanic.

This certainly has to be one of my favourite – It’s not a bug, it’s a feature! moments.

Share this on

The post Space Invaders appeared first on Toby's Ramblings.

]]>
http://tosbourn.com/space-invaders/feed/ 0
Nokogiri won’t install? Check that you have accepted Xcode’s EULA http://tosbourn.com/nokogiri-wont-install-check-accepted-xcodes-eula/ http://tosbourn.com/nokogiri-wont-install-check-accepted-xcodes-eula/#comments Mon, 06 Oct 2014 11:21:50 +0000 http://tosbourn.com/?p=2350 Here is a pro tip I never thought I would need to share! For Nokogiri to install on OSx out of the box it requires some of the command line tools that come with xCode. I have everything like that installed because I have had to install things like Nokogiri several times in the past. […]

The post Nokogiri won’t install? Check that you have accepted Xcode’s EULA appeared first on Toby's Ramblings.

]]>
Here is a pro tip I never thought I would need to share!

For Nokogiri to install on OSx out of the box it requires some of the command line tools that come with xCode. I have everything like that installed because I have had to install things like Nokogiri several times in the past.

Today though I was getting this error message that suggested that I didn’t have those required tools installed.

Building native extensions.  This could take a while...
Building nokogiri using packaged libraries.
ERROR:  Error installing nokogiri:
	ERROR: Failed to build gem native extension.

    /Users/tobyosbourn/.rvm/rubies/ruby-2.1.1/bin/ruby extconf.rb
Building nokogiri using packaged libraries.
checking for iconv.h... *** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers.  Check the mkmf.log file for more details.  You may
need configuration options.

10 minutes of yak shaving later and I decided to open up xCode to see what the craic was, turns out a point release had been downloaded and installed and came with a new EULA, as soon as I accepted this I was able to install the nokogiri gem successfully!

Share this on

The post Nokogiri won’t install? Check that you have accepted Xcode’s EULA appeared first on Toby's Ramblings.

]]>
http://tosbourn.com/nokogiri-wont-install-check-accepted-xcodes-eula/feed/ 0
WhiteHat Aviator Phoning Home? http://tosbourn.com/whitehat-aviator-phoning-home/ http://tosbourn.com/whitehat-aviator-phoning-home/#comments Fri, 03 Oct 2014 15:16:46 +0000 http://tosbourn.com/?p=2348 Recently I was contacted by someone who wishes to remain anonymous (which is fair enough!) about my review of WhiteHat Aviator. In the email I was asked to look at how WhiteHat Aviator phones home. The person who contacted me was concerned that they had noticed network activity coming roughly every five hours from Aviator […]

The post WhiteHat Aviator Phoning Home? appeared first on Toby's Ramblings.

]]>
Recently I was contacted by someone who wishes to remain anonymous (which is fair enough!) about my review of WhiteHat Aviator.

In the email I was asked to look at how WhiteHat Aviator phones home. The person who contacted me was concerned that they had noticed network activity coming roughly every five hours from Aviator to some WhiteHat servers.

My gut reaction was this was likely just the service phoning home to look for updates, but not knowing the best practice for doing this on a Mac I decided to email WhiteHat and ask about why Aviator phones home, I had mentioned this was in response to an email I had received from a concerned user.

I wanted to share parts of their response because I think it clears up any issues;

Here’s how it works – every 5 hours the browser calls back to our server with a non-unique query so we can’t correlate/track our users. We spent a lot of time working on this to try to limit our ability to track users, because browsers like Chrome phone home with the machine-ID and user-ID of the system for every update and that wasn’t something we were willing to do.

If the file that is downloaded says there is an update the browser will automatically fetch and barring any signing issues or corruption in the file it will install it for use the next time the user downloads the browser. Other browsers have paved the way in this regard, and find that it is significantly safer to always keep the user updated, since the majority of features we launch are security related anyway.

The purpose of our browser was to be used by the common person, not by the security pro or privacy nut. They have the technical chops to use Tor (in a safe manner, which isn’t actually very easy to do) or modify their own browser. The average person doesn’t have that ability, so this browser is intended to give them the right training wheels to make sure
they don’t have issues. We always allow our users to disable plugins, but if they remove them the browser will just re-download them so that doesn’t make much sense. Each plugin has been chosen specifically to achieve better usability and higher security. For example, we had to ship with UA spoofer, because too many sites are poorly programmed and don’t know that other browsers exist and don’t have a graceful failure mode. PDF.js was essential, because it’s much safer, from what we can tell thus far, than Adobe’s PDF viewer – time will tell if that was an incorrect assumption but other browsers are betting on the same thing, so I think it’s a safe bet. Disconnect is the plugin that supports ad-blocking and blocking of tracking pixels which speeds up the user experience and speeds up increases security in the process. Those are the only plugins we ship with – if the user has others, that didn’t come from us.

The end result here, is that I think this user is conflating privacy with security, and while we are a much more private browser than most, we would never claim to be 100% private, because we have to allow our users to get updates. If 100% privacy assurance is the goal, Tor browser is the one of choice – though, there are many security and privacy pitfalls due to compromised exit nodes, which is why I would never recommend it to the Masses.”

If you are a concerned user after hearing or experiencing the phoning home, then I hope this clears it up for you :-)

Share this on

The post WhiteHat Aviator Phoning Home? appeared first on Toby's Ramblings.

]]>
http://tosbourn.com/whitehat-aviator-phoning-home/feed/ 2
Converting numbers to hex using to_s in Ruby http://tosbourn.com/converting-numbers-hex-using-to_s-ruby/ http://tosbourn.com/converting-numbers-hex-using-to_s-ruby/#comments Thu, 25 Sep 2014 12:21:48 +0000 http://tosbourn.com/?p=2345 Today I learned that you can easily convert a number to its hex value by calling to_s on it along with the base you want to convert as a parameter on to_s. 170.to_s(16) => "aa" Of course this means you can also convert it to binary (base 2) 170.to_s(2) => "10101010" Personally I like my […]

The post Converting numbers to hex using to_s in Ruby appeared first on Toby's Ramblings.

]]>
Today I learned that you can easily convert a number to its hex value by calling to_s on it along with the base you want to convert as a parameter on to_s.

170.to_s(16)
 => "aa"

Of course this means you can also convert it to binary (base 2)

170.to_s(2)
 => "10101010"

Personally I like my hexadecimals to be in uppercase, I could call to_s(16).upcase each time but that feels a little wasteful, why don’t we make a method to do that for us?

class Numeric
   def to_hex
      to_s(16).upcase
   end
end

My use case for this was to convert RGB values to Hex values, now generally when you do that you would want an RGB value of 0,0,0 to be converted into 000000, but as you can will calling to_hex on 0 currently produces 0.

0.to_hex
 => "0"

How do we change this? The method I went for was to pad my output after changing it to a string.

0.to_hex.rjust(2, '0')
 => "00"
Share this on

The post Converting numbers to hex using to_s in Ruby appeared first on Toby's Ramblings.

]]>
http://tosbourn.com/converting-numbers-hex-using-to_s-ruby/feed/ 0
Backing up an s3 bucket to another s3 bucket http://tosbourn.com/backing-s3-bucket-another-s3-bucket/ http://tosbourn.com/backing-s3-bucket-another-s3-bucket/#comments Mon, 22 Sep 2014 13:05:56 +0000 http://tosbourn.com/?p=2343 In a project I am working on in Rumble Labs we had cause to want to backup an entire s3 bucket before making potentially destructive changes. After some research I found that AWS have an officially supported client that would allow you to do just this. In this guide I will walk you through how […]

The post Backing up an s3 bucket to another s3 bucket appeared first on Toby's Ramblings.

]]>
In a project I am working on in Rumble Labs we had cause to want to backup an entire s3 bucket before making potentially destructive changes.

After some research I found that AWS have an officially supported client that would allow you to do just this. In this guide I will walk you through how to install the AWS client, create a new bucket and sync your stuff across to this new bucket.

Installing and Configuring AWS CLI

I had both Python and Pip installed so was able to install everything using Pip with the following command

sudo pip install awscli

If you don’t have Pip installed you can follow this guide.

Now we need to configure our AWS client by giving it our keys and such, this is explained in full in the official guide but all I needed to do was run the following command

aws configure

You will be asked a series of questions;

  • Access Key (required)
  • Secret Key (required)
  • Default Region (I left this blank)
  • Default Output format (I left this blank)

You can run the configure command as many times as you like, any blank answers will assume the previous setting is staying unchanged.

Backing up your s3 Bucket

The first thing we want to do is create a new bucket to house our backup

aws s3 mb s3://my-backup-bucket

What this does is tell aws that we are performing an s3 related action, the action we are performing is mb (make bucket) and we are supplying a bucket name, in this case my-backup-bucket.

Now we have everything in place to copy our stuff into our new bucket, we do this with the aws sync command

aws s3 sync s3://my-current-bucket s3://my-backup-bucket

What this does is tell aws again that we are performing an s3 action, this time we are performing the sync action. This action takes two properties, the bucket we are copying from and the bucket we are copying to.

Once you run this you will receive the output of the files copying in your terminal, which I wanted because I like to keep an eye on these things. If you don’t want to see this I see there is a --quiet flag listed in the documentation for sync.

Share this on

The post Backing up an s3 bucket to another s3 bucket appeared first on Toby's Ramblings.

]]>
http://tosbourn.com/backing-s3-bucket-another-s3-bucket/feed/ 4
Force a CSV download from a WordPress Plugin http://tosbourn.com/force-csv-download-wordpress-plugin/ http://tosbourn.com/force-csv-download-wordpress-plugin/#comments Tue, 16 Sep 2014 22:24:26 +0000 http://tosbourn.com/?p=2340 If you are writing a WordPress Plugin and want to automatically download a CSV file when a certain page is hit here is roughly how you go about it. All this code can sit in your main plugin file, or however you like to organise your plugins. // Standard code to add a menu item […]

The post Force a CSV download from a WordPress Plugin appeared first on Toby's Ramblings.

]]>
If you are writing a WordPress Plugin and want to automatically download a CSV file when a certain page is hit here is roughly how you go about it.

All this code can sit in your main plugin file, or however you like to organise your plugins.



// Standard code to add a menu item in the admin section
function my_menu() {
    add_menu_page('Download CSV', 'Download CSV', 'add_users', 'my_csv_download', 'csv_download');

}

// Important - Note this is not stored in a function, this is just in the main
// file, if we ran it through a function headers would already get set.
if (isset($_GET['page']) && $_GET['page'] === 'my_csv_download') {
    // Just setting some headers for our download
    header("Pragma: public");
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Cache-Control: private", false);
    header("Content-Type: application/octet-stream");
    header("Content-Disposition: attachment; filename=\"my_csv.csv\";" );
    header("Content-Transfer-Encoding: binary");
    echo csv(); // Your CSV goes here.
    exit;
}

// This action adds our menu to the main menu structure
add_action('admin_menu', 'my_menu');
Share this on

The post Force a CSV download from a WordPress Plugin appeared first on Toby's Ramblings.

]]>
http://tosbourn.com/force-csv-download-wordpress-plugin/feed/ 0