Instantly share code, notes, and snippets.

Embed
What would you like to do?
DuckDuckHack Webcast - Making a "Random Person" Spice Instant Answer
(function (env) {
"use strict";
env.ddg_spice_random_person = function(api_result){
if (!api_result) {
return Spice.failed('random_person');
}
Spice.add({
id: "random_person",
name: "Answer",
data: api_result.results,
meta: {
sourceName: "RandomUser",
sourceUrl: 'http://randomuser.me'
},
normalize: function(item) {
return {
title: item.user.name.first + " " + item.user.name.last,
subtitle: item.user.email,
image: item.user.picture.medium,
record_data: item.user,
record_keys: [
"username",
"password",
"dob",
"phone"
]
};
},
templates: {
group: 'info',
options: {
content: 'record',
moreAt: true
}
}
});
};
}(this));
package DDG::Spice::RandomPerson;
use DDG::Spice;
spice is_cached => 0;
spice proxy_cache_valid => "418 1d";
spice wrap_jsonp_callback => 1;
spice to => 'http://api.randomuser.me/';
triggers start => 'random person';
# spice to => 'http://api.randomuser.me/?gender=$1';
# triggers start => 'random';
# sub random_gender {
# my @genders = ("male", "female");
# my $random_gender = $genders[rand @genders];
# return $random_gender;
# }
handle remainder_lc => sub {
return "" unless $_;
return;
# my $remainder = $_;
# my $gender;
# # srand();
# $gender = $1 if $remainder =~ m/(female|male)/;
# $gender = random_gender() if $remainder =~ m/person|user/;
# return unless $gender;
# return $gender;
};
1;
#!/usr/bin/env perl
use strict;
use warnings;
use Test::More;
use DDG::Test::Spice;
spice is_cached => 1;
ddg_spice_test(
[qw( DDG::Spice::RandomPerson)],
'random person' => test_spice(
'/js/spice/random_person/',
call_type => 'include',
caller => 'DDG::Spice::RandomPerson',
is_cached => 0
),
'random people' => undef,
'random' => undef
);
done_testing;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment