Tuesday, 11 February 2014

Ruby: Class loading/unloading example

Recently I had the unfortunate task of writing specs to test a singleton.
Why would that be a chore I hear you ask?
Because the singleton in question had several configuration options.
Most of which were file based.
So out of the ~60 specs, 50 of them were for error conditions.
(File missing, folder not readable, not JSON, JSON but with invalid stanzas etc)

The problem was that each test had to start with a clean slate.
The singleton class must be pristine and new each run.
How the hell do you do that?

Unload the class, then reload it.

I fiddled about quite a bit with this, so I've put together a simple project that illustrates how this is done.

The bottom line is that you have to send :remove_const and the class name to the module or whatever your class is defined in.

For example, say a class is defined in a file skavee_bloopsie.rb thus:

module Skavee
  class Bloopsie
  end
end

Then this is what you run to unload and reload it:

Skavee.send(:remove_const, :Bloopsie)

This works at any depth of modules:

module A
  module B
    module C
      class Z
        def ohai
          'ohai!'
        end
      end
    end
  end
end

A::B::C.send(:remove_const, :Z)

You can test this yourself by copying and pasting this into irb:

module A
  module B
    module C
      class Z
        def ohai
          'ohai!'
        end
      end
    end
  end
end

a = A::B::C::Z.new
 => #<A::B::C::Z:0x007fc78c9b1dc8>

puts a.ohai
 => "ohai!"

A::B::C.send(:remove_const, :Z)
 => A::B::C::Z

b = A::B::C::Z.new
 => NameError: uninitialized constant A::B::C::Z

And here's the Gotcha!

a.ohai
 => "ohai!"

Wha? Well it's obvious. The instance is still being referenced.

So be careful out there people.

An example gem is available via my repo at https://github.com/ZenGirl/ruby-class-unloading

Friday, 11 October 2013

RubyMine GitLab Integration

I setup the community version of GitLab so I can have locally controlled projects.
Since I use RubyMine as my IDE, the integration with both github and gitlab was a bit convoluted.

First I couldn't get ssh to work, so I used http (since it was on my home network) to get the push/pull working.
(There are links to pages explaining how to fix that at the end of this post)

One you have GitLab running, you can create a project.
After doing that, you get to see a page with boxes telling you how to connect using git from the command line.

One thing to note is that there are two commands to set your `user.name` and `user.email`.
The key is that they use `--global` which is problematic.

So. Let's say you have created a project called `test`. Do this:

cd [your projects folder]
mkdir test
cd test
git init
git config --local user.name "YOUR_NAME"
git config --local user.email "YOUR_GITLAB_ACCOUNT_EMAIL"
git remote add origin [HTTP_URL_PROVIDED_BY_GITLAB]
touch README.md
git add README.md
git commit -m 'Initial'
git push -u origin master

You can have 'port' and 'groups' so the url could look like this:

http://gitlab.local:8080/home_projects/testing.git

Ok. What about task management integration?

Fiddly, but doable.

First, you have to use the GitLab API.
And for that you need your private key AND know the project number.

When you issue a curl request you get back JSON:

curl http://gitlab.local:8080/api/v3/projects/3/issues?private_token=YOUR_API_TOKEN
[
    {
        "id": 4,
        "project_id": 3,
        "title": "Some task",
        "description": "Some description",
        "labels": ["Enhancement"],
        "milestone": null,
        "assignee": {
            "id": 4,
            "username": "bill",
            "email": "bill@bill.com.au",
            "name": "Bill Bloggs",
            "state": "active",
            "created_at": "2013-10-10T22:59:26Z"
        },
        "author": {
            "id": 4,
            "username": "bill",
            "email": "bill@bill.com.au",
            "name": "Bill Bloggs",
            "state": "active",
            "created_at": "2013-10-10T22:59:26Z"
        },
        "state": "opened",
        "updated_at": "2013-10-11T01:07:50Z",
        "created_at": "2013-10-11T01:07:19Z"
    }
    ,
    {
        "id": 3,
        "project_id": 3,
        "title": "Another task",
        "description": "Another description",
        "labels": ["Bug"],
        "milestone": null,
        "assignee": {
            "id": 5,
            "username": "bill",
            "email": "bill@bill.com.au",
            "name": "Bill Bloggs",
            "state": "active",
            "created_at": "2013-10-10T23:00:19Z"
        },
        "author": {
            "id": 4,
            "username": "bill",
            "email": "bill@bill.com.au",
            "name": "Bill Bloggs",
            "state": "active",
            "created_at": "2013-10-10T22:59:26Z"
        },
        "state": "opened",
        "updated_at": "2013-10-11T01:07:59Z",
        "created_at": "2013-10-11T01:06:54Z"
    }
]

So how to get RubyMine to use this?

1. Open preferences -> tasks
2. Change 'Changelist name format' to {project}-#{number} {summary}

Under Servers, add a 'generic' server

In the general tab:

1. Change the server URL to http://YOUR_SERVER/YOUR_REPO_WITHOUT_DOT_GIT
2. Insert your username and password
3. Click 'Use HTTP Authentication'

Go to the gitlab server and under your account settings, there is an input box with your private key.

Copy it.

In the additional tab:

1. Add the URL: http://YOUR_SERVER/api/v3/projects/PROJECT_NUMBER/issues?private_token=YOUR_PRIVATE_KEY
2. Check the 'JSON' option
3. In the Task pattern insert: "id":({id}.?+),.+?,"title":"({summary}.+?)"

Hit test.

You should see 'Connection is successful'.

From now on you should be able to use the tasks drop down.

FYI When creating tasks in gitlab, there is an irritating anti-feature for assigning labels.
You have to type them in.
The set I use is:

Enhancement
Bug
New Feature
Quick Fix
Duplicate
Invalid
Question
No Fix
Documentation
Presentation

I still need to figure out how to have GitLab return the project name ala TestProject-#{number} {summary} format.

Off-topic: Go read
https://wiki.archlinux.org/index.php/Gitlab#Backup_and_restore
and https://github.com/gitlabhq/gitlab-recipes
and http://api.gitlab.org/
and https://github.com/gitlabhq/gitlab-public-wiki/wiki/Trouble-Shooting-Guide
and https://groups.google.com/forum/#!forum/gitlabhq

Friday, 4 October 2013

__rvm_date syntax error: unexpected end of file

When running rspec tests inside RubyMine, you may see something like this:

 sh: __rvm_date: line 1: syntax error: unexpected end of file
 sh: error importing function definition for `__rvm_date'

What worked for me is to shutdown RubyMine and edit the

 /Applications/RubyMine.app/bin/idea.properties

file and add:

 rubymine.console.environment=disabled

To the end of it.

Apparently there is a bug for it at:

 http://youtrack.jetbrains.com/issue/RUBY-14308

Something to do with loading rvm through shell inside RubyMine.

em::RemoteFetcher::FetchError: SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed

Ok.

One day you'll see this:

$ bundle install
Fetching gem metadata from https://rubygems.org/.........
Fetching gem metadata from https://rubygems.org/..
Resolving dependencies...

Gem::RemoteFetcher::FetchError: SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed (https://s3.amazonaws.com/production.s3.rubygems.org/gems/rake-10.1.0.gem)
An error occurred while installing rake (10.1.0), and Bundler cannot continue.
Make sure that `gem install rake -v '10.1.0'` succeeds before bundling.

Wait. What?

Worse is when you do a `gem list --remote` you get NOTHING.

WHAT? PHHBBTT! [Brain melts]

The issue is somewhat arcane and is embedded in rvm using openssl.

Let's have a look at the certificates:

$ rvm osx-ssl-certs status all
Certificates for /usr/lib/ssl/cert.pem: Old.
Certificates for /usr/local/etc/openssl/cert.pem: Up to date.
Certificates for /etc/openssl/cert.pem: Old.

Ah. So what worked for me is:

rvm get stable
rvm reload

If you still get 'Old' on the status, do this:

$ rvm osx-ssl-certs update all
Updating certificates for /usr/lib/ssl/cert.pem: Updating certificates in '/usr/lib/ssl/cert.pem'.
...elided...

and now you'll be able to bundle again.

Saturday, 7 September 2013

Iron Man Zombie Robot Hitler is not happy - and he has a gatling gun

No.
I'm not kidding.
Ben keeps finding these movies.
I don't know how he does it.

He's a node warrior.

In any case 'Nazis at the Centre of the Earth' is worth a look.
If you are into gouging your eyes out with forks that is.

It's appalling.
There are so many 'WTFFF?' moments in this movie I couldn't even begin to do justice to it.
Although in this case justice might be better served by getting every copy on the planet and burning them.
Oh.. And the extra two F's are 100% totally insufficient.

I think the point when I said "Ok. This is very, very bad" was when an Lockheed P-3 Orion survey plane encounters the Nazi saucer rising out of the Antarctic ice and the radio conversation goes something like this:

Pilot: "I'd like to report a UFO!"
Off-Camera Dude: "UFO?"
Pilot: "Yes! It's huge!"
Off-Camera Dude: "You're cleared to engage!"

Wait. What? Engage? With what? Bad language? This is an Orion, not an F-18!

And then the Orion launches 8, yes *eight*, Hellfire missiles at the saucer.

"Calm down Kim... Breathe..."

Two things.

1) It's a frackin' survey plane. I know the Americans go a little gun happy arming things, but an Orion? In Antarctica? Hellfires? Eight of them?

2) As the pilot of the Orion, I would notice that the saucer is size of Nebraska. Oh. And the rivets holding it together were 3 foot across. Eight Hellfires would not be enough. Sixty four wouldn't be enough. I'd be "gone".

Oh.. And Robot Hitler? Cool. In a "WTF?" kinda way. I kept asking myself some things:

1) Why does he need to sit down? He's a frickin' robot.
2) Robot Hitler?
3) Gatling gun?
4) [Head explodes]

But I digress.

Wait. Where was i?
Oh yes.
It's KRAP.
But in a that "Wouldn't Miss It For The World" kinda way.
A kinda "Robot Hitler with a Gatling Gun" kinda way.

Wednesday, 4 September 2013

A Movie, my D&D roots and software development

Hmm.
Just finished watching "Tad - The Lost Explorer".
Worth it btw... So go find it and watch it.

In any case...
There was a scene where the explorers reach a chamber.
With dozens of different coloured ropes hanging down.
They eventually picked the right one of course...
And the door opened and the movie continued.

I was reminded of my D&D roots back in the 70's.
I ran a dungeon.
A bad one.
An evil one.
A 'gotcha' one.
An 'oh... I'm dead.. Frack!' one.

One weekend I had a dozen or so people trying to navigate my dungeon.
They encountered a room.
With coloured ropes hanging down.
And a door on the other side with "нажмите сюда" on a button.

(For those that don't know any Cyrillic it *very* roughly means "press here" - I'm not a Russian speaker so I apologise in advance for any butchering of the language)

So...
Here was a bunch of very smart people debating how to get across the room and open the door...

And did they debate?

Hell yeah.

For four weekends they investigated every rock, every crack, every sand grain at the entrance of the room.

I waited... Impatiently I might add...

On the fourth weekend, they had catalogued every rock, every crack, every sand grain...
They roped themselves together and formed a line across the room.
The one at the front (A guy who nearly had a heart attack doing it) strapped three spears together...
And with the air buzzing with protection spells, pressed the button.

And...

And...

The door opened.
The rest started the most excruciating process of getting each member, their donkeys, their supplies across that room.
They all made it.

Why? Because there was no trap...
They were inculcated with the idea that every move would be a deadly one.
My dungeons were death traps.
The slightest mis-step would end everyone.

That room.
They called it "The Room Of Sorrows."
Not that room.
I just put a simple room.
No traps.
No death.
No hidden issues.

I marvelled at their ingenuity at bypassing traps that weren't there.

They made it and congratulated each other.

And now, I look back and see how often a team of developers often does the same.
Not every issue is a "Room Of Sorrows."
You don't need to spend hours on some issues.

Sometimes you need to have the b***s to just barrel in and TRUST that you are frackin' smart enough and ugly enough to work out the issues as you take the path.

Pick a path.

Commit.

Oh. And Push... Mustn't forget the push.

Monday, 2 September 2013

GalactiCraft! OMG!

Ok. Haven't posted for a while as I've been rather busy.
Yesterday (Sunday) I decided to rebuild our internal network minecraft server up.
Got bukkit and configured it up and allowed external access to it for my family.

In any case, while doing this I stumbled on Galacticraft.
It's a tekkit mod that is just hilarious and looks very cool.

The site is at:

http://micdoodle8.com/mods/galacticraft

And you should *definitely* watch the podcasts by yoglabs.
Hysterically funny.
I cracked up repeatedly.

My favorite parts are.. are...
Oh what the heck...
ALL OF THEM.
Giant three headed creepers, spiders with spacesuits, Shai Hulud, alien eggs, variable gravity, slow motion skeleton arrows...
I just giggle madly if I think of the spider with a helmet and air tanks.

I could go on...
But you need to go and watch the podcasts.
Do it.
Do it now...

Oh... Shai Hulud... If you haven't watched or read Dune, this won't mean anything.
But don't despair! Go get the book, or watch the movies.
My favorite is the De Laurentius "theatrical" version. (We have all the versions)




Saturday, 17 August 2013

Harley and Callum - Foster Cats 14 and 15

Here are a few shots of our latest foster cats Harley (a male sleek black cat) and Callum (a male ginger cutie).
These two are completely inseparable.
They play and cuddle constantly.
They are also 'lickers'.
I've been cat-foliated several times now and my ears are squeeky clean if a bit sore.
Harley loves to groom and spends ages tugged and licking my hair.





Monday, 8 July 2013

CouchDB Sinatra REST bare-metal call test application

Some months ago I was involved in configuring an API to use with CouchDB. In the process of this, I created a test application using Sinatra to utilize the various CouchDB REST calls. This is that application.

Later I created a MUCH more sophisticated set of sub-apps in Padrino to utilize the lessons learned. For some time now I have been asked to release it into the wild to aid other developers, but have been too busy. Now I have time. Hence this post.

It is not great. It is not well written. It is not documented. It has no tests. But it works.

It was written in one day to get the process of using rest-client to operate against a test CouchDB database. That is all.

I make no apologies for the code.

It works. That is all.

To use it:

1) Install CouchDB someplace.

I have it on an external server as exampled in the config/couchdb.yml file.

2) Clone the repo from https://github.com/ZenGirl/CouchDBSinatraRestAPI

Goes without saying...

3) Change the config/couchdb.yml file

Change to suit.

4) Fire it up

bundle install
rackup -s thin -o 0.0.0.0 -p 9292 -E development config.ru

Then open a browser to http://0.0.0.0:9292/index.html and start playing.

Honestly!

I would love to here from cloners who would like enhancements as I'm always ready to learn. If you see a problem, or want to have me change something, tell me.

Sunday, 7 July 2013

Well, it's official. I've been unemployed for 6 months and broke.

I left Guvera officially mid last year due to fairly dramatic family needs. Then Guvera asked me back on contract which I could do while handling those family issues. I finished up that contract just before christmas to work on some other exciting projects.

I have tons of work on several major startups since then. Mainly Rails, Padrino and Java with some PHP and Systems Admin work. While those startups may still create income, none of them have produced dollar 1 so far.

So I have no option but to hit the job market and try to find work to pay the bills. So if you're looking for a highly experienced technologist, toolmaker, solution architect, problem solver, trouble shooter, firefighter and innovator, then go no further. Have a look at my LinkedIn profile to see if you could use my skill sets. In particular see my colleagues recommendations.

Here's a quick run down of who I am and what I can do:

I have over thirty years experience in computing and have worked for large multi-nationals such as Sun Microsystems, Microsoft, Western Mining Corporation and many, many startups. I have written Unix Systems Administration manuals and have taught systems administration and programming to both groups and individuals.

I love technology!
Since the late 70’s I have been involved in the open source community and contributed heavily to many early projects. My code exists in many implementations of linux and I have even been cited on a man page (albeit for a game). Ben and I own far too much tech... We even have a first gen MakerBot and have used it to fix quite a few things around the house!

TDD is a part of everything I write and has been for many years. Unfortunately I have found that many companies consider TDD to be a waste of programming resource, which I consider short sighted.

Release early, release often. I have always advocated this and used (and built) many deployment tools such as Capistrano et al to get code live quickly.

I have used MySQL, MongoDB and recently CouchDB to handle large amounts of data. A recent project involved 100’s of millions of documents handling companies historical data.

I am no stranger to working from home and can be incredibly productive. For the last 6 months of last year I contracted to a major music distribution company and worked from home with occasional in-house and Skype meetings.

So if I fit your needs, get in touch!