PageRenderTime 39ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/DDG/Spice/Shorten.pm

http://github.com/duckduckgo/zeroclickinfo-spice
Perl | 23 lines | 17 code | 5 blank | 1 comment | 3 complexity | b8dfb26a8dd0603ac2f768932f8fd579 MD5 | raw file
Possible License(s): Apache-2.0
  1. package DDG::Spice::Shorten;
  2. # ABSTRACT: Return a shortened version of a URL.
  3. use strict;
  4. use DDG::Spice;
  5. spice from => '([^/]+)/(.*)';
  6. spice to => 'https://is.gd/create.php?format=json&url=$1%3A%2F%2F$2&callback={{callback}}';
  7. triggers any => 'shorten', 'shorten url', 'short url', 'url shorten';
  8. my %connector_words = map { $_ => 1 } qw(for of);
  9. handle remainder => sub {
  10. my @query_words = grep { !$connector_words{$_} } split /\s+/;
  11. my $q = shift @query_words;
  12. return if (@query_words); # We should only work for a single 'word' in the query.
  13. $q =~ m|(?<schema>https?)?(?:://)?(?<loc>.+)|;
  14. my ($schema, $location) = ($+{'schema'}, $+{'loc'});
  15. return unless defined $location && $location =~ /\./; # Location part should contain at least one dot.
  16. return ($schema || 'http'), $location;
  17. };
  18. 1;