PageRenderTime 40ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/DDG/Spice/RandWord.pm

http://github.com/duckduckgo/zeroclickinfo-spice
Perl | 49 lines | 35 code | 11 blank | 3 comment | 6 complexity | e66e8bd53ff68607b0f7a93b1968dcbb MD5 | raw file
Possible License(s): Apache-2.0
  1. package DDG::Spice::RandWord;
  2. # ABSTRACT: Shows a random word
  3. use strict;
  4. use DDG::Spice;
  5. use List::Util 'min';
  6. spice from => '(?:([0-9]+)\-([0-9]+)\-([0-9]+))';
  7. spice to => 'http://api.wordnik.com/v4/words.json/randomWords?minLength=$1&maxLength=$2&limit=$3&api_key={{ENV{DDG_SPICE_WORDNIK_APIKEY}}}&callback={{callback}}';
  8. spice proxy_cache_valid => "418 1d";
  9. spice content_type_javascript => 1;
  10. triggers any => "random word", "random words";
  11. spice alt_to => {
  12. rand_word_fetch_id => {
  13. to => 'http://api.wordnik.com:80/v4/word.json/$1/definitions?limit=1&includeRelated=false&sourceDictionaries=all&useCanonical=false&includeTags=false&api_key={{ENV{DDG_SPICE_WORDNIK_APIKEY}}}',
  14. }
  15. };
  16. handle query_lc => sub {
  17. my $minlen = 1;
  18. my $maxlen = 100;
  19. my $limit = 10;
  20. my $maxLimit = 500;
  21. if ($_ =~ m/(?<limit>\d+)? ?random word(s)? ?((?<min>\d+)\-(?<max>\d+))?$/) {
  22. if (!$1 && !$2) {
  23. # 'random word'
  24. $limit = 1;
  25. } elsif ($1) {
  26. # '15 random words'
  27. $limit = min($1, $maxLimit);
  28. }
  29. $minlen = $+{min} if $+{min};
  30. $maxlen = $+{max} if $+{max};
  31. if ($minlen > $maxlen) {
  32. ($minlen, $maxlen) = ($maxlen, $minlen)
  33. }
  34. return join('-', $minlen, $maxlen, $limit);
  35. }
  36. return;
  37. };
  38. 1;