/lib/DDG/Spice/Shorten.pm

https://github.com/pardocz/zeroclickinfo-spice · Perl · 34 lines · 27 code · 6 blank · 1 comment · 3 complexity · 7f3f3f02a538ed6a4827b2a12edb3ba9 MD5 · raw file

  1. package DDG::Spice::Shorten;
  2. # ABSTRACT: Return a shortened version of a URL.
  3. use DDG::Spice;
  4. primary_example_queries "shorten http://www.duckduckgo.com/about.html";
  5. secondary_example_queries "url shorten www.github.com/explore";
  6. description "Shorten URLs using the is.gd API";
  7. name "Shorten";
  8. icon_url "/i/is.gd.ico";
  9. source "Shorten";
  10. code_url "https://github.com/duckduckgo/zeroclickinfo-spice/blob/master/lib/DDG/Spice/Shorten.pm";
  11. topics "social";
  12. category "computing_tools";
  13. attribution github => ['https://github.com/danjarvis','Dan Jarvis'],
  14. twitter => ['http://twitter.com/danjarvis','danjarvis'];
  15. spice from => '([^/]+)/(.*)';
  16. spice to => 'http://is.gd/create.php?format=json&url=$1%3A%2F%2F$2&callback={{callback}}';
  17. triggers any => 'shorten', 'shorten url', 'short url', 'url shorten';
  18. my %connector_words = map { $_ => 1 } qw(for of);
  19. handle remainder => sub {
  20. my @query_words = grep { !$connector_words{$_} } split /\s+/;
  21. my $q = shift @query_words;
  22. return if (@query_words); # We should only work for a single 'word' in the query.
  23. $q =~ m|(?<schema>https?)?(?:://)?(?<loc>.+)|;
  24. my ($schema, $location) = ($+{'schema'}, $+{'loc'});
  25. return unless defined $location && $location =~ /\./; # Location part should contain at least one dot.
  26. return ($schema || 'http'), $location;
  27. };
  28. 1;