PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/DDG/Spice/Congress.pm

http://github.com/duckduckgo/zeroclickinfo-spice
Perl | 160 lines | 140 code | 15 blank | 5 comment | 4 complexity | 1c8c28b34c5352610ffcad53dc033689 MD5 | raw file
Possible License(s): Apache-2.0
  1. package DDG::Spice::Congress;
  2. # ABSTRACT: Return current Congress members for given state.
  3. use strict;
  4. use DDG::Spice;
  5. spice to => 'http://congress.api.sunlightfoundation.com/legislators?apikey={{ENV{DDG_SPICE_CONGRESS_APIKEY}}}&chamber=$1&state=$2&per_page=all';
  6. spice from => '([^/]+)/?(?:([^/]+)/?(?:([^/]+)|)|)';
  7. spice wrap_jsonp_callback => 1;
  8. triggers startend => 'senate', 'senators', 'senator', 'representatives', 'reps', 'house', 'representative';
  9. sub rtrim($);
  10. handle query_lc => sub {
  11. my (%states) = (
  12. 'alabama' => 'al',
  13. 'al' => 'al',
  14. 'alaska' => 'ak',
  15. 'ak' => 'ak',
  16. 'arizona' => 'az',
  17. 'az' => 'az',
  18. 'arkansas' => 'ar',
  19. 'ar' => 'ar',
  20. 'california' => 'ca',
  21. 'ca' => 'ca',
  22. 'colorado' => 'co',
  23. 'co' => 'co',
  24. 'connecticut' => 'ct',
  25. 'ct' => 'ct',
  26. 'delaware' => 'de',
  27. 'de' => 'de',
  28. 'florida' => 'fl',
  29. 'fl' => 'fl',
  30. 'georgia' => 'ga',
  31. 'ga' => 'ga',
  32. 'hawaii' => 'hi',
  33. 'hi' => 'hi',
  34. 'idaho' => 'id',
  35. 'id' => 'id',
  36. 'illinois' => 'il',
  37. 'il' => 'il',
  38. 'indiana' => 'in',
  39. 'in' => 'in',
  40. 'iowa' => 'ia',
  41. 'ia' => 'ia',
  42. 'kansas' => 'ks',
  43. 'ks' => 'ks',
  44. 'kentucky' => 'ky',
  45. 'ky' => 'ky',
  46. 'louisiana' => 'la',
  47. 'la' => 'la',
  48. 'maine' => 'me',
  49. 'me' => 'me',
  50. 'maryland' => 'md',
  51. 'md' => 'md',
  52. 'massachusetts' => 'ma',
  53. 'ma' => 'ma',
  54. 'michigan' => 'mi',
  55. 'mi' => 'mi',
  56. 'minnesota' => 'mn',
  57. 'mn' => 'mn',
  58. 'mississippi' => 'ms',
  59. 'ms' => 'ms',
  60. 'missouri' => 'mo',
  61. 'mo' => 'mo',
  62. 'montana' => 'mt',
  63. 'mt' => 'mt',
  64. 'nebraska' => 'ne',
  65. 'ne' => 'ne',
  66. 'nevada' => 'nv',
  67. 'nv' => 'nv',
  68. 'new hampshire' => 'nh',
  69. 'nh' => 'nh',
  70. 'new jersey' => 'nj',
  71. 'nj' => 'nj',
  72. 'new mexico' => 'nm',
  73. 'nm' => 'nm',
  74. 'new york' => 'ny',
  75. 'ny' => 'ny',
  76. 'north carolina' => 'nc',
  77. 'nc' => 'nc',
  78. 'north dakota' => 'nd',
  79. 'nd' => 'nd',
  80. 'ohio' => 'oh',
  81. 'oh' => 'oh',
  82. 'oklahoma' => 'ok',
  83. 'ok' => 'ok',
  84. 'oregon' => 'or',
  85. 'or' => 'or',
  86. 'pennsylvania' => 'pa',
  87. 'pa' => 'pa',
  88. 'rhode island' => 'ri',
  89. 'ri' => 'ri',
  90. 'south carolina' => 'sc',
  91. 'sc' => 'sc',
  92. 'south dakota' => 'sd',
  93. 'sd' => 'sd',
  94. 'tennessee' => 'tn',
  95. 'tn' => 'tn',
  96. 'texas' => 'tx',
  97. 'tx' => 'tx',
  98. 'utah' => 'ut',
  99. 'ut' => 'ut',
  100. 'vermont' => 'vt',
  101. 'vt' => 'vt',
  102. 'virginia' => 'va',
  103. 'va' => 'va',
  104. 'washington' => 'wa',
  105. 'wa' => 'wa',
  106. 'west virginia' => 'wv',
  107. 'wv' => 'wv',
  108. 'wisconsin' => 'wi',
  109. 'wi' => 'wi',
  110. 'wyoming' => 'wy',
  111. 'wy' => 'wy'
  112. );
  113. # Matches against special variable `$_`, which contains query
  114. my ($state1, $chamber, $state2) = /^(.*)(?:\s)*(senate|senators|senator|representatives|representative|reps|house)(?:\s)*(.*)$/g;
  115. my ($state);
  116. # Regex returns a space after $1, so we must remove it before hashing
  117. $state1 = rtrim($state1);
  118. if (exists $states{$state1}) {
  119. $state = $states{$state1};
  120. } elsif (exists $states{$state2}) {
  121. $state = $states{$state2};
  122. } else {
  123. return;
  124. }
  125. my (%chambers) = (
  126. "senators" => "senate",
  127. "senate" => "senate",
  128. "senator" => "senate",
  129. "reps" => "house",
  130. "representatives" => "house",
  131. "representative" => "house",
  132. "house" => "house"
  133. );
  134. $chamber = $chambers{$chamber};
  135. # state needs to be uppercase for Sunlight API
  136. return $chamber, uc $state if ($chamber && $state);
  137. return;
  138. };
  139. 1;
  140. sub rtrim($) {
  141. # Removes trailing whitespace
  142. my $string = shift;
  143. $string =~ s/\s+$//;
  144. return $string;
  145. }