Skip to content

Instantly share code, notes, and snippets.

@moollaza
Last active February 16, 2017 11:13
Show Gist options
  • Select an option

  • Save moollaza/0f935d1b11eb66f1f2e8 to your computer and use it in GitHub Desktop.

Select an option

Save moollaza/0f935d1b11eb66f1f2e8 to your computer and use it in GitHub Desktop.

Revisions

  1. moollaza revised this gist Nov 30, 2015. No changes.
  2. moollaza revised this gist Nov 25, 2015. No changes.
  3. moollaza created this gist Nov 25, 2015.
    42 changes: 42 additions & 0 deletions RandomPerson.pm
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@
    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;
    24 changes: 24 additions & 0 deletions RandomPerson.t
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    #!/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;

    41 changes: 41 additions & 0 deletions random_person.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,41 @@
    (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));