Class: Daywalker::Legislator
- Includes:
- HappyMapper
- Defined in:
- lib/daywalker/legislator.rb
Overview
Represents a legislator, either a Senator or Representative.
They have the following attributes:
title
-
The title held by a Legislator, as a Symbol, ether
:senator
or:representative
first_name
-
Legislator’s first name
middle_name
-
Legislator’s middle name
last_name
-
Legislator’s last name
name_suffix
-
Legislator’s suffix (Jr., III, etc)
nickname
-
Preferred nickname of Legislator
party
-
Legislator’s party as a
Sybmol
,:democrat
,:republican
, or:independent
. state
-
two-letter
String
abbreviation of the Legislator’s state. district
-
The district a Legislator represents. For Representatives, this is a
Fixnum
. For Senators, this is aSymbol
, either:junior_seat
or:senior_seat
. in_office
-
true
if the Legislator is currently server, or false if the Legislator is no longer due to defeat/resignation/death/etc. gender
-
Legislator’s gender as a
Symbol
, :male or :female phone
-
Legislator’s Congressional office phone number # FIXME normalize this to phone_number
fax_number
-
Legislator’s Congressional office fax number
website_url
-
URL of the Legislator’s Congressional wesbite as a
String
webform_url
-
URL of the Legislator’s web contact form as a
String
email
-
Legislator’s email address
congress_office
-
Legislator’s Washington, DC Office Address
bioguide_id
-
Legislator’s ID assigned by the COngressional Biographical DIrectory (bioguide.congress.gov) and also used by the Washington Post and NY Times.
votesmart_id
-
Legislator ID assigned by Project Vote Smart (www.votesmart.org).
fec_id
-
Legislator’s ID provided by the Federal Election Commission (fec.gov)
govtrack_id
-
Legislator’s ID provided by Govtrack.us (www.govtrack.us)
crp_id
-
Legislator’s ID provided by Center for Responsive Politics (opensecrets.org)
eventful_id
-
Legislator’s ‘Performer ID’ on eventful.com
congresspedia_url
-
URL of the Legislator’s Congresspedia entry (congresspedia.org)
twitter_id
-
Legislator’s ID on Twitter (twitter.com)
youtube_url
-
URL of the Legislator’s YouTube account (youtube.com) as a
String
Constant Summary collapse
- VALID_ATTRIBUTES =
[:district, :title, :eventful_id, :in_office, :state, :votesmart_id, :official_rss_url, :party, :email, :crp_id, :website_url, :fax_number, :govtrack_id, :first_name, :middle_name, :last_name, :congress_office, :bioguide_id, :webform_url, :youtube_url, :nickname, :phone, :fec_id, :gender, :name_suffix, :twitter_id, :sunlight_old_id, :congresspedia_url]
Class Method Summary collapse
-
.all(conditions = {}) ⇒ Object
Find all legislators matching a Hash of attribute name/values.
-
.all_by_address(address) ⇒ Object
Find all the legislators serving a specific address.
-
.all_by_latitude_and_longitude(latitude, longitude) ⇒ Object
Find all the legislators serving a specific latitude and longitude.
-
.all_by_zip(zip) ⇒ Object
Find all Legislators who serve a particular zip code.
-
.method_missing(method_id, *args, &block) ⇒ Object
:nodoc:.
-
.respond_to?(method_id, include_private = false) ⇒ Boolean
:nodoc:.
-
.unique(conditions) ⇒ Object
Find a unique legislator matching a Hash of attribute name/values.
Instance Method Summary collapse
- #full_name ⇒ Object
-
#initialize(attributes = {}) ⇒ Legislator
constructor
A new instance of Legislator.
Constructor Details
#initialize(attributes = {}) ⇒ Legislator
Returns a new instance of Legislator.
119 120 121 122 123 |
# File 'lib/daywalker/legislator.rb', line 119 def initialize(attributes = {}) attributes.each do |attribute, value| send("#{attribute}=", value) end end |
Class Method Details
.all(conditions = {}) ⇒ Object
Find all legislators matching a Hash of attribute name/values. See VALID_ATTRIBUTES for possible attributes.
Daywalker::Legislator.all(:state => 'NY', :title => :senator)
Dynamic finders based on the attribute names are also possible. This query can be rewritten as:
Daywalker::Legislator.all_by_state_and_title('NY', :senator)
Returns an Array of Legislators.
Gotchas:
-
Results are case insensative (Richard and richard are equivilant)
-
Results are exact (Richard vs Rich)
-
nil attributes will match anything, not legislators without a value for the attribute
-
Passing an Array of values to match, ie
:state => ['NH', 'MA']
is not supported at this time
179 180 181 182 183 184 185 186 |
# File 'lib/daywalker/legislator.rb', line 179 def self.all(conditions = {}) conditions = TypeConverter.normalize_conditions(conditions) query = conditions.merge(:apikey => Daywalker.api_key) response = get('/legislators.getList.xml', :query => query) handle_response(response) end |
.all_by_address(address) ⇒ Object
Find all the legislators serving a specific address. This will include the district’s Represenative, the Senior Senator, and the Junior Senator.
Returns a Hash containing keys :representative, :junior_senator, and :senior_senator, with values corresponding to the appropriate Legislator.
Raises Daywalker::AddressError if the address can’t be geocoded.
203 204 205 206 207 |
# File 'lib/daywalker/legislator.rb', line 203 def self.all_by_address(address) location = Daywalker.geocoder.locate(address) all_by_latitude_and_longitude(location[:latitude], location[:longitude]) end |
.all_by_latitude_and_longitude(latitude, longitude) ⇒ Object
Find all the legislators serving a specific latitude and longitude. This will include the district’s Represenative, the Senior Senator, and the Junior Senator.
Returns a Hash containing keys :representative, :junior_senator, and :senior_senator, with values corresponding to the appropriate Legislator.
192 193 194 195 196 |
# File 'lib/daywalker/legislator.rb', line 192 def self.all_by_latitude_and_longitude(latitude, longitude) district = District.unique_by_latitude_and_longitude(latitude, longitude) district.legislators end |
.all_by_zip(zip) ⇒ Object
Find all Legislators who serve a particular zip code. This would include the Junior and Senior Senators, as well as the Representatives of any Districts in the zip code.
130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/daywalker/legislator.rb', line 130 def self.all_by_zip(zip) raise ArgumentError, 'missing required parameter zip' if zip.nil? query = { :zip => zip, :apikey => Daywalker.api_key } response = get('/legislators.allForZip.xml', :query => query) handle_response(response) end |
.method_missing(method_id, *args, &block) ⇒ Object
:nodoc:
209 210 211 212 213 214 215 216 217 |
# File 'lib/daywalker/legislator.rb', line 209 def self.method_missing(method_id, *args, &block) # :nodoc: match = DynamicFinderMatch.new(method_id) if match.match? create_finder_method(method_id, match.finder, match.attribute_names) send(method_id, *args, &block) else super end end |
.respond_to?(method_id, include_private = false) ⇒ Boolean
:nodoc:
219 220 221 222 223 224 225 226 |
# File 'lib/daywalker/legislator.rb', line 219 def self.respond_to?(method_id, include_private = false) # :nodoc: match = DynamicFinderMatch.new(method_id) if match.match? true else super end end |
.unique(conditions) ⇒ Object
Find a unique legislator matching a Hash of attribute name/values. See VALID_ATTRIBUTES for possible attributes.
Daywalker::Legislator.unique(:state => 'NY', :district => 4)
Dynamic finders based on the attribute names are also possible. This query can be rewritten as:
Daywalker::Legislator.unique_by_state_and_district('NY', 4)
Returns a Legislator. ArgumentError is raised if more than one result is found.
Gotchas:
-
Results are case insensative (Richard and richard are equivilant)
-
Results are exact (Richard vs Rich are not the same)
155 156 157 158 159 160 161 162 |
# File 'lib/daywalker/legislator.rb', line 155 def self.unique(conditions) conditions = TypeConverter.normalize_conditions(conditions) query = conditions.merge(:apikey => Daywalker.api_key) response = get('/legislators.get.xml', :query => query) handle_response(response).first end |
Instance Method Details
#full_name ⇒ Object
125 126 127 |
# File 'lib/daywalker/legislator.rb', line 125 def full_name [first_name, middle_name, last_name, name_suffix].compact.join(' ') end |