campaign_cash 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -13,15 +13,19 @@ Simple ruby wrapper for The New York Times Campaign Finance API[http://developer
13
13
 
14
14
  == USAGE:
15
15
 
16
+ require 'rubygems'
17
+ require 'campaign_cash'
18
+
16
19
  You'll want to set your API key as an environment variable in order to run the tests. Otherwise, you'll need to set it like so:
17
20
 
18
21
  Base.api_key = YOUR_API_KEY
19
22
 
20
23
  Currently there are methods to support retrieving candidates and committees, and every method requires at least one parameter,
21
- the election cycle. Some examples:
24
+ the election cycle. Some examples (provided you've done include CampaignCash):
22
25
 
23
- > Candidate.find_by_fecid(2010,"H4NY07011") # find Gary Ackerman's details for the 2010 cycle
24
- > Committee.search(2010, "Growth") # find all committees in the 2010 cycle with the word 'Growth' in the name.
26
+ Candidate.find_by_fecid(2010,"H4NY07011") # find Gary Ackerman's details for the 2010 cycle
27
+ Committee.search(2010, "Growth") # find all committees in the 2010 cycle with the word 'Growth' in the name.
28
+ Candidate.search(2010, "Udall") # find all candidates in the 2010 cycle with the name 'Udall'
25
29
 
26
30
  Check out the tests for further examples.
27
31
 
@@ -13,6 +13,7 @@ Gem::Specification.new do |s|
13
13
 
14
14
  s.required_rubygems_version = ">= 1.3.6"
15
15
  s.rubyforge_project = "campaign_cash"
16
+ s.add_runtime_dependency "json"
16
17
 
17
18
  s.add_development_dependency "bundler", ">= 1.0.0"
18
19
 
@@ -1,6 +1,7 @@
1
1
  require 'rubygems'
2
2
  require 'open-uri'
3
3
  require 'json'
4
+ require 'date'
4
5
 
5
6
  module CampaignCash
6
7
  class Base
@@ -37,6 +38,14 @@ module CampaignCash
37
38
  def api_key
38
39
  @@api_key
39
40
  end
41
+
42
+ def date_parser(date)
43
+ date ? Date.parse(date) : nil
44
+ end
45
+
46
+ def cycle_from_date(date=Date.today)
47
+ date.year.even? ? date.year : date.year+1
48
+ end
40
49
 
41
50
  ##
42
51
  # Builds a request URI to call the API server
@@ -0,0 +1,38 @@
1
+ module CampaignCash
2
+ class Filing < Base
3
+
4
+ attr_reader :committee_name, :date_coverage_from, :amended_uri, :fec_uri, :date_coverage_to, :committee, :report_title, :amended
5
+
6
+ def initialize(params={})
7
+ params.each_pair do |k,v|
8
+ instance_variable_set("@#{k}", v)
9
+ end
10
+ end
11
+
12
+ def self.create_from_api(params={})
13
+ self.new :committee_name => params['committee_name'],
14
+ :date_coverage_from => date_parser(params['date_coverage_from']),
15
+ :date_coverage_to => date_parser(params['date_coverage_to']),
16
+ :committee => params['committee'],
17
+ :report_title => params['report_title'],
18
+ :fec_uri => params['fec_uri'],
19
+ :amended => params['amended'],
20
+ :amended_uri => params['amended_uri']
21
+ end
22
+
23
+ def self.today
24
+ cycle = cycle_from_date
25
+ reply = Base.invoke("#{cycle}/filings", {})
26
+ results = reply['results']
27
+ @filings = results.map{|c| Filing.create_from_api(c)}
28
+ end
29
+
30
+ def self.ymd(year, month, day)
31
+ cycle = cycle_from_date(Date.parse("#{month}/#{day}/#{year}"))
32
+ reply = Base.invoke("#{cycle}/filings/#{year}/#{month}/#{day}", {})
33
+ results = reply['results']
34
+ @filings = results.map{|c| Filing.create_from_api(c)}
35
+ end
36
+
37
+ end
38
+ end
@@ -1,3 +1,3 @@
1
1
  module CampaignCash
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
data/lib/campaign_cash.rb CHANGED
@@ -1,11 +1,8 @@
1
1
  $:.unshift(File.dirname(__FILE__)) unless
2
2
  $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
3
 
4
- module CampaignCash
5
-
6
- end
7
-
8
- require "#{File.dirname(__FILE__)}/campaign_cash/base"
9
- require "#{File.dirname(__FILE__)}/campaign_cash/version"
10
- require "#{File.dirname(__FILE__)}/campaign_cash/candidate"
11
- require "#{File.dirname(__FILE__)}/campaign_cash/committee"
4
+ require "campaign_cash/base"
5
+ require "campaign_cash/candidate"
6
+ require "campaign_cash/committee"
7
+ require "campaign_cash/filing"
8
+ require "campaign_cash/version"
@@ -0,0 +1,37 @@
1
+ require File.dirname(__FILE__) + '/../test_helper.rb'
2
+
3
+ class TestCampaignCash::TestFiling < Test::Unit::TestCase
4
+ include CampaignCash
5
+
6
+ context "a day" do
7
+ setup do
8
+ month, day, year = "11", "27", "2010"
9
+ reply = Base.invoke("2010/filings/#{year}/#{month}/#{day}", {})
10
+ @results = reply['results']
11
+ @filings = @results.map{|c| Filing.create_from_api(c)}
12
+ end
13
+
14
+ should "return a list of objects of the Filing type" do
15
+ assert_kind_of(Filing, @filings.first)
16
+ end
17
+
18
+ %w(committee_name fec_uri committee amended).each do |attr|
19
+ should "assign the value of the @#{attr} attribute from the '#{attr}' key in the hash" do
20
+ assert_equal(@results.first[attr], @filings.first.send(attr))
21
+ end
22
+ end
23
+ end
24
+
25
+ context "today's filings" do
26
+ setup do
27
+ reply = Base.invoke('2010/filings', {})
28
+ @results = reply['results']
29
+ @filings = @results.map{|c| Filing.create_from_api(c)}
30
+ end
31
+
32
+ should "return a list of objects of the Filing type" do
33
+ assert_kind_of(Filing, @filings.first)
34
+ end
35
+ end
36
+
37
+ end
data/test/test_helper.rb CHANGED
@@ -8,6 +8,7 @@ require File.dirname(__FILE__) + '/../lib/campaign_cash'
8
8
  require File.dirname(__FILE__) + '/../lib/campaign_cash/base'
9
9
  require File.dirname(__FILE__) + '/../lib/campaign_cash/candidate'
10
10
  require File.dirname(__FILE__) + '/../lib/campaign_cash/committee'
11
+ require File.dirname(__FILE__) + '/../lib/campaign_cash/filing'
11
12
 
12
13
  # set your NYT Campaign Finance API key as an environment variable to run the tests
13
14
  API_KEY = ENV['NYT_CAMPFIN_API_KEY']
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: campaign_cash
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 23
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 3
10
- version: 0.0.3
9
+ - 4
10
+ version: 0.0.4
11
11
  platform: ruby
12
12
  authors:
13
13
  - Derek Willis
@@ -19,9 +19,23 @@ date: 2010-12-03 00:00:00 -05:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
22
- name: bundler
22
+ name: json
23
23
  prerelease: false
24
24
  requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: bundler
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
25
39
  none: false
26
40
  requirements:
27
41
  - - ">="
@@ -33,7 +47,7 @@ dependencies:
33
47
  - 0
34
48
  version: 1.0.0
35
49
  type: :development
36
- version_requirements: *id001
50
+ version_requirements: *id002
37
51
  description: A thin client for The New York Times Campaign Finance API
38
52
  email:
39
53
  - dwillis@gmail.com
@@ -53,9 +67,11 @@ files:
53
67
  - lib/campaign_cash/base.rb
54
68
  - lib/campaign_cash/candidate.rb
55
69
  - lib/campaign_cash/committee.rb
70
+ - lib/campaign_cash/filing.rb
56
71
  - lib/campaign_cash/version.rb
57
72
  - test/campaign_cash/test_candidate.rb
58
73
  - test/campaign_cash/test_committee.rb
74
+ - test/campaign_cash/test_filing.rb
59
75
  - test/test_helper.rb
60
76
  has_rdoc: true
61
77
  homepage: http://rubygems.org/gems/campaign_cash