pitosalas-govsdk 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION.yml ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ :patch: 1
3
+ :major: 0
4
+ :minor: 0
@@ -0,0 +1,124 @@
1
+ =begin
2
+ * Name: GovSDK
3
+ * Description:
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
+ =end
24
+
25
+ require 'rubygems'
26
+ require 'httparty'
27
+ require 'cgi'
28
+ require 'net/http'
29
+
30
+ #require "ruby-debug"
31
+ #Debugger.settings[:autolist] = 1 # list nearby lines on stop
32
+ #Debugger.settings[:autoeval] = 1
33
+ #Debugger.start
34
+
35
+ class GenericAPI
36
+
37
+ def initialized?
38
+ !@api_key.nil? || @api_key.kind_of?(String)
39
+ end
40
+
41
+ end
42
+
43
+ # Control Access to Sunlight API.
44
+
45
+ class Sunlight < GenericAPI
46
+ include HTTParty
47
+ Sunlight.base_uri "services.sunlightlabs.com"
48
+
49
+ def legislators_search(params)
50
+ result = Sunlight.get("/api/legislators.search", :query => {:name => params})
51
+ result["response"]["results"]
52
+ end
53
+
54
+ # Simply declare and remember the API Key
55
+ def key=(key)
56
+ @api_key = key
57
+ Sunlight.default_params :apikey => key
58
+ end
59
+
60
+ # Call any of the various legal Sunlight queries
61
+ def legislators_get(params)
62
+ begin
63
+ result = Sunlight.get("/api/legislators.get", :query => params)
64
+ result = result["response"]["legislator"]
65
+ rescue Net::HTTPServerException => exception
66
+ puts "EXCEPTION: from Sunlight - legislators.get: #{exception.response.body}"
67
+ return nil
68
+ end
69
+ end
70
+
71
+ end
72
+
73
+ # Control Access to OpenSecrets API.
74
+ class OpenSecrets < GenericAPI
75
+ include HTTParty
76
+ base_uri "www.opensecrets.org/api/"
77
+ format :xml
78
+
79
+ def key=(key)
80
+ @api_key = key
81
+ OpenSecrets.default_params :apikey => key
82
+ end
83
+
84
+ def get_cand_info(method_name, crpID, cycle)
85
+ query_hash = {:method => method_name}
86
+ begin
87
+ result = OpenSecrets.get("", :query => query_hash.merge({:cid => crpID, :cycle => cycle}))
88
+ rescue Net::HTTPServerException => exception
89
+ puts "EXCEPTION: from Opensecrets! #{method_name}: #{exception.response.body}"
90
+ return nil
91
+ end
92
+ end
93
+
94
+ def get_cand_summary_for_crpID(crpID, cycle = "")
95
+ result = get_cand_info("candSummary", crpID, cycle)
96
+ result["response"]["summary"]
97
+ end
98
+
99
+ def get_cand_pfd_assets(crpID, cycle = "")
100
+ result = get_cand_info("memPFDprofile", crpID, cycle)
101
+ result["response"]["member_profile"]["assets"]["asset"]
102
+ end
103
+
104
+ def get_cand_pfd_transactions(crpID, cycle = "")
105
+ result = get_cand_info("memPFDprofile", crpID, cycle)
106
+ result["response"]["member_profile"]["transactions"]["transaction"]
107
+ end
108
+
109
+ def get_cand_pfd_positions_held(crpID, cycle = "")
110
+ result = get_cand_info("memPFDprofile", crpID, cycle)
111
+ result = result["response"]["member_profile"]
112
+ if result.has_key?("positions")
113
+ result["positions"]["position"]
114
+ else
115
+ nil
116
+ end
117
+ end
118
+ end
119
+
120
+
121
+
122
+
123
+
124
+
data/lib/govsdk.rb ADDED
@@ -0,0 +1,59 @@
1
+ =begin
2
+ * Name: GovSDK
3
+ * Description:
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
+ =end
24
+
25
+ #require "ruby-debug"
26
+ #Debugger.settings[:autolist] = 1 # list nearby lines on stop
27
+ #Debugger.settings[:autoeval] = 1
28
+ #Debugger.start
29
+
30
+ class GovSdk
31
+
32
+ def self.load_apis
33
+ @sunl_api = Sunlight.new
34
+ @opens_api = OpenSecrets.new
35
+ end
36
+
37
+ def self.sunlight_api
38
+ @sunl_api
39
+ end
40
+
41
+ def self.opensecrets_api
42
+ @opens_api
43
+ end
44
+
45
+ def self.followmoney_api
46
+ @followm_api
47
+ end
48
+
49
+ def self.init(apikeys = {})
50
+ apikeys.keys.each { |x| raise ArgumentError unless [:followmoney, :sunlight, :opensecrets].include?(x) }
51
+ GovSdk.load_apis
52
+ apikeys.each do |key, val|
53
+ @sunl_api.key = val if key == :sunlight
54
+ @opens_api.key = val if key == :opensecrets
55
+ @followm_api.key = val if key == :followmoney
56
+ end
57
+ end
58
+
59
+ end
data/lib/govsdkgem.rb ADDED
@@ -0,0 +1,27 @@
1
+ =begin
2
+ * Name: GovSDK
3
+ * Description:
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
+ =end
24
+
25
+ require 'govsdk'
26
+ require 'apimanagers'
27
+ require 'congressperson'
data/lib/template.rb ADDED
@@ -0,0 +1,23 @@
1
+ =begin
2
+ * Name: GovSDK
3
+ * Description:
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
+ =end
@@ -0,0 +1,109 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+ =begin
3
+ * Name: GovSDK
4
+ * Description:
5
+ * Author: Pito Salas
6
+ * Copyright: (c) R. Pito Salas and Associates, Inc.
7
+ * Date: January 2009
8
+ * License: GPL
9
+
10
+ This file is part of GovSDK.
11
+
12
+ GovSDK is free software: you can redistribute it and/or modify
13
+ it under the terms of the GNU General Public License as published by
14
+ the Free Software Foundation, either version 3 of the License, or
15
+ (at your option) any later version.
16
+
17
+ GovSDK is distributed in the hope that it will be useful,
18
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
19
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20
+ GNU General Public License for more details.
21
+
22
+ You should have received a copy of the GNU General Public License
23
+ along with GovSDK. If not, see <http://www.gnu.org/licenses/>.
24
+ =end
25
+
26
+ class TestAPIs < Test::Unit::TestCase
27
+ context "brand new API manager" do
28
+ setup do
29
+ @apim = GenericAPI.new
30
+ end
31
+
32
+ should "not be initialized" do
33
+ assert_equal false, @apim.initialized?
34
+ end
35
+ end
36
+
37
+ context "Sunlight API" do
38
+ setup do
39
+ GovSdk.load_apis
40
+ GovSdk.sunlight_api.key = "4ffa22917ab1ed010a8e681c550c9593"
41
+ end
42
+
43
+ should "be initialized once APIkey is set" do
44
+ assert_equal true, GovSdk.sunlight_api.initialized?
45
+ end
46
+
47
+ should "return no legislator called Pito Salas" do
48
+ result = GovSdk.sunlight_api.legislators_search("Pito Salas")
49
+ assert_equal 0, result.length
50
+ end
51
+
52
+ should "return a single legislator called Ted Kennedy" do
53
+ result = GovSdk.sunlight_api.legislators_search("Ted Kennedy")
54
+ assert 1, result.length
55
+ end
56
+
57
+ should "return a single legislator with the nickname ted" do
58
+ result = GovSdk.sunlight_api.legislators_get(:nickname => "Ted", :lastname => "Kennedy")
59
+ assert_equal "Ted", result["nickname"]
60
+ end
61
+
62
+ should "be able to locate crp_id N00000308" do
63
+ result = GovSdk.sunlight_api.legislators_get(:crp_id => "N00000308")
64
+ end
65
+
66
+ should "be able to go from name to crp_id and back" do
67
+ result = GovSdk.sunlight_api.legislators_get(:nickname => "ted", :lastname => "Kennedy")
68
+ crp_id = result["crp_id"]
69
+ result = GovSdk.sunlight_api.legislators_get(:crp_id => crp_id)
70
+ assert_equal "Ted", result["nickname"]
71
+ end
72
+
73
+ end
74
+
75
+ context "OpenSecrets API manager" do
76
+ setup do
77
+ GovSdk.load_apis
78
+ GovSdk.opensecrets_api.key = "09c975b6d3f19eb865805b2244311065"
79
+ @os_api = GovSdk.opensecrets_api
80
+ end
81
+
82
+ should "be initialized once APIkey is set" do
83
+ assert_equal true, @os_api.initialized?
84
+ end
85
+
86
+ should "locate candidate summary for N00000308 and he's in MA" do
87
+ assert_equal "MA", @os_api.get_cand_summary_for_crpID("N00000308", 2006)["state"]
88
+ end
89
+
90
+ should "locate asset list for N00000360 and check that he has 3" do
91
+ assert_equal 3, @os_api.get_cand_pfd_assets("N00000360", cycle = "").length
92
+ end
93
+
94
+ should "locate transaction list for N00000019 and check that he has 3" do
95
+ assert_equal 20, @os_api.get_cand_pfd_transactions("N00000019", cycle = "").length
96
+ end
97
+
98
+ should "locate list of positions held for N00000360 and check the count" do
99
+ assert_equal 16, @os_api.get_cand_pfd_positions_held("N00000360", cycle = "").length
100
+ end
101
+
102
+ should "locate list of positions held for N00000308 and check the count" do
103
+ assert_equal 4, @os_api.get_cand_pfd_positions_held("N00000308", cycle = "").length
104
+ assert_equal 2, @os_api.get_cand_pfd_positions_held("N00000019", cycle = "").length
105
+ end
106
+
107
+ end
108
+
109
+ end
@@ -0,0 +1,109 @@
1
+ =begin
2
+ * Name: GovSDK
3
+ * Description:
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
+ =end
24
+
25
+ require File.dirname(__FILE__) + '/test_helper'
26
+
27
+ class CongressPersonTest < Test::Unit::TestCase
28
+ context "In basic CongressPerson cases" do
29
+ setup do
30
+ GovSdk.load_apis
31
+ GovSdk.sunlight_api.key = "4ffa22917ab1ed010a8e681c550c9593"
32
+ @cpnone = CongressPerson.find_by_name("Pito Salas")
33
+ @cpkennedy = CongressPerson.find_by_name("Kennedy")
34
+ @cpkennedy[0].nickname == "Ted" ? @ted_crp_id = @cpkennedy[0].crp_id : @ted_crp_id = @cpkennedy[1].crp_id
35
+ end
36
+
37
+ should "return none" do
38
+ assert_equal 0, @cpnone.length
39
+ end
40
+
41
+ should "return two legislators named Kennedy" do
42
+ assert_equal 2, @cpkennedy.length
43
+ @cpkennedy.each do |a_kennedy|
44
+ assert ["Edward", "Patrick Joseph"].include? a_kennedy.firstname
45
+ end
46
+ end
47
+
48
+ context "trying to use CRP from earlier test" do
49
+ setup do
50
+ @cpteddy = CongressPerson.find_by_crp_id(@ted_crp_id)
51
+ end
52
+
53
+ should "Bad crp should be nil" do
54
+ @cpbad = CongressPerson.find_by_crp_id("1")
55
+ assert_nil @cpbad
56
+ end
57
+
58
+ should "crpid should be for ted kennedy" do
59
+ assert_equal "Ted", @cpteddy.nickname
60
+ end
61
+ end
62
+
63
+ context "Get fundraising summary for N00000308" do
64
+ setup do
65
+ GovSdk.opensecrets_api.key = "09c975b6d3f19eb865805b2244311065"
66
+ @mycandidate = CongressPerson.find_by_crp_id("N00000308")
67
+ end
68
+
69
+ should "pull info out of summary" do
70
+ assert_not_nil @mycandidate.crp_id
71
+ fund_sum = @mycandidate.get_fundraising_summary(2008)
72
+ assert_equal 0, fund_sum.debt
73
+ fund_sum = @mycandidate.get_fundraising_summary(2006)
74
+ assert_equal 0, fund_sum.debt
75
+ end
76
+ end
77
+
78
+ context "Get a look at positions held detail for N00000360 and Clinton" do
79
+ setup do
80
+ GovSdk.opensecrets_api.key = "09c975b6d3f19eb865805b2244311065"
81
+ @mycandidate = CongressPerson.find_by_crp_id("N00000360")
82
+ @nopositioncandidate = CongressPerson.find_by_name("Clinton")
83
+ end
84
+
85
+ should "see what positions were held for N00000360" do
86
+ positions_held = @mycandidate.get_positions_held(2008)
87
+ assert_equal 16, positions_held.length
88
+ end
89
+
90
+ should "work if candidate has no positions held information" do
91
+ positions_held = @nopositioncandidate[0].get_positions_held(2008)
92
+ assert_equal 1, positions_held.length
93
+ end
94
+ end
95
+
96
+ context "Try some other odd cases" do
97
+ setup do
98
+ GovSdk.opensecrets_api.key = "09c975b6d3f19eb865805b2244311065"
99
+ @franks = CongressPerson.find_by_name("Frank")[0]
100
+ end
101
+
102
+ should "see that barney frank reported zero positions" do
103
+ assert_equal 0, @franks.get_positions_held(2008).length
104
+ end
105
+ end
106
+
107
+
108
+ end
109
+ end
@@ -0,0 +1,45 @@
1
+ =begin
2
+ * Name: GovSDK
3
+ * Description:
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
+ =end
24
+
25
+ require File.dirname(__FILE__) + '/test_helper'
26
+
27
+ class GovSdkTest < Test::Unit::TestCase
28
+ context "" do
29
+ setup do
30
+
31
+ end
32
+
33
+ should "Check error checking with invalid arg" do
34
+ assert_raise ArgumentError do
35
+ GovSdk.init(:xx => 1)
36
+ end
37
+ end
38
+ should "Check error checking with valid args" do
39
+ assert_nothing_raised ArgumentError do
40
+ GovSdk.init(:sunlight => 1, :opensecrets => 2)
41
+ end
42
+ end
43
+
44
+ end
45
+ end
@@ -0,0 +1,13 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+ require 'mocha'
5
+ require 'pp'
6
+
7
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
8
+ require 'govsdk'
9
+ require 'apimanagers'
10
+ require 'congressperson'
11
+
12
+ class Test::Unit::TestCase
13
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pitosalas-govsdk
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Pito Salas
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-01-30 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: TODO
17
+ email: rps@salas.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - VERSION.yml
26
+ - lib/apimanagers.rb
27
+ - lib/congress_person.rb
28
+ - lib/govsdk.rb
29
+ - lib/govsdkgem.rb
30
+ - lib/template.rb
31
+ - test/apimanagers_test.rb
32
+ - test/congressperson_test.rb
33
+ - test/govsdk_test.rb
34
+ - test/test_helper.rb
35
+ has_rdoc: true
36
+ homepage: http://github.com/pitosalas/govsdk
37
+ post_install_message:
38
+ rdoc_options:
39
+ - --inline-source
40
+ - --charset=UTF-8
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: "0"
48
+ version:
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "0"
54
+ version:
55
+ requirements: []
56
+
57
+ rubyforge_project:
58
+ rubygems_version: 1.2.0
59
+ signing_key:
60
+ specification_version: 2
61
+ summary: TODO
62
+ test_files: []
63
+