pitosalas-govsdk 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/README.textile CHANGED
@@ -22,22 +22,46 @@ Unfortunately for now, each of the different services request that you use it's
22
22
 
23
23
  h2. IDs
24
24
 
25
- There are various IDs (identifiers assigned by some outside, real world agency to some outside real world thing) throughout the government. One you know is a drivers license number assigned to a person. Within our universe here are some of the IDs:
25
+ There are various IDs (identifiers assigned by some outside, real world agency to some outside real world thing) throughout the government. One you know is a drivers license number assigned to a person.
26
+
27
+ h2. Basic usage
28
+
29
+ Here is a very simplistic minimal example:
30
+ <pre>
31
+ <code>
32
+ # You always have to start off by declaring that you want to use Govsdk. You need to supply one or more of your API keys.
33
+ GovSdk.init :sunlight => "enter your API key..."
34
+
35
+ #Lookup a congress person "barney" "frank". The call returns an array of matching congresspeople
36
+ cp = CongressPerson.find_by_names("barney", "frank")
37
+
38
+ # print the photo url of the first congress person matching
39
+ puts cp[0].photo_url
40
+ </code>
41
+ </pre>
26
42
 
27
- # CrpId - Center for responsible politics IDs for congress persons
28
-
29
- In many cases none of the APIs furnish a useful ID.
30
43
 
31
44
  h1. KEY CLASSES
32
45
 
46
+ # CongressPerson - Work with legislators: senators and congressmen
47
+
48
+ #### self.find_by_name - return an array of CongressPersons
49
+ #### self.find_by_zipcode - return an array of CongressPersons
50
+ #### self.find_by_crp_id - return the CongressPerson with specified crp id
51
+
52
+ h2. Internal classes
53
+
33
54
  # APIMgr - Parent class of each of the manager classes below
34
55
  ## NewYorkTimesAPI - Class managing access to the New York Times API
35
56
  ## SunlightAPI - Class managing access to the Sunlight API
36
57
  ## SpenSecretsAPI - Class managing the OpenSecrets API
37
58
  ### Methods: setAPIKey/getAPIKey
38
59
 
39
- # CongressPerson - Work with legislators: senators and congressmen
40
-
41
- #### self.find_by_name - return an array of CongressPersons
42
- #### self.find_by_zipcode - return an array of CongressPersons
43
- #### self.find_by_crp_id - return the CongressPerson with specified crp id
60
+
61
+
62
+ h1. Utils
63
+
64
+ There are lots of simple transformations which come up in using these
65
+ APIs and so for convenience sake some of those are built right into this library
66
+
67
+ Utils.lookup_state_name(string) -> two letter state code for that state
data/VERSION.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
2
  :minor: 0
3
- :patch: 4
3
+ :patch: 5
4
4
  :major: 0
@@ -32,7 +32,7 @@ class CongressPerson < GovSdkBase
32
32
 
33
33
  attr_reader :firstname, :lastname, :party, :state, :congress_office, :phone, :fax, :emai,
34
34
  :website, :webform, :website_url, :bioguide_id, :congresspedia_url, :state_machine_id,
35
- :district, :title, :govtrack_id, :crp_id, :nickname, :votesmart_id
35
+ :district, :title, :govtrack_id, :crp_id, :nickname, :votesmart_id, :votesmart_id
36
36
 
37
37
  # Maps specific set of CongressPerson attributes to Sunlight APIs. Mapping is structured as
38
38
  # "name of attr in CongressPerson object" => "name of hash key in result returned from API call"
@@ -78,7 +78,16 @@ class CongressPerson < GovSdkBase
78
78
  sunlight_hash = GovSdk.sunlight_api.legislators_search(name)
79
79
  sunlight_hash.collect {|leg| convert_to_congressperson(leg["result"]["legislator"])}
80
80
  end
81
-
81
+
82
+ # Return an array of Congress People, searching using first and last name
83
+ def self.find_by_names(firstname, lastname)
84
+ assume_string lastname
85
+ assume_string firstname
86
+ assume_uses_sunl_api
87
+ result_array = GovSdk.sunlight_api.legislators_getlist(:firstname => firstname, :lastname => lastname)
88
+ result_array.collect { |leg| convert_to_congressperson(leg["legislator"])}
89
+ end
90
+
82
91
  def self.find_by_zipcode(zip)
83
92
  raise ArgumentError, 'zipcodes must be Strings' unless zip.is_a?(String)
84
93
  end
data/lib/govsdk_base.rb CHANGED
@@ -36,19 +36,36 @@ class GovSdkBase
36
36
  end
37
37
 
38
38
  def assume_uses_google_api
39
- raise ArgumentError, "Call requires Google API and Google key" + called.shift unless GovSdk.google_api.initialized?
39
+ raise ArgumentError, "Call requires Google API and Google key" + caller.shift unless GovSdk.google_api.initialized?
40
+ end
41
+
42
+ def self.assume_uses_sunl_api
43
+ raise ArgumentError, "Call requires Sunlight API, so it needs you to set the Sunlight API key" + caller.shift unless GovSdk.sunlight_api.initialized?
44
+ end
45
+
46
+ def assume_uses_sunl_api
47
+ self.assume_uses_sunl_api
40
48
  end
41
49
 
42
50
  def assume_nil_or_integer val
43
- raise ArgumentError, "Requires nil or integer argument. Called from " + called.shift unless val.nil? || val.kind_of?(Integer)
51
+ raise ArgumentError, "Requires nil or integer argument. Called from " + caller.shift unless val.nil? || val.kind_of?(Integer)
44
52
  end
45
53
 
46
54
  def self.assume_hash val
47
- raise ArgumentError, "Requires a hash argument. Called from " + called.shift unless val.kind_of?(Hash)
55
+ raise ArgumentError, "Requires a hash argument. Called from " + caller.shift unless val.kind_of?(Hash)
48
56
  end
49
57
 
50
58
  def assume_hash val
51
59
  self.assume_hash val
52
60
  end
53
61
 
62
+ def self.assume_string(val)
63
+ raise ArgumentError, "Requires a string argument. Called from " + caller.shift unless val.kind_of?(String)
64
+ end
65
+
66
+ def assume_string(val)
67
+ self.assume_string val
68
+ end
69
+
70
+
54
71
  end
data/lib/govsdkgem.rb CHANGED
@@ -31,3 +31,4 @@ require 'congress_person'
31
31
  require 'vote_smart_api'
32
32
  require 'google_api'
33
33
  require 'sunlight_api'
34
+ require 'util'
data/lib/util.rb ADDED
@@ -0,0 +1,110 @@
1
+ =begin
2
+ * Name: util.rb
3
+ * Description: Small handy translators to support users of govsdk
4
+ * Author: Pito Salas
5
+ * Copyright: (c) R. Pito Salas and Associates, Inc.
6
+ * Date: January 2009
7
+ * License: GPL
8
+
9
+ This file is part of GovSDK.
10
+
11
+ GovSDK is free software: you can redistribute it and/or modify
12
+ it under the terms of the GNU General Public License as published by
13
+ the Free Software Foundation, either version 3 of the License, or
14
+ (at your option) any later version.
15
+
16
+ GovSDK is distributed in the hope that it will be useful,
17
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
18
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
+ GNU General Public License for more details.
20
+
21
+ You should have received a copy of the GNU General Public License
22
+ along with GovSDK. If not, see <http://www.gnu.org/licenses/>.
23
+
24
+ require "ruby-debug"
25
+ Debugger.settings[:autolist] = 1 # list nearby lines on stop
26
+ Debugger.settings[:autoeval] = 1
27
+ Debugger.start
28
+
29
+ =end
30
+
31
+ STATE_ABBR = {
32
+ 'AL' => 'Alabama',
33
+ 'AK' => 'Alaska',
34
+ 'AS' => 'America Samoa',
35
+ 'AZ' => 'Arizona',
36
+ 'AR' => 'Arkansas',
37
+ 'CA' => 'California',
38
+ 'CO' => 'Colorado',
39
+ 'CT' => 'Connecticut',
40
+ 'DE' => 'Delaware',
41
+ 'DC' => 'District of Columbia',
42
+ 'FM' => 'Micronesia1',
43
+ 'FL' => 'Florida',
44
+ 'GA' => 'Georgia',
45
+ 'GU' => 'Guam',
46
+ 'HI' => 'Hawaii',
47
+ 'ID' => 'Idaho',
48
+ 'IL' => 'Illinois',
49
+ 'IN' => 'Indiana',
50
+ 'IA' => 'Iowa',
51
+ 'KS' => 'Kansas',
52
+ 'KY' => 'Kentucky',
53
+ 'LA' => 'Louisiana',
54
+ 'ME' => 'Maine',
55
+ 'MH' => 'Islands1',
56
+ 'MD' => 'Maryland',
57
+ 'MA' => 'Massachusetts',
58
+ 'MI' => 'Michigan',
59
+ 'MN' => 'Minnesota',
60
+ 'MS' => 'Mississippi',
61
+ 'MO' => 'Missouri',
62
+ 'MT' => 'Montana',
63
+ 'NE' => 'Nebraska',
64
+ 'NV' => 'Nevada',
65
+ 'NH' => 'New Hampshire',
66
+ 'NJ' => 'New Jersey',
67
+ 'NM' => 'New Mexico',
68
+ 'NY' => 'New York',
69
+ 'NC' => 'North Carolina',
70
+ 'ND' => 'North Dakota',
71
+ 'OH' => 'Ohio',
72
+ 'OK' => 'Oklahoma',
73
+ 'OR' => 'Oregon',
74
+ 'PW' => 'Palau',
75
+ 'PA' => 'Pennsylvania',
76
+ 'PR' => 'Puerto Rico',
77
+ 'RI' => 'Rhode Island',
78
+ 'SC' => 'South Carolina',
79
+ 'SD' => 'South Dakota',
80
+ 'TN' => 'Tennessee',
81
+ 'TX' => 'Texas',
82
+ 'UT' => 'Utah',
83
+ 'VT' => 'Vermont',
84
+ 'VI' => 'Virgin Island',
85
+ 'VA' => 'Virginia',
86
+ 'WA' => 'Washington',
87
+ 'WV' => 'West Virginia',
88
+ 'WI' => 'Wisconsin',
89
+ 'WY' => 'Wyoming'
90
+ }
91
+ #
92
+ # Simple collection of Utilities useful to govsdk users
93
+ #
94
+ class Util
95
+
96
+ #
97
+ # 2 letter code for state that most closely matches state_name, or nil.
98
+ #
99
+ def self.lookup_state_name(state_name)
100
+ matches = STATE_ABBR.select { |k,v| v.include? state_name.capitalize}
101
+ if matches.length == 1
102
+ return matches[0][0]
103
+ else
104
+ return nil
105
+ end
106
+ end
107
+
108
+ end
109
+
110
+
@@ -35,6 +35,8 @@ class CongressPersonTest < Test::Unit::TestCase
35
35
  setup do
36
36
  GovSdk.load_apis
37
37
  GovSdk.sunlight_api.key = "4ffa22917ab1ed010a8e681c550c9593"
38
+ GovSdk.google_api.key = "ABQIAAAAyvWaJgF_91PvBZhITx5FDxRIYAcXj39F4zFQfQ2X3IEFURxvMRRUi0aCG6WofnUSRRoI-Pgytm5yUA"
39
+
38
40
  @cpnone = CongressPerson.fuzzy_find_by_name("Pito Salas")
39
41
  @cpkennedy = CongressPerson.fuzzy_find_by_name("Kennedy")
40
42
  @cpkennedy[0].nickname == "Ted" ? @ted_crp_id = @cpkennedy[0].crp_id : @ted_crp_id = @cpkennedy[1].crp_id
@@ -50,6 +52,13 @@ class CongressPersonTest < Test::Unit::TestCase
50
52
  assert ["Edward", "Patrick Joseph"].include? a_kennedy.firstname
51
53
  end
52
54
  end
55
+
56
+ should "locate barney frank by first and last name" do
57
+ cp = CongressPerson.find_by_names("barney", "frank")
58
+ assert_equal 1, cp.length
59
+ assert_equal "26897", cp[0].votesmart_id
60
+ pp cp[0].photo_url
61
+ end
53
62
 
54
63
  context "trying to use CRP from earlier test" do
55
64
  setup do
data/test/test_helper.rb CHANGED
@@ -17,6 +17,7 @@ require 'generic_api'
17
17
  require 'sunlight_api'
18
18
  require 'open_secrets_api'
19
19
  require 'vote_smart_api'
20
+ require 'util'
20
21
 
21
22
  class Test::Unit::TestCase
22
23
  end
@@ -1,4 +1,3 @@
1
- require File.dirname(__FILE__) + '/test_helper'
2
1
  =begin
3
2
  * Name: test_template.rb
4
3
  * Description: Template for GovSdk Tests
@@ -27,6 +26,7 @@ require File.dirname(__FILE__) + '/test_helper'
27
26
  Debugger.settings[:autoeval] = 1
28
27
  Debugger.start
29
28
  =end
29
+ require File.dirname(__FILE__) + '/test_helper'
30
30
 
31
31
  class MyTest < Test::Unit::TestCase
32
32
  context "Test context" do
data/test/util_test.rb ADDED
@@ -0,0 +1,42 @@
1
+ =begin
2
+ * Name: test_template.rb
3
+ * Description: Template for GovSdk Tests
4
+ * Author: Pito Salas
5
+ * Copyright: (c) R. Pito Salas and Associates, Inc.
6
+ * Date: January 2009
7
+ * License: GPL
8
+
9
+ This file is part of GovSDK.
10
+
11
+ GovSDK is free software: you can redistribute it and/or modify
12
+ it under the terms of the GNU General Public License as published by
13
+ the Free Software Foundation, either version 3 of the License, or
14
+ (at your option) any later version.
15
+
16
+ GovSDK is distributed in the hope that it will be useful,
17
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
18
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
+ GNU General Public License for more details.
20
+
21
+ You should have received a copy of the GNU General Public License
22
+ along with GovSDK. If not, see <http://www.gnu.org/licenses/>.
23
+
24
+ require "ruby-debug"
25
+ Debugger.settings[:autolist] = 1 # list nearby lines on stop
26
+ Debugger.settings[:autoeval] = 1
27
+ Debugger.start
28
+ =end
29
+
30
+ require File.dirname(__FILE__) + '/test_helper'
31
+
32
+ class MyTest < Test::Unit::TestCase
33
+ context "Util methods test" do
34
+
35
+ should "Lookup a state" do
36
+ assert_equal "MA", Util.lookup_state_name("Mass")
37
+ assert_equal "MA", Util.lookup_state_name("mass")
38
+ assert_equal nil, Util.lookup_state_name("new")
39
+ assert_equal nil, Util.lookup_state_name("pito")
40
+ end
41
+ end
42
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pitosalas-govsdk
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pito Salas
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-02-18 00:00:00 -08:00
12
+ date: 2009-02-25 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -34,6 +34,7 @@ files:
34
34
  - lib/govsdk.rb
35
35
  - lib/govsdk_base.rb
36
36
  - lib/govsdkgem.rb
37
+ - lib/util.rb
37
38
  - test/congressperson_test.rb
38
39
  - test/google_api_test.rb
39
40
  - test/govsdk_test.rb
@@ -41,6 +42,7 @@ files:
41
42
  - test/sunlight_api_test.rb
42
43
  - test/test_helper.rb
43
44
  - test/test_template.rb
45
+ - test/util_test.rb
44
46
  - test/vote_smart_api_test.rb
45
47
  has_rdoc: true
46
48
  homepage: http://github.com/pitosalas/govsdk