Skip to content

Instantly share code, notes, and snippets.

@Kimtaro
Created March 26, 2009 17:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kimtaro/86208 to your computer and use it in GitHub Desktop.
Save Kimtaro/86208 to your computer and use it in GitHub Desktop.
# Run like so: perl api_functional.pl api.smart.fm YOUR_API_KEY
use strict;
use warnings;
use utf8;
use Test::More qw(no_plan);
use LWP::UserAgent;
use JSON;
use XML::Simple;
use Data::Dumper;
use Time::HiRes qw(gettimeofday tv_interval);
use URI::Escape qw(uri_escape_utf8);
sub get {
my ($url) = @_;
my $ua = LWP::UserAgent->new;
print "Requesting $url\n";
my $start = [gettimeofday];
my $r = $ua->get($url);
print 'Took '. tv_interval($start, [gettimeofday]) ." seconds\n";
return $r;
}
sub is_success {
my ($r, $name) = @_;
ok($r->is_success, "$name should return 200 (was ". $r->code .")");
}
sub is_faster_than {
my ($r, $max, $name) = @_;
return unless ok($r->header('X-Runtime'), "$name X-Runtime should be included");
$max *= 1000; # Milliseconds
my $runtime = $r->header('X-Runtime');
$runtime =~ s/^ (\d+) ms $/$1/x;
cmp_ok($runtime, '<', $max,
"$name X-Runtime should be faster than $max milliseconds (was ". $r->header('X-Runtime') .')');
}
my $BASE = "http://$ARGV[0]";
my $KEY = $ARGV[1];
my $TEXT = <<'EOT';
By 1988, house music had exploded in the UK, and acid house was increasingly popular.[50] There was also a long established warehouse party subculture based around the sound system scene. In 1988, the music played at warehouse parties was predominantly house. That same year, the Balearic party vibe associated with Ibiza based DJ Alfredo Fiorito was transported to London, when Danny Rampling and Paul Oakenfold opened the clubs Shoom and Spectrum, respectively. Both night spots quickly became synonymous with acid house, and it was during this period that the use of MDMA, as a party drug, started to gain prominence. Other important UK clubs at this time included Back to Basics in Leeds, Sheffield's Leadmill and Music Factory, and in Manchester The Haçienda, where Mike Pickering and Graeme Park's Friday night spot, called Nude, was an important proving ground for American EDM, including the first techno from Detroit.[51] Acid house party fever escalated in London and Manchester, and it was fast becoming a cultural phenomenon. MDMA-fueled club goers, faced with 2 A.M. closing hours, sought refuge in the warehouse party scene that ran all night. To escape the attention of the press, and the authorities, this after-hours activity quickly went underground. Within a year, however, up to 10,000 people at a time were attending the first commercially organized mass parties, called raves, and a media storm ensued.[52]
EOT
# Text from http://en.wikipedia.org/wiki/Techno
# List items for #9900, 36 items, with video and transcript
{
my ($r, $xml, $json);
# XML
$r = get("$BASE/lists/9900/items.xml?per_page=50");
is_success($r, 'List items (XML)');
is_faster_than($r, 1, 'List items (XML)');
#print $r->content;
$xml = XMLin($r->content);
my @xml_ids = sort keys %{$xml->{items}->{item}};
is(scalar @xml_ids, 36, 'There should be 36 items (XML)');
# JSON
$r = get("$BASE/lists/9900/items.json?per_page=50");
is_success($r, 'List items (JSON)');
is_faster_than($r, 1, 'List items (JSON)');
$json = from_json($r->content);
my @json_ids = sort map {$_->{id}} @{$json};
is(scalar @json_ids, 36, 'There should be 36 items (JSON)');
is_deeply(\@xml_ids, \@json_ids, 'XML and JSON items should be identical');
}
# GET Item extract, English - Japanese
{
my ($r, $xml, $json);
my $text = uri_escape_utf8( $TEXT );
# XML
$r = get("$BASE/items/extract.xml?" .
"text=$text&language=en&translation_language=ja&words_only=true&api_key=$KEY");
is_success($r, 'Extract (XML)');
is_faster_than($r, 2, 'Extract (XML)');
$xml = XMLin($r->content);
my @xml_words = sort map {$_ =~ s/^\s+|\s+$//gm; $_} @{$xml->{word}};
is(scalar @xml_words, 110, 'There should be 110 items (XML)');
# JSON
$r = get("$BASE/items/extract.json?" .
"text=$text&language=en&translation_language=ja&words_only=true&api_key=$KEY");
is_success($r, 'Extract (JSON)');
is_faster_than($r, 2, 'Extract (JSON)');
$json = from_json($r->content);
my @json_words = sort keys %{$json};
is(scalar @json_words, 110, 'There should be 110 items (JSON)');
unless ( is_deeply(\@xml_words, \@json_words, 'XML and JSON words should be identical') ) {
print Dumper \@xml_words, \@json_words;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment