PageRenderTime 41ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/DDG/Spice/SalesTax.pm

http://github.com/duckduckgo/zeroclickinfo-spice
Perl | 55 lines | 35 code | 10 blank | 10 comment | 6 complexity | d5777caf325d19599127984f96900641 MD5 | raw file
Possible License(s): Apache-2.0
  1. package DDG::Spice::SalesTax;
  2. #ABSTRACT: Returns the sales tax for any state (not including territories) in the United States.
  3. use strict;
  4. use DDG::Spice;
  5. use Locale::SubCountry;
  6. triggers any => 'sales tax for', 'sales tax in', 'sales tax';
  7. spice to => 'http://dev.snapcx.io:9100/taxv1/getStateTaxRate?request_id=DDG_Requestor&state=$1';
  8. spice wrap_jsonp_callback => 1;
  9. spice is_cached => 1;
  10. #Create US SubCountry object
  11. my $US = new Locale::SubCountry("US");
  12. # Handle statement
  13. handle remainder_lc => sub {
  14. # No need to check US country, as this IA will run for all international users. I am still keeping commented out code as FYI.
  15. # return unless $loc && $loc->{country_code} eq 'US';
  16. my ($state, $stateCode); #Define vars
  17. s/^what is (the)?//g; # strip common words
  18. return unless $_; # Guard against "no answer"
  19. # Guard against, to sending zipcodes as parameters. We have different IA.
  20. if ($_ =~ /^[0-9]{5}$/){
  21. return;
  22. }
  23. # Guard against, sales tax holiday. We have different IA.
  24. if ($_ =~ /((?i)holiday)/){
  25. return;
  26. }
  27. # Washington D.C is a district and is not supported by the SubCountry package.
  28. if(m/\b(washington\s(dc|d\.c))\b/i) {
  29. $state = "Washington D.C";
  30. $stateCode = "DC";
  31. } else {
  32. # $US->full_name returns the full state name based on the ISO3166 code
  33. $stateCode = $_;
  34. $state = $US->full_name($_); # Check for state using ISO code (PA)
  35. if($state eq "unknown") {
  36. $state = $US->full_name($US->code($_)); # If state is "unknown" search for code using full state name (Pennsylvania)
  37. $stateCode = $US->code($_);
  38. }
  39. }
  40. # error checking
  41. return if $state eq "unknown";
  42. return unless (defined $state and $stateCode);
  43. return $stateCode; # return result
  44. };
  45. 1;