/lib/DDG/Spice/Transit/NJT.pm

https://github.com/pardocz/zeroclickinfo-spice · Perl · 50 lines · 35 code · 8 blank · 7 comment · 2 complexity · 7c42f4d25110f5f5be3335d12a69d290 MD5 · raw file

  1. package DDG::Spice::Transit::NJT;
  2. use DDG::Spice;
  3. primary_example_queries "next train from Metropark to New York Penn";
  4. secondary_example_queries "train times to Trenton from Secaucus";
  5. description "Lookup the next NJ Transit train going your way";
  6. name "NJT";
  7. source "NJT";
  8. code_url "https://github.com/duckduckgo/zeroclickinfo-spice/blob/master/lib/DDG/Spice/Transit/NJT.pm";
  9. topics "everyday";
  10. category "time_sensitive";
  11. attribution twitter => 'mattr555',
  12. github => ['https://github.com/mattr555/', 'Matt Ramina'];
  13. spice to => 'http://njt-api.appspot.com/njt/times/$1';
  14. spice wrap_jsonp_callback => 1;
  15. spice proxy_cache_valid => "418 1d";
  16. #load a list of stops so we don't trigger this if we don't get njt stops
  17. #(the triggers are similar to SEPTA's)
  18. my @stops = share('stops.txt')->slurp;
  19. #check if the stop name is in the list of stops
  20. #(using the same matching algorithm as the backend)
  21. sub is_stop {
  22. foreach my $stop (@stops){
  23. return 1 if index(lc $stop, lc $_[0]) > -1;
  24. }
  25. return;
  26. };
  27. triggers any => "next train", "train times", "train schedule", "njt", "nj transit", "new jersey transit";
  28. handle remainder => sub {
  29. return unless /(?:from |to )?(.+) (to|from) (.+)/;
  30. my $orig = $1;
  31. my $dest = $3;
  32. if (is_stop($orig) and is_stop($dest)){
  33. #lowercase the stop names found in the query and change the spaces to dashes
  34. my $orig = join "-", map { lc } split /\s+/, $orig;
  35. my $dest = join "-", map { lc } split /\s+/, $dest;
  36. #if the word between the two stop names is "to", then we're going from the first stop to the second
  37. #if it's "from", then we're going from the second stop to the first
  38. return $2 eq 'to' ? ($orig, $dest) : ($dest, $orig);
  39. }
  40. return;
  41. };
  42. 1;