MetaCPAN JavaScript API

Apr 15, 2013 / By Yanick Champoux

Tags: , ,

Sometimes, it’s humongous revolutions. Most of the time, it’s itsy bitsy evolutionary steps. Today’s hack definitively sits in the second category, but I have the feeling that it’s a little dab of abstraction that is going to provide a lot of itch relief.

You see, MetaCPAN does not only have a pretty face, but also has a smashing backend that can be used straight-up for fun and profit.

Accessing REST endpoints is not hard, but it’s a little bit of a low-level chore. In Perl space, there is already MetaCPAN::API to abstract.

my $ua = LWP::UserAgent;
my $me = decode_json(
    $ua->get( 'https://api.metacpan.org/author/YANICK'
)->content;
[/perl>

<p>into</p>

1
my $mcpan = MetaCPAN::API;
my $me = $mcpan->author('YANICK');

In JavaScript-land? Well, there was jQuery, of course:

$.get('https://api.metacpan.org/author/YANICK').success( function(data) {
    alert( 'hi there ' + data.name );
});

But now there is also metacpan.js:

$.metacpan().author('YANICK').success( function(data) {
    alert( 'hi there ' + data.name );
});

The plugin is still very simple and only implements author(), module(), release(), and file(). And each of these methods is nothing but a glorified wrapper around the underlying $.ajax() calls. But then again, isn’t the road to heaven paved with glorified wrappers? (Which could be more of an indication of the terrible littering habits of angels than anything else, mind you.)

Enjoy (and/or fork, depending on how much the current code is already scratching your own itch)!

Leave a Reply

  • (will not be published)

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>