Skip to content

Commit

Permalink
updated hash syntax to ruby 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
dwillis committed Mar 3, 2014
1 parent 60d84bc commit bed8ebf
Show file tree
Hide file tree
Showing 11 changed files with 275 additions and 283 deletions.
8 changes: 4 additions & 4 deletions lib/campaign_cash/base.rb
Expand Up @@ -67,9 +67,9 @@ def check_offset(offset)
##
# Builds a request URI to call the API server
def build_request_url(path, params)
URI::HTTP.build :host => API_SERVER,
:path => "#{API_BASE}/#{path}.json",
:query => params.map {|k,v| "#{k}=#{v}"}.join('&')
URI::HTTP.build host: API_SERVER,
path: "#{API_BASE}/#{path}.json",
query: params.map {|k,v| "#{k}=#{v}"}.join('&')
end

def invoke(path, params={})
Expand All @@ -78,7 +78,7 @@ def invoke(path, params={})
raise "You must initialize the API key before you run any API queries"
end

full_params = params.merge 'api-key' => @@api_key
full_params = params.merge({"api-key" => @@api_key})
full_params.delete_if {|k,v| v.nil?}

check_offset(params[:offset]) if params[:offset]
Expand Down
101 changes: 50 additions & 51 deletions lib/campaign_cash/candidate.rb
Expand Up @@ -5,11 +5,11 @@ class Candidate < Base
# A candidate is a person seeking a particular office within a particular two-year election
# cycle. Each candidate is assigned a unique ID within a cycle.
attr_reader :name, :id, :state, :district, :party, :fec_uri, :committee_id,
:mailing_city, :mailing_address, :mailing_state, :mailing_zip,
:total_receipts, :total_contributions, :total_from_individuals,
:total_from_pacs, :candidate_loans, :total_disbursements,
:total_refunds, :debts_owed, :begin_cash, :end_cash, :status,
:date_coverage_to, :date_coverage_from, :relative_uri, :office
:mailing_city, :mailing_address, :mailing_state, :mailing_zip,
:total_receipts, :total_contributions, :total_from_individuals,
:total_from_pacs, :candidate_loans, :total_disbursements,
:total_refunds, :debts_owed, :begin_cash, :end_cash, :status,
:date_coverage_to, :date_coverage_from, :relative_uri, :office

def initialize(params={})
params.each_pair do |k,v|
Expand All @@ -19,42 +19,41 @@ def initialize(params={})

# Creates a new candidate object from a JSON API response.
def self.create(params={})
self.new :name => params['name'],
:id => params['id'],
:state => parse_state(params['state']),
:office => parse_office(params['id']),
:district => parse_district(params['district']),
:party => params['party'],
:fec_uri => params['fec_uri'],
:committee_id => parse_committee(params['committee']),
:mailing_city => params['mailing_city'],
:mailing_address => params['mailing_address'],
:mailing_state => params['mailing_state'],
:mailing_zip => params['mailing_zip'],
:total_receipts => params['total_receipts'].to_f,
:total_contributions => params['total_contributions'].to_f,
:total_from_individuals => params['total_from_individuals'].to_f,
:total_from_pacs => params['total_from_pacs'].to_f,
:candidate_loans => params['candidate_loans'].to_f,
:total_disbursements => params['total_disbursements'].to_f,
:total_refunds => params['total_refunds'].to_f,
:debts_owed => params['debts_owed'].to_f,
:begin_cash => params['begin_cash'].to_f,
:end_cash => params['end_cash'].to_f,
:status => params['status'],
:date_coverage_from => params['date_coverage_from'],
:date_coverage_to => params['date_coverage_to']
self.new name: params['name'],
id: params['id'],
state: parse_state(params['state']),
office: parse_office(params['id']),
district: parse_district(params['district']),
party: params['party'],
fec_uri: params['fec_uri'],
committee_id: parse_committee(params['committee']),
mailing_city: params['mailing_city'],
mailing_address: params['mailing_address'],
mailing_state: params['mailing_state'],
mailing_zip: params['mailing_zip'],
total_receipts: params['total_receipts'].to_f,
total_contributions: params['total_contributions'].to_f,
total_from_individuals: params['total_from_individuals'].to_f,
total_from_pacs: params['total_from_pacs'].to_f,
candidate_loans: params['candidate_loans'].to_f,
total_disbursements: params['total_disbursements'].to_f,
total_refunds: params['total_refunds'].to_f,
debts_owed: params['debts_owed'].to_f,
begin_cash: params['begin_cash'].to_f,
end_cash: params['end_cash'].to_f,
status: params['status'],
date_coverage_from: params['date_coverage_from'],
date_coverage_to: params['date_coverage_to']
end

def self.create_from_search_results(params={})
self.new :name => params['candidate']['name'],
:id => params['candidate']['id'],
:state => params['candidate']['id'][2..3],
:office => parse_office(params['candidate']['id'][0..0]),
:district => parse_district(params['district']),
:party => params['candidate']['party'],
:committee_id => parse_committee(params['committee'])

self.new name: params['candidate']['name'],
id: params['candidate']['id'],
state: params['candidate']['id'][2..3],
office: parse_office(params['candidate']['id'][0..0]),
district: parse_district(params['district']),
party: params['candidate']['party'],
committee_id: parse_committee(params['committee'])
end

def self.parse_state(state)
Expand Down Expand Up @@ -82,15 +81,15 @@ def self.parse_district(uri)

def self.categories
{
"individual_total" => "Contributions from individuals",
"contribution_total" => "Total contributions",
"candidate_loan" => "Loans from candidate",
"receipts_total" => "Total receipts",
"refund_total" => "Total refunds",
"pac_total" => "Contributions from PACs",
"disbursements_total" => "Total disbursements",
"end_cash" => "Cash on hand",
"debts_owed" => "Debts owed by",
individual_total: "Contributions from individuals",
contribution_total: "Total contributions",
candidate_loan: "Loans from candidate",
receipts_total: "Total receipts",
refund_total: "Total refunds",
pac_total: "Contributions from PACs",
disbursements_total: "Total disbursements",
end_cash: "Cash on hand",
debts_owed: "Debts owed by",
}
end

Expand All @@ -114,15 +113,15 @@ def self.leaders(category, cycle=CURRENT_CYCLE)
# Returns an array of candidates matching a search term within a cycle. Defaults to the
# current cycle.
def self.search(name, cycle=CURRENT_CYCLE, offset=nil)
reply = invoke("#{cycle}/candidates/search", {:query => name, :offset => offset})
reply = invoke("#{cycle}/candidates/search", {query: name, offset: offset})
results = reply['results']
results.map{|c| self.create_from_search_results(c)}
end

# Returns an array of newly created FEC candidates within a current cycle. Defaults to the
# current cycle.
def self.new_candidates(cycle=CURRENT_CYCLE, offset=nil)
reply = invoke("#{cycle}/candidates/new",{:offset => offset})
reply = invoke("#{cycle}/candidates/new",{offset: offset})
results = reply['results']
results.map{|c| self.create(c)}
end
Expand All @@ -133,9 +132,9 @@ def self.state(state, chamber=nil, district=nil, cycle=CURRENT_CYCLE, offset=nil
path = "#{cycle}/seats/#{state}"
if chamber
path += "/#{chamber}"
path += "/#{district}" if district
path += "/#{district}" if district
end
reply = invoke(path,{:offset => offset})
reply = invoke(path, {offset: offset})
results = reply['results']
results.map{|c| self.create_from_search_results(c)}
end
Expand Down
166 changes: 83 additions & 83 deletions lib/campaign_cash/committee.rb
Expand Up @@ -2,54 +2,54 @@ module CampaignCash
class Committee < Base

FILING_FREQUENCY = {
'A' => 'Administratively Terminated',
'D' => 'Debt',
'M' => 'Monthly',
'Q' => 'Quarterly',
'T' => 'Terminated',
'W' => 'Waived'
A: 'Administratively Terminated',
D: 'Debt',
M: 'Monthly',
Q: 'Quarterly',
T: 'Terminated',
W: 'Waived'
}

INTEREST_GROUP = {
'C' => 'Corporation',
'L' => 'Labor Union',
'M' => 'Membership',
'T' => 'Trade Association',
'V' => 'Cooperative',
'W' => 'Corporation without Capital Stock'
C: 'Corporation',
L: 'Labor Union',
M: 'Membership',
T: 'Trade Association',
V: 'Cooperative',
W: 'Corporation without Capital Stock'
}

COMMITTEE_TYPE = {
'C' => 'Communication Cost',
'D' => 'Delegate',
'E' => 'Electioneering Communications',
'H' => 'House Candidate',
'I' => 'Independent Expenditure (Person or Group)',
'N' => 'Non-Party, Non-Qualified',
'P' => 'Presidential Candidate',
'Q' => 'Qualified Non-Party',
'S' => 'Senate Candidate',
'X' => 'Non-Qualified Party',
'Y' => 'Qualified Party',
'Z' => 'National Party Non-Federal'
C: 'Communication Cost',
D: 'Delegate',
E: 'Electioneering Communications',
H: 'House Candidate',
I: 'Independent Expenditure (Person or Group)',
N: 'Non-Party, Non-Qualified',
P: 'Presidential Candidate',
Q: 'Qualified Non-Party',
S: 'Senate Candidate',
X: 'Non-Qualified Party',
Y: 'Qualified Party',
Z: 'National Party Non-Federal'
}

COMMITTEE_DESIGNATION = {
'A' => 'Authorized by a Candidate',
'J' => 'Joint Fundraiser',
'P' => 'Principal Campaign Committee',
'U' => 'Unauthorized',
'B' => 'Lobbyist/Registrant PAC',
'D' => 'Leadership PAC'
A: 'Authorized by a Candidate',
J: 'Joint Fundraiser',
P: 'Principal Campaign Committee',
U: 'Unauthorized',
B: 'Lobbyist/Registrant PAC',
D: 'Leadership PAC'
}

attr_reader :name, :id, :state, :district, :party, :fec_uri, :candidate,
:city, :address, :state, :zip, :relative_uri, :sponsor_name,
:total_receipts, :total_contributions, :total_from_individuals,
:total_from_pacs, :candidate_loans, :total_disbursements,
:total_refunds, :debts_owed, :begin_cash, :end_cash,
:date_coverage_to, :date_coverage_from, :other_cycles, :super_pac, :filings,
:total_candidate_contributions, :total_independent_expenditures
:city, :address, :state, :zip, :relative_uri, :sponsor_name,
:total_receipts, :total_contributions, :total_from_individuals,
:total_from_pacs, :candidate_loans, :total_disbursements,
:total_refunds, :debts_owed, :begin_cash, :end_cash,
:date_coverage_to, :date_coverage_from, :other_cycles, :super_pac, :filings,
:total_candidate_contributions, :total_independent_expenditures

def initialize(params={})
params.each_pair do |k,v|
Expand All @@ -58,52 +58,52 @@ def initialize(params={})
end

def self.create(params={})
self.new :name => params['name'],
:id => params['id'],
:state => params['state'],
:party => params['party'],
:fec_uri => params['fec_uri'],
:city => params['city'],
:address => params['address'],
:zip => params['zip'],
:sponsor_name => params['sponsor_name'],
:leadership => params['leadership'],
:super_pac => params['super_pac'],
:total_receipts => params['total_receipts'].to_f,
:total_contributions => params['total_contributions'].to_f,
:total_from_individuals => params['total_from_individuals'].to_f,
:total_from_pacs => params['total_from_pacs'].to_f,
:candidate_loans => params['candidate_loans'].to_f,
:total_disbursements => params['total_disbursements'].to_f,
:total_candidate_contributions => params['total_candidate_contributions'].to_f,
:total_independent_expenditures => params['total_independent_expenditures'].to_f,
:total_refunds => params['total_refunds'].to_f,
:debts_owed => params['debts_owed'].to_f,
:begin_cash => params['begin_cash'].to_f,
:end_cash => params['end_cash'].to_f,
:date_coverage_from => date_parser(params['date_coverage_from']),
:date_coverage_to => date_parser(params['date_coverage_to']),
:candidate_id => parse_candidate(params['candidate']),
:filing_frequency => get_frequency(params['filing_frequency']),
:interest_group => get_interest_group(params['interest_group']),
:committee_type => get_committee_type(params['committee_type']),
:designation => get_designation(params['designation']),
:other_cycles => params['other_cycles'].map{|cycle| cycle['cycle']['cycle']}
self.new name: params['name'],
id: params['id'],
state: params['state'],
party: params['party'],
fec_uri: params['fec_uri'],
city: params['city'],
address: params['address'],
zip: params['zip'],
sponsor_name: params['sponsor_name'],
leadership: params['leadership'],
super_pac: params['super_pac'],
total_receipts: params['total_receipts'].to_f,
total_contributions: params['total_contributions'].to_f,
total_from_individuals: params['total_from_individuals'].to_f,
total_from_pacs: params['total_from_pacs'].to_f,
candidate_loans: params['candidate_loans'].to_f,
total_disbursements: params['total_disbursements'].to_f,
total_candidate_contributions: params['total_candidate_contributions'].to_f,
total_independent_expenditures: params['total_independent_expenditures'].to_f,
total_refunds: params['total_refunds'].to_f,
debts_owed: params['debts_owed'].to_f,
begin_cash: params['begin_cash'].to_f,
end_cash: params['end_cash'].to_f,
date_coverage_from: date_parser(params['date_coverage_from']),
date_coverage_to: date_parser(params['date_coverage_to']),
candidate_id: parse_candidate(params['candidate']),
filing_frequency: get_frequency(params['filing_frequency']),
interest_group: get_interest_group(params['interest_group']),
committee_type: get_committee_type(params['committee_type']),
designation: get_designation(params['designation']),
other_cycles: params['other_cycles'].map{|cycle| cycle['cycle']['cycle']}
end

def self.create_from_search_results(params={})
self.new :name => params['name'],
:id => params['id'],
:city => params['city'],
:state => params['state'],
:zip => params['zip'],
:district => params['district'],
:party => params['party'],
:candidate_id => parse_candidate(params['candidate']),
:treasurer => params['treasurer'],
:fec_uri => params['fec_uri'],
:leadership => params['leadership'],
:super_pac => params['super_pac']
self.new name: params['name'],
id: params['id'],
city: params['city'],
state: params['state'],
zip: params['zip'],
district: params['district'],
party: params['party'],
candidate_id: parse_candidate(params['candidate']),
treasurer: params['treasurer'],
fec_uri: params['fec_uri'],
leadership: params['leadership'],
super_pac: params['super_pac']
end

def self.get_frequency(frequency)
Expand Down Expand Up @@ -137,7 +137,7 @@ def self.find(fecid, cycle=CURRENT_CYCLE)
end

def self.search(name, cycle=CURRENT_CYCLE, offset=nil)
reply = invoke("#{cycle}/committees/search", {:query => name,:offset => offset})
reply = invoke("#{cycle}/committees/search", {query: name, offset: offset})
results = reply['results']
results.map{|c| create_from_search_results(c)}
end
Expand All @@ -149,19 +149,19 @@ def self.latest(cycle=CURRENT_CYCLE)
end

def self.superpacs(cycle=CURRENT_CYCLE, offset=nil)
reply = invoke("#{cycle}/committees/superpacs",{:offset => offset})
reply = invoke("#{cycle}/committees/superpacs",{offset: offset})
results = reply['results']
results.map{|c| create_from_search_results(c)}
end

def filings(cycle=CURRENT_CYCLE, offset=nil)
reply = Base.invoke("#{cycle}/committees/#{id}/filings",{:offset => offset})
reply = Base.invoke("#{cycle}/committees/#{id}/filings",{offset: offset})
results = reply['results']
results.map{|c| Filing.create(c)}
end

def unamended_filings(cycle=CURRENT_CYCLE, offset=nil)
reply = Base.invoke("#{cycle}/committees/#{id}/filings",{:offset => offset})
reply = Base.invoke("#{cycle}/committees/#{id}/filings",{offset: offset})
results = reply['results'].select{|f| f['amended'] == false}
results.map{|c| Filing.create(c, name=self.name)}
end
Expand Down

0 comments on commit bed8ebf

Please sign in to comment.