Last active
February 16, 2017 11:13
-
-
Save moollaza/0f935d1b11eb66f1f2e8 to your computer and use it in GitHub Desktop.
DuckDuckHack Webcast - Making a "Random Person" Spice Instant Answer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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