Created
September 18, 2013 21:48
-
-
Save bycoffe/6616237 to your computer and use it in GitHub Desktop.
A Ruby module for searching for FEC images
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# encoding: utf-8 | |
require 'net/http' | |
require 'nokogiri' | |
require 'uri' | |
module PaperChase | |
class Report | |
attr_accessor :type, :amended, :filed_on, :from_date, :end_date, :pages, :page_by_page, :pdf_url | |
def initialize(params={}) | |
@type = params[:type] | |
@amended = params[:amended] | |
@filed_on = params[:filed_on] | |
@from_date = params[:from_date] | |
@end_date = params[:end_date] | |
@pages = params[:pages] | |
@page_by_page = params[:page_by_page] | |
@pdf_url = params[:pdf_url] | |
end | |
end | |
class Committee | |
attr_accessor :id, :name, :city, :state, :party, :designation, :type, :candidate_state, :candidate_office | |
def initialize(params={}) | |
@id = params[:id] | |
@name = params[:name] | |
@city = params[:city] | |
@state = params[:state] | |
@party = params[:party] | |
@designation = params[:designation] | |
@type = params[:type] | |
@candidate_state = params[:candidate_state] | |
@candidate_office = params[:candidate_office] | |
end | |
def uri | |
URI.parse("http://images.nictusa.com/cgi-bin/fecimg/?#{@id}") | |
end | |
def page | |
@page ||= Net::HTTP.get(uri) | |
end | |
def doc | |
@doc ||= Nokogiri::HTML(page) | |
end | |
def reports | |
return @reports if @reports | |
table = doc.css("table")[2] | |
return [] unless table | |
@reports = doc.css("table")[2].css('td[align="center"] a').map do |href| | |
row = href.parent.parent | |
cells = row.css("td") | |
PaperChase::Report.new({ | |
:type => cells[0].text, | |
:amended => cells[1].text, | |
:filed_on => cells[2].text, | |
:from_date => cells[3].text, | |
:end_date => cells[4].text, | |
:pages => cells[5].text, | |
:page_by_page => "http://images.nictusa.com/#{cells[6].css("a")[0].attr("href")}", | |
:pdf_url => "http://images.nictusa.com/#{cells[7].css("a")[0].attr("href")}" | |
}) | |
end | |
@reports | |
end | |
end | |
class Search | |
def initialize(search_params={}) | |
@search_params = make_params(search_params) | |
@uri = URI.parse("http://images.nictusa.com/cgi-bin/fecimg/") | |
@response = search | |
end | |
def make_params(search_params) | |
allowed = [:filerid, :name, :treas, :city, :img_num, :state, :party, :type] | |
params = {} | |
allowed.each do |param| | |
params[param.to_s] = search_params[param] || '' | |
end | |
params | |
end | |
def search | |
Net::HTTP.post_form(@uri, @search_params) | |
end | |
def results(&block) | |
@response.body | |
doc = Nokogiri::HTML(@response.body) | |
rows = [] | |
fields = [:id, :name, :city, :state, :party, :designation, :type, :candidate_state, :candidate_office] | |
doc.css("tr")[1..-1].each do |row| | |
result = PaperChase::Committee.new(Hash[*fields.zip(row.css("td").map { |x| x.text.gsub("\302\240", ' ').strip }).flatten(1)]) | |
if block_given? | |
yield result | |
else | |
rows << result | |
end | |
end | |
block_given? ? nil : rows | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment