Saying stuff about stuff.

#FirstSevenLanguages

Lingo (Macromedia Director)
ActionScript
JavaScript
VBScript
SQL
CFScript
Ruby

Ruby Medium API Client

Medium have a few official API clients (Go / Node / Python) but there isn’t one for Ruby and although there already appear to be (at least) a couple of “unofficial” Ruby Medium API clients I wasn’t entirely convinced by them (lack of tests / lack of source code).

After looking over Medium’s API docs and their existing clients — each of which is barely more than a single file — I decided to write my own Ruby Medium API client. It does little more than communicate with the correct endpoints and convert to and from JSON; I’d even argue that it’s thinner than the official clients as, for example, it doesn’t attempt to know the list of valid publishing licences or statuses or the valid attributes for a post — which seems like a losing game at the best of times but particularly for an “unofficial” client.

Medium’s API docs are particularly useful as they provide example HTTP responses for each endpoint making it easy to test by mocking responses with Webmock — in fact my first work on the library was test-first on a train with no internet*. I’ve been using it for a while as I now cross-post on Medium, it’s working well and helping to smooth my publishing flow (as opposed to doing it manually which is time-consuming and error-prone).

Here’s how to use it to create a post that points back to a canonical URL (an access token is required):

client = Medium::Client.new(access_token)
client.create_post(
  canonicalUrl: 'http://example.com/hello-world',
  content: '<p>Some interesting words.</p>',
  contentFormat: 'html',
  publishStatus: 'draft',
  title: 'Hello World',
)

* Pro-tip: prepare for an internet-less train journey by queueing up docs that might be required. Also, always use the most excellent DevDocs which feels like a web app from the future.

Peity and Interactive Data Visualization for the Web

Peity book shot

Did I ever tell you that Peity — “a jQuery plugin for generating progressive <svg> pie, donut, bar and line charts” and my most successful open source project to-date (it currently has more than 3500 stars on GitHub) — was mentioned in a real book made of paper and printed by O’Reilly?

The book is called Interactive Data Visualization for the Web and is available from:

It’s a short and sweet mention of Peity, my favourite remark is:

+10 cuteness points

This news is a couple of years old now (the book was published in 2013!) but it looks like a second edition is coming in 2017.


It’s also nice to see that a friend and fellow New Bamboo alumni is mentioned in the book for his project Dashku.

What happens when null is passed to a JavaScript regular expression?

It’s quite possible that the following is not the expected consequence of passing null to a JavaScript regular expression — it certainly surprised me:

/null/.test(null)
// => true

It looks like extra intentional work is being done to convert the value null to the string "null" whereas my assumption was that, particularly as it’s falsy, it would be interpreted as a blank string.

What’s happening?

I was impressed at how easy it was to find the answer in the ECMA spec that defines this behaviour. It turns out that it is doing extra intentional work to convert null to the string "null".

The spec also describes the following surprising (to me at least) cases:

/undefined/.test(undefined)
// => true

/true/.test(true)
// => true

/false/.test(false)
// => true

/\[object Object\]/.test({ a: 1 })
// => true

/1,2/.test([1, 2])
// => true

One example that does feel natural is converting a number to a string:

/123.45/.test(123.45)
// => true

But even this has some edge cases:

/Infinity/.test(1 / 0)
// => true

/NaN/.test(0 / 0)
// => true

Back to my problem, what’s the fix?

I could imagine this introducing a type of Scunthorpe problem — albeit with a relatively small window of opportunity — and for the non-falsy cases is the sort of issue that a type-checking system like Flow could help mitigate. Fortunately in my case the value was either a string or null so it was enough to default to an empty string:

var value = null
/null/.test(value || '')
// => false

Introducing Screamshot

Screamshot

I’ve been playing with programmatically fetching screenshots for years but never felt happy with the end result. Using PhantomJS directly seemed cumbersome and things didn’t work reliably for me when they reached production. It was whilst playing with Aesthetic that I learned how easy it was to use Capybara outside of a test environment as a tool for saving screenshots — I was struck by a thought:

My next screenshot-fetching app will use PhantomJS via Capybara and Poltergeist and will be just great!

— Me, before I’d written any code.

So I wrote a Rails app that receives a request, creates a database record, returns an identifier, queues up a job, fetches the screenshot and writes it to a file, and finally notifies the requesting app via a webhook that the screenshot is ready. It did seem a bit much for such a “simple” thing but I was quite pleased with it — and webhooks are cool. As usual it brought my server to its knees.

I started wondering about using Heroku which made me think that being synchronous might be the way to go — as in, the motivation was mostly $$$-driven… Anyway, I rewrote it again*.

It’s called Screamshot and can be found on GitHub, this time it’s synchronous and built on Sinatra and PhantomJS (via Capybara/Poltergeist).

Here’s how to use Screamshot:

$ curl "http://API_TOKEN@screamshot.dev/screenshot?url=http://example.com"

It has plenty of room for improvement: it doesn’t appreciate slow ad servers and therefore often times out, the fonts don’t look great, and it’s currently utilising the limited concurrency of WEBrick.

But I still like it*** — and you might too.

* To be honest part of my motivation for making this again is because I like the idea** for the logo!

** I said I like the idea for the logo…

*** Again, mainly just the logo and colour scheme.

This is amazing! I would love to spend my days performing this silliness.

White Wall

Img 3039

Database “postgres” does not exist

I recently encountered a strange error with PostgreSQL on my development machine:

psql: FATAL:  database "postgres" does not exist
DETAIL:  The database subdirectory "base/12641" is missing.

It was strange because most of my databases were working but a couple weren’t. Also, there was a postgres database - though the postgres role didn’t appear to exist. psql worked but psql -l would output the same error.

I didn’t want to lose my local data and spent a long time failing to find a proper fix - I tried a quick reinstall but that didn’t work either. In the end I resorted to blatting the current install and starting again.

How to dump/restore databases and reinstall PostgreSQL on a Mac with Homebrew

Dump any databases you wish to keep and stop PostgreSQL:

$ pg_dump -Fc --no-acl --no-owner my_database > my_database.dump
$ launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist

Better to keep the existing data just in case this process needs to be rolled back:

$ mv /usr/local/var/postgres somewhere/safe

Reinstall and start PostgreSQL:

$ brew uninstall postgresql
$ brew install postgresql
$ launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist

Create and restore the old database(s):

$ createdb my_database
$ pg_restore --no-acl --no-owner -d my_database my_database.dump

If your luck is as good as mine then you should have a fully working PostgreSQL install with your previous database(s).

Why?

I think this problem was caused by me not reading the instructions and blindly upgrading PostgreSQL with brew upgrade postgresql to go from 9.4 to 9.5. Or was it 9.3 to 9.4…

The proper way to migrate PostgreSQL data between upgrades

Here’s how to properly upgrade PostgreSQL data using pg_upgrade - though I’m sure I did this!

A Homage to jQuery Plugins

I wrote a little blog post on my company blog: a homage to jQuery plugins. It includes the phrase “front-end toolbox” which triggered my euphemism alarm but managed to slip through everyone else’s.

js-model-rails

Last week I put together a Gem for use with the new Rails 3.1 asset pipeline, it packages up js-model and means you’ll never have right-click-save-as-move-to-directory ever again. Now all you have to do is include it in your Gemfile:

gem 'js-model-rails'

And require js-model in your application.js:

//= require js-model

Being able to manage JavaScript and CSS dependencies using Gems is a nice feature but it really starts to get interesting when you consider how a little backend integration can ease your frontend development. For instance, at the moment js-model-rails does something I always forget to do myself:

config.active_record.include_root_in_json = false

But in the future it’ll read your include_root_in_json setting and configure the JavaScript accordingly.

There are so many areas where having access to the backend environment will make working on the frontend much simpler. I’m not suggesting that every JavaScript library needs a corresponding Gem but in this case - and no doubt many others - it’s the start of a beautiful friendship.

Here’s the source on Github.