bcardarella-deepthroat 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 Brian Cardarella
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,7 @@
1
+ deepthroat==========
2
+ Description goes here.
3
+
4
+ COPYRIGHT
5
+ =========
6
+
7
+ Copyright (c) 2008 Brian Cardarella. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,51 @@
1
+ require 'rake'
2
+
3
+ begin
4
+ require 'jeweler'
5
+ Jeweler::Tasks.new do |s|
6
+ s.name = "deepthroat"
7
+ s.summary = %Q{TODO}
8
+ s.email = "bcardarella@gmail.com"
9
+ s.homepage = "http://github.com/bcardarella/deepthroat"
10
+ s.description = "TODO"
11
+ s.authors = ["Brian Cardarella"]
12
+ end
13
+ rescue LoadError
14
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
15
+ end
16
+
17
+ require 'rake/rdoctask'
18
+ Rake::RDocTask.new do |rdoc|
19
+ rdoc.rdoc_dir = 'rdoc'
20
+ rdoc.title = 'deepthroat'
21
+ rdoc.options << '--line-numbers' << '--inline-source'
22
+ rdoc.rdoc_files.include('README*')
23
+ rdoc.rdoc_files.include('lib/**/*.rb')
24
+ end
25
+
26
+ require 'rake/testtask'
27
+ Rake::TestTask.new(:test) do |t|
28
+ t.libs << 'lib' << 'test'
29
+ t.pattern = 'test/**/test_*.rb'
30
+ t.verbose = false
31
+ end
32
+
33
+ begin
34
+ require 'rcov/rcovtask'
35
+ Rcov::RcovTask.new do |t|
36
+ t.libs << 'test'
37
+ t.test_files = FileList['test/**/*_test.rb']
38
+ t.verbose = true
39
+ end
40
+ rescue LoadError
41
+ puts "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
42
+ end
43
+
44
+ begin
45
+ require 'cucumber/rake/task'
46
+ Cucumber::Rake::Task.new(:features)
47
+ rescue LoadError
48
+ puts "Cucumber is not available. In order to run features, you must: sudo gem install cucumber"
49
+ end
50
+
51
+ task :default => :test
data/VERSION.yml ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ :major: 0
3
+ :minor: 3
4
+ :patch: 0
@@ -0,0 +1,41 @@
1
+ module DeepThroat
2
+ class Base
3
+ include HTTParty
4
+ include HappyMapper
5
+
6
+ CYCLES = [2006, 2008]
7
+
8
+ base_uri "http://www.opensecrets.org/api"
9
+
10
+ def self.find(attributes = {})
11
+ raise DeepThroat::NoCIDError unless attributes[:cid]
12
+ parse(get_xml(attributes))[0]
13
+ end
14
+
15
+ def self.get_xml(attributes = {})
16
+ attributes.merge!({ :apikey => DeepThroat.api_key })
17
+
18
+ attributes.delete_if { |key, value| value.nil? }
19
+
20
+ if attributes[:cycle].to_i != 0 and not CYCLES.include?(attributes[:cycle].to_i)
21
+ raise DeepThroat::InvalidOrMissing::CycleError, "Valid Cycles are: #{ CYCLES.join(", ") }"
22
+ end
23
+
24
+ handle_response(get("/", :query => attributes), attributes)
25
+ end
26
+
27
+ def self.handle_response(response, attributes = {})
28
+ case response.code.to_i
29
+ when 200 then response.body
30
+ when 400..499
31
+ case response.body
32
+ when /key does not have access to this method/ then raise DeepThroat::InvalidOrMissing::ApiKeyError, "#{attributes[:apikey]} is not valid API Key"
33
+ when /Invalid CID/ then raise DeepThroat::InvalidOrMissing::CIDError, "#{attributes[:cid]} is not valid CID"
34
+ when /Invalid Year|Cycle/ then raise DeepThroat::InvalidOrMissing::CycleError, "Valid Cycles are: #{ CYCLES.join(", ") }"
35
+ else ; raise "Something unexpected happened: #{response.body}"
36
+ end
37
+ else ; raise "Something unexpected happened: #{response.body}"
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,29 @@
1
+ module DeepThroat
2
+
3
+ class Contributions < Base
4
+ include HappyMapper
5
+
6
+ class Contributor
7
+ include HappyMapper
8
+
9
+ tag "contributor"
10
+ attribute :organization, String, :tag => "org_name"
11
+ attribute :total, Integer
12
+ end
13
+
14
+ def self.find(attributes = {})
15
+ super(attributes.merge({:method => "candContrib"}))
16
+ end
17
+
18
+ attr_accessor :cid, :cycle, :xml, :details
19
+
20
+ tag "contributors"
21
+ attribute :cid, String
22
+ attribute :cycle, Integer
23
+ attribute :name, String, :tag => "cand_name"
24
+ attribute :origin, String
25
+ attribute :source, String
26
+ has_many :contributors, Contributor
27
+
28
+ end
29
+ end
@@ -0,0 +1,31 @@
1
+ module DeepThroat
2
+
3
+ class Industries < Base
4
+ include HappyMapper
5
+
6
+ class Industry
7
+ include HappyMapper
8
+
9
+ tag "industry"
10
+ attribute :industry, String, :tag => "industry_name"
11
+ attribute :indivs, Integer
12
+ attribute :pacs, Integer
13
+ attribute :total, Integer
14
+ end
15
+
16
+ def self.find(attributes = {})
17
+ super(attributes.merge({:method => "candIndustry"}))
18
+ end
19
+
20
+ attr_accessor :cid, :cycle, :xml, :details
21
+
22
+ tag "industries"
23
+ attribute :cid, String
24
+ attribute :cycle, Integer
25
+ attribute :name, String, :tag => "cand_name"
26
+ attribute :origin, String
27
+ attribute :source, String
28
+ has_many :industries, Industry
29
+
30
+ end
31
+ end
@@ -0,0 +1,35 @@
1
+ module DeepThroat
2
+
3
+ module InvalidOrMissing
4
+ class IndustryCodeError < StandardError; end
5
+ end
6
+
7
+ class Industry < Base
8
+ include HappyMapper
9
+
10
+ def self.find(attributes = {})
11
+ unless attributes.include?(:ind)
12
+ raise DeepThroat::InvalidOrMissing::IndustryCodeError, "Please pass a valid industry code - :ind => 'code'"
13
+ end
14
+
15
+ super(attributes.merge({:method => "CandIndByInd"}))
16
+ end
17
+
18
+ attr_accessor :cid, :cycle, :xml, :details
19
+
20
+ tag "candIndus"
21
+ attribute :cid, String
22
+ attribute :cycle, Integer
23
+ attribute :name, String, :tag => "cand_name"
24
+ attribute :origin, String
25
+ attribute :source, String
26
+ attribute :industry, String
27
+ attribute :chamber, String
28
+ attribute :party, String
29
+ attribute :state, String
30
+ attribute :total, Integer
31
+ attribute :indivs, Integer
32
+ attribute :pacs, Integer
33
+ attribute :rank, Integer
34
+ end
35
+ end
@@ -0,0 +1,28 @@
1
+ module DeepThroat
2
+
3
+ class Summaries < Base
4
+ include HappyMapper
5
+
6
+ def self.find(attributes = {})
7
+ super(attributes.merge({:method => "candSummary"}))
8
+ end
9
+
10
+ attr_accessor :cid, :cycle, :xml, :details
11
+
12
+ tag "summary"
13
+ attribute :cid, String
14
+ attribute :cycle, Integer
15
+ attribute :name, String, :tag => "cand_name"
16
+ attribute :origin, String
17
+ attribute :source, String
18
+ attribute :state, String
19
+ attribute :party, String
20
+ attribute :chamber, String
21
+ attribute :first_elected, String
22
+ attribute :next_election, String
23
+ attribute :total, Integer
24
+ attribute :spent, Integer
25
+ attribute :cash_on_hand, Integer
26
+ attribute :debt, Integer
27
+ end
28
+ end
@@ -0,0 +1,34 @@
1
+ module DeepThroat
2
+
3
+ class TopSectors < Base
4
+ include HappyMapper
5
+
6
+ class Sector
7
+ include HappyMapper
8
+
9
+ tag "sector"
10
+ attribute :sector, String, :tag => "sector_name"
11
+ attribute :sectorid, String
12
+ attribute :indivs, Integer
13
+ attribute :pacs, Integer
14
+ attribute :total, Integer
15
+
16
+
17
+ end
18
+
19
+ def self.find(attributes = {})
20
+ super(attributes.merge({:method => "candSector"}))
21
+ end
22
+
23
+ attr_accessor :cid, :cycle, :xml, :details
24
+
25
+ tag "sectors"
26
+ attribute :cid, String
27
+ attribute :cycle, Integer
28
+ attribute :name, String, :tag => "cand_name"
29
+ attribute :origin, String
30
+ attribute :source, String
31
+ has_many :sectors, Sector
32
+
33
+ end
34
+ end
data/lib/deepthroat.rb ADDED
@@ -0,0 +1,28 @@
1
+ require 'happymapper'
2
+ require 'httparty'
3
+ require 'deepthroat/base'
4
+ require 'deepthroat/contributions'
5
+ require 'deepthroat/industries'
6
+ require 'deepthroat/industry'
7
+ require 'deepthroat/top_sectors'
8
+ require 'deepthroat/summaries'
9
+
10
+
11
+ module DeepThroat
12
+ def self.api_key
13
+ @api_key
14
+ end
15
+
16
+ def self.api_key=(api_key)
17
+ @api_key = api_key
18
+ end
19
+
20
+ class NoCIDError < StandardError; end
21
+ class NoCycleError < StandardError; end
22
+
23
+ module InvalidOrMissing
24
+ class ApiKeyError < StandardError; end
25
+ class CIDError < StandardError; end
26
+ class CycleError < StandardError; end
27
+ end
28
+ end
@@ -0,0 +1,26 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class DeepThroatTest < Test::Unit::TestCase
4
+ context "Class methods" do
5
+ context ".api_key=" do
6
+ setup { DeepThroat.api_key = "test" }
7
+ should "set the api_key" do
8
+ assert_equal "test", DeepThroat.api_key
9
+ end
10
+ end
11
+ end
12
+
13
+ context "DeepThroat Errors" do
14
+ should "include NoCIDError" do
15
+ assert_nothing_raised do
16
+ DeepThroat::NoCIDError
17
+ end
18
+ end
19
+
20
+ should "include NoCycleError" do
21
+ assert_nothing_raised do
22
+ DeepThroat::NoCycleError
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,52 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'rr'
4
+ require 'shoulda'
5
+ require 'fake_web'
6
+ require 'ruby-debug'
7
+
8
+ FakeWeb.allow_net_connect = false
9
+
10
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
11
+
12
+ require 'deepthroat'
13
+
14
+ class Test::Unit::TestCase
15
+ include RR::Adapters::TestUnit unless included_modules.include?(RR::Adapters::TestUnit)
16
+
17
+ def self.define_xml_fixture_methods
18
+ Dir.new("#{File.dirname(__FILE__)}/xml_samples").map { |file| file.gsub(/.xml/,"") if file =~ /.xml/ }.compact.each do |file|
19
+ define_method("#{file}_response") do
20
+ instance_eval("@#{file}_xml ||= File.read(\"#{File.dirname(__FILE__)}/xml_samples/#{file}.xml\")")
21
+ end
22
+
23
+ define_method("#{file}_xml") do
24
+ instance_eval("#{file}_response").match(/\<response\>.*$/)[0]
25
+ end
26
+ end
27
+ end
28
+
29
+ FakeWeb.allow_net_connect = false
30
+ define_xml_fixture_methods
31
+
32
+ def web_faker(method, *params)
33
+ method = method.to_s
34
+ options = params[0]
35
+
36
+ open_secrets_method = case method.split("_bad")[0]
37
+ when "contributions" then "candContrib"
38
+ when "industries" then "candIndustry"
39
+ when "industry" then "CandIndByInd"
40
+ when "top_sectors" then "candSector"
41
+ when "summaries" then "candSummary"
42
+ end
43
+
44
+ if method =~ /bad/
45
+ method.sub!(/(\w+)_bad/,"").sub!(/^/,"bad")
46
+ end
47
+
48
+ options.merge!({ :method => open_secrets_method })
49
+
50
+ FakeWeb.register_uri("http://www.opensecrets.org/api/?#{options.map {|key, value| "#{key}=#{value}"}.join("&")}", :response => send("#{method}_response"))
51
+ end
52
+ end
@@ -0,0 +1,156 @@
1
+ require File.dirname(__FILE__) + '/../test_helper'
2
+
3
+ class TestBase < Test::Unit::TestCase
4
+ context "Included Modules" do
5
+ should "have HTTParty" do
6
+ assert DeepThroat::Base.included_modules.include?(HTTParty)
7
+ end
8
+
9
+ should "have HappyMapper" do
10
+ assert DeepThroat::Base.included_modules.include?(HappyMapper)
11
+ end
12
+ end
13
+
14
+ context "Class Methods" do
15
+ context ".find" do
16
+ setup do
17
+ @xml_response = "<response></response>"
18
+ @attributes = {:cid => "TestCID"}
19
+ end
20
+
21
+ should "respond to :find" do
22
+ assert DeepThroat::Base.respond_to? :find
23
+ end
24
+
25
+ should "return an instance method" do
26
+ mock(DeepThroat::Base).parse(@xml_response) { [DeepThroat::Base.new] }
27
+ mock(DeepThroat::Base).get_xml(@attributes) { @xml_response }
28
+ assert DeepThroat::Base.find(@attributes).instance_of?(DeepThroat::Base)
29
+ end
30
+
31
+ should "raise DeepThroat::NoCIDError if :cid is not passed" do
32
+ assert_raise DeepThroat::NoCIDError do
33
+ DeepThroat::Base.find
34
+ end
35
+ end
36
+ end
37
+
38
+ context ".get_xml" do
39
+ setup do
40
+ @attributes = {:cid=>"TestCID"}
41
+ end
42
+
43
+ should "return a String" do
44
+ stub(DeepThroat::Base).get("/", anything) { Object.new }
45
+ stub(DeepThroat::Base).handle_response(anything, anything) { String.new }
46
+ assert_kind_of String, DeepThroat::Base.get_xml(@attributes)
47
+ end
48
+
49
+ should "call HTTParty's .get" do
50
+ mock(DeepThroat::Base).get("/", anything)
51
+ stub(DeepThroat::Base).handle_response(anything, anything)
52
+ DeepThroat::Base.get_xml(@attributes)
53
+ end
54
+
55
+ should "handle the response" do
56
+ stub(DeepThroat::Base).get("/", anything) { Object.new }
57
+ mock(DeepThroat::Base).handle_response(anything, anything)
58
+ DeepThroat::Base.get_xml(@attributes)
59
+ end
60
+
61
+ should "raise 'DeepThroat::InvalidOrMissing::CycleError' when asking for invalid CYCLE" do
62
+ stub(DeepThroat::Base).get("/", anything) { Object.new }
63
+ stub(DeepThroat::Base).handle_response(anything, anything)
64
+
65
+ assert_raise DeepThroat::InvalidOrMissing::CycleError do
66
+ DeepThroat::Base.get_xml(@attributes.merge({:cycle => 2009}))
67
+ end
68
+ end
69
+ end
70
+
71
+ context ".handle_response(response)" do
72
+ context "200" do
73
+ should "return an XML string" do
74
+ mock_response = Object.new
75
+ mock(mock_response) do
76
+ code { "200" }
77
+ body { "<response></response>" }
78
+ end
79
+
80
+ assert_kind_of String, DeepThroat::Base.handle_response(mock_response)
81
+ end
82
+ end
83
+
84
+ context "400" do
85
+ should "raise 'DeepThroat::InvalidOrMissing::ApiKeyError'" do
86
+ mock_response = Object.new
87
+ mock(mock_response) do
88
+ code { "400" }
89
+ body { "key does not have access to this method" }
90
+ end
91
+
92
+ assert_raise DeepThroat::InvalidOrMissing::ApiKeyError do
93
+ DeepThroat::Base.handle_response(mock_response)
94
+ end
95
+ end
96
+
97
+ should "raise 'DeepThroat::InvalidOrMissing::CIDError'" do
98
+ mock_response = Object.new
99
+ mock(mock_response) do
100
+ code { "400" }
101
+ body { "Invalid CID" }
102
+ end
103
+
104
+ assert_raise DeepThroat::InvalidOrMissing::CIDError do
105
+ DeepThroat::Base.handle_response(mock_response)
106
+ end
107
+ end
108
+
109
+ should "raise 'DeepThroat::InvalidOrMissing::CycleError'" do
110
+ mock_response = Object.new
111
+ mock(mock_response) do
112
+ code { "400" }
113
+ body { "Invalid Year|Cycle" }
114
+ end
115
+
116
+ assert_raise DeepThroat::InvalidOrMissing::CycleError do
117
+ DeepThroat::Base.handle_response(mock_response)
118
+ end
119
+ end
120
+
121
+ should "raise 'Something unexpected happened'" do
122
+ mock_response = Object.new
123
+ mock(mock_response) do
124
+ code { "400" }
125
+ body.twice { "" }
126
+ end
127
+
128
+ assert_raise RuntimeError, "Something unexpected happened" do
129
+ DeepThroat::Base.handle_response(mock_response)
130
+ end
131
+ end
132
+ end
133
+
134
+ context "unknown code" do
135
+ should "raise 'Something unexpected happened'" do
136
+ mock_response = Object.new
137
+ mock(mock_response) do
138
+ code { "700" }
139
+ body { "" }
140
+ end
141
+
142
+ assert_raise RuntimeError, "Something unexpected happened" do
143
+ DeepThroat::Base.handle_response(mock_response)
144
+ end
145
+ end
146
+ end
147
+ end
148
+
149
+ context "HTTParty's .base_uri" do
150
+ should "equal 'http://www.opensecrets.org/api'" do
151
+ assert_equal "http://www.opensecrets.org/api", DeepThroat::Base.default_options[:base_uri]
152
+ end
153
+ end
154
+
155
+ end
156
+ end