PageRenderTime 38ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/DDG/Spice/Transit/PATH.pm

http://github.com/duckduckgo/zeroclickinfo-spice
Perl | 41 lines | 26 code | 7 blank | 8 comment | 2 complexity | 6a4aed096b2098bf8f030ed78a787615 MD5 | raw file
Possible License(s): Apache-2.0
  1. package DDG::Spice::Transit::PATH;
  2. # ABSTRACT: Information on next trains on PATH (New York + New Jersey)
  3. use strict;
  4. use DDG::Spice;
  5. spice to => 'http://njt-api1.appspot.com/path/times/$1';
  6. spice wrap_jsonp_callback => 1;
  7. spice proxy_cache_valid => "418 1d";
  8. #load a list of stops so we don't trigger this if we don't get path stops
  9. #(the triggers are similar to other transit IAs)
  10. my @stops = share('stops.txt')->slurp;
  11. #check if the stop name is in the list of stops
  12. #(using the same matching algorithm as the backend)
  13. sub is_stop {
  14. foreach my $stop (@stops){
  15. return 1 if index(lc $stop, lc $_[0]) > -1;
  16. }
  17. return;
  18. };
  19. triggers any => "next train", "train times", "train schedule", "path", "trans hudson";
  20. handle remainder => sub {
  21. return unless /(?:from |to )?(.+) (to|from) (.+)/;
  22. my $orig = $1;
  23. my $dest = $3;
  24. if (is_stop($orig) and is_stop($dest)){
  25. #lowercase the stop names found in the query and change the spaces to dashes
  26. my $orig = join "-", map { lc } split /\s+/, $orig;
  27. my $dest = join "-", map { lc } split /\s+/, $dest;
  28. #if the word between the two stop names is "to", then we're going from the first stop to the second
  29. #if it's "from", then we're going from the second stop to the first
  30. return $2 eq 'to' ? ($orig, $dest) : ($dest, $orig);
  31. }
  32. return;
  33. };
  34. 1;