Skip to content

Commit

Permalink
spec out more of the disclosure filings and lobbyists
Browse files Browse the repository at this point in the history
  • Loading branch information
Justin Palmer committed Jan 21, 2009
1 parent f1df31f commit 582d423
Show file tree
Hide file tree
Showing 10 changed files with 183 additions and 55 deletions.
28 changes: 0 additions & 28 deletions examples/basic.rb

This file was deleted.

28 changes: 28 additions & 0 deletions examples/basic_example.rb
@@ -0,0 +1,28 @@
require 'pp'
dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
require File.join(dir, 'sunshine')

Sunshine.api_key = File.read(File.join(dir, '..', '.sunlight_api_key'))

pelosi = Sunshine::Legislator.find(:all, {:title => 'Rep'})
pelosi.each do |l|
puts "#{l.full_name}: #{l.district}"
end

many_dudes = Sunshine::Legislator.search('smith')
many_dudes.each do |score, leg|
puts leg.full_name
end

zipdudes = Sunshine::Legislator.find_by_zipcode(97209)
zipdudes.each do |dude|
puts dude.full_name
end

democrats = Sunshine::Legislator.find(:all, :party => 'D')
pp Sunshine::Legislator.find(:one, :firstname => 'nancy')

legislators = Sunshine::Legislator.find_by_lat_long("36.234822", "-115.180664")
legislators.each do |l|
puts l.full_name
end
File renamed without changes.
8 changes: 0 additions & 8 deletions examples/lobbyist.rb

This file was deleted.

19 changes: 19 additions & 0 deletions examples/lobbyist_example.rb
@@ -0,0 +1,19 @@
require 'pp'
dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
require File.join(dir, 'sunshine')

Sunshine.api_key = File.read(File.join(dir, '..', '.sunlight_api_key'))

filing = Sunshine::DisclosureFiling.find('29D4D19E-CB7D-46D2-99F0-27FF15901A4C')
filing.lobbyists.each do |lob|
puts lob.full_name
end

filing = Sunshine::DisclosureFiling.find(:first, {:client_name => 'Sunlight Foundation'})
filing.client_contact_name
#pp filing
#pp filing.raw_data['client_contact_lastname']
puts "CONTACT NAME:#{filing.client_contact_name}"
filing.lobbyists.each do |lob|
puts "-- #{lob.full_name}"
end
28 changes: 14 additions & 14 deletions lib/sunshine/base.rb
Expand Up @@ -6,24 +6,12 @@ module Sunshine
class Base
include HTTParty
base_uri "http://services.sunlightlabs.com/api"

attr_reader :raw_data
class << self; attr_accessor :keys_to_exclude end

# def self.inherited(subclass)
# super
# subclass.instance_variable_set("@keys_to_exclude", instance_variable_get("@keys_to_exclude"))
# end
#
# def self.keys_to_exclude
# self.instance_variable_get("@keys_to_exclude")
# end

def initialize(data)
@raw_data = data
excluded_keys = self.class.keys_to_exclude

puts "KEYS:#{excluded_keys} CLASS:#{self.class}"

data.each do |key, value|
unless !excluded_keys.nil? && excluded_keys.include?(key.to_sym)
instance_variable_set("@#{key}", value)
Expand All @@ -34,6 +22,18 @@ def initialize(data)
end
end

# If you need to define attributes that exist in the JSON data, you can exclude them from
# being automatically defined.
#
# == Example
# class Lobbyist < Base
# exclude :firstname
#
# def firstname
# @raw_data['firstname'].capitalize
# end
# end
#
def self.exclude(*keys)
keys = keys.collect {|k| k.to_sym }.uniq
self.keys_to_exclude = keys || []
Expand All @@ -46,7 +46,7 @@ def self.fetch(method, param_info)
get("/#{method}.json?#{param_info}&apikey=#{Sunshine.api_key}")
rescue Net::HTTPServerException => e
puts "#{e.response.code} ERROR: #{e.response.body_parsed}"
return
return nil
end
end

Expand Down
55 changes: 51 additions & 4 deletions lib/sunshine/disclosure_filing.rb
@@ -1,6 +1,18 @@
module Sunshine
class DisclosureFiling < Base
exclude :lobbyists
exclude :lobbyists, :client_contact_firstname, :client_contact_lastname

def client_contact_name
"#{client_contact_firstname} #{client_contact_lastname}"
end

def client_contact_firstname
@raw_data['client_contact_firstname'].capitalize
end

def client_contact_lastname
@raw_data['client_contact_lastname'].capitalize
end

def lobbyists
@raw_data['lobbyists'].collect do |lb|
Expand All @@ -9,10 +21,45 @@ def lobbyists
end

class << self
# Get a disclosure filing
#
# This wraps all of Sunlights lobbyist methods into a single convenient method.
#
# == Examples
#
# # Find by filing id
# Disclosure.find(filing_id)
# Disclosure.find("29D4D19E-CB7D-46D2-99F0-27FF15901A4C")
#
# # Find by clinet_name or registrant_name. One of these parameters
# # must be present
# Disclosure.find(:all, :client_name => 'Sunlight Foundation')
# Disclosure.find(:all, :registrant_name => 'Sunlight Foundation')
# Disclosure.find(:first, :client_name => 'Sunlight Foundation')
#
def find(*args)
if args.first.is_a?(String)
response = fetch('lobbyists.getFiling', {:id => args.first})
return initialize_one(response['response']['filing'])
results = []
# search by filing id
arguments = args.dup
if arguments.first.is_a?(String)
response = fetch('lobbyists.getFiling', {:id => arguments.first})
results = initialize_one(response['response']['filing'])
else
amount = arguments.shift
arguments = arguments.first
params = {}
params.merge!(:year => arguments[:year]) if arguments[:year]
params.merge!(:client_name => arguments[:client_name]) if arguments[:client_name]
params.merge!(:registrant_name => arguments[:registrant_name]) if arguments[:registrant_name]
response = fetch('lobbyists.getFilingList', params)
if response
if amount == :first
filing = response['response']['filings'].first
return initialize_one(filing['filing']) if filing
elsif amount == :all
return response['response']['filings']
end
end
end
end
end
Expand Down
26 changes: 25 additions & 1 deletion lib/sunshine/lobbyist.rb
@@ -1,5 +1,29 @@
module Sunshine
class Lobbyist < Base

exclude :firstname, :lastname

# def self.search(names, year, threshold)
# results = []
# year = year || Time.now.year
# names = names.join(',') if names.is_a? Array
# response = fetch('lobbyists.search', :name => names, :year => year, :threshold => threshold)
# response['response']['results'].each do |item|
# results << [item['result']['score'], initialize_one(item['result']['lobbyist'])]
# end
# results
# end


def full_name
"#{firstname} #{lastname}"
end

def firstname
@raw_data['firstname'].capitalize
end

def lastname
@raw_data['lastname'].capitalize
end
end
end
35 changes: 35 additions & 0 deletions spec/disclosure_filing_spec.rb
@@ -0,0 +1,35 @@
require 'spec/spec_helper'

include Sunshine

describe DisclosureFiling do
before do
@disclosure = DisclosureFiling.find("29D4D19E-CB7D-46D2-99F0-27FF15901A4C")
end

it "should find a disclosure when given an id" do
@disclosure.should be_instance_of(DisclosureFiling)
end

it "should find a disclosure when given a client name" do
disclosure = DisclosureFiling.find(:first, {:client_name => 'Sunlight Foundation'})
disclosure.should be_instance_of(DisclosureFiling)
end

it "should return nil if a client name and recipient name are both given" do
disclosure = DisclosureFiling.find(:first, {:client_name => 'Sunlight Foundation', :recipient_name => 'Sunlight Foundation'})
disclosure.should_not be(nil)
end

it "should initialize lobbyists for a filing" do
lobbyists = @disclosure.lobbyists
lobbyists.first.should be_instance_of(Lobbyist)
end

it "should compose client contact name from firstname and lastname" do
firstname = @disclosure.raw_data['client_contact_firstname'].capitalize
lastname = @disclosure.raw_data['client_contact_lastname'].capitalize

@disclosure.client_contact_name.should == "#{firstname} #{lastname}"
end
end
11 changes: 11 additions & 0 deletions spec/lobbyists_spec.rb
@@ -0,0 +1,11 @@
require 'spec/spec_helper'

describe Lobbyist do
before do
#@lobbyists.search('Sunlight Foundation')
end

it "should exclude firstname and lastname from instance" do
@speaker.class.keys_to_exclude.should be(['firstname', 'lastname'])
end
end

0 comments on commit 582d423

Please sign in to comment.