Class: CongressPerson

Inherits:
GovSdkBase show all
Defined in:
lib/congress_person.rb

Overview

  • Name: GovSDK

    • Description:

    • Author: Pito Salas

    • Copyright: © R. Pito Salas and Associates, Inc.

    • Date: January 2009

    • License: GPL

    This file is part of GovSDK.

    GovSDK is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

    GovSDK is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along with GovSDK. If not, see <www.gnu.org/licenses/>.

    require “ruby-debug” Debugger.settings = 1 # list nearby lines on stop Debugger.settings = 1 Debugger.start

Constant Summary collapse

SUNLIGHT_MAP =

Maps specific set of CongressPerson attributes to Sunlight APIs. Mapping is structured as “name of attr in CongressPerson object” => “name of hash key in result returned from API call”

{ 
  "firstname"         => "firstname",
  "lastname"          => "lastname",
  "nickname"          => "nickname",
  "party"             => "party",
  "state"             => "state",
  "congress_office"   => "congress_office",
  "phone"             => "phone",
  "fax"               => "fax",
  "email"             => "email",
  "website"           => "website",
  "webform"           => "webform",
  "bioguide_id"       => "bioguide_id",
  "votesmart_id"      => "votesmart_id",
  "congresspedia_url" => "congresspedia_url",
  "fec_id"            => "fec_id",
  "district"          => "district",
  "title"             => "title",
  "govtrack_id"       => "govtrack_id",
  "crp_id"            => "crp_id"
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from GovSdkBase

assume_hash, #assume_hash, #assume_nil_or_integer, #assume_not_blank, #assume_string, assume_string, #assume_uses_google_api, #assume_uses_sunl_api, assume_uses_sunl_api

Instance Attribute Details

#bioguide_idObject (readonly)

Returns the value of attribute bioguide_id.



33
34
35
# File 'lib/congress_person.rb', line 33

def bioguide_id
  @bioguide_id
end

#congress_officeObject (readonly)

Returns the value of attribute congress_office.



33
34
35
# File 'lib/congress_person.rb', line 33

def congress_office
  @congress_office
end

#congresspedia_urlObject (readonly)

Returns the value of attribute congresspedia_url.



33
34
35
# File 'lib/congress_person.rb', line 33

def congresspedia_url
  @congresspedia_url
end

#crp_idObject (readonly)

Returns the value of attribute crp_id.



33
34
35
# File 'lib/congress_person.rb', line 33

def crp_id
  @crp_id
end

#districtObject (readonly)

Returns the value of attribute district.



33
34
35
# File 'lib/congress_person.rb', line 33

def district
  @district
end

#emaiObject (readonly)

Returns the value of attribute emai.



33
34
35
# File 'lib/congress_person.rb', line 33

def emai
  @emai
end

#faxObject (readonly)

Returns the value of attribute fax.



33
34
35
# File 'lib/congress_person.rb', line 33

def fax
  @fax
end

#firstnameObject (readonly)

Returns the value of attribute firstname.



33
34
35
# File 'lib/congress_person.rb', line 33

def firstname
  @firstname
end

#govtrack_idObject (readonly)

Returns the value of attribute govtrack_id.



33
34
35
# File 'lib/congress_person.rb', line 33

def govtrack_id
  @govtrack_id
end

#lastnameObject (readonly)

Returns the value of attribute lastname.



33
34
35
# File 'lib/congress_person.rb', line 33

def lastname
  @lastname
end

#nicknameObject (readonly)

Returns the value of attribute nickname.



33
34
35
# File 'lib/congress_person.rb', line 33

def nickname
  @nickname
end

#partyObject (readonly)

Returns the value of attribute party.



33
34
35
# File 'lib/congress_person.rb', line 33

def party
  @party
end

#phoneObject (readonly)

Returns the value of attribute phone.



33
34
35
# File 'lib/congress_person.rb', line 33

def phone
  @phone
end

#stateObject (readonly)

Returns the value of attribute state.



33
34
35
# File 'lib/congress_person.rb', line 33

def state
  @state
end

#state_machine_idObject (readonly)

Returns the value of attribute state_machine_id.



33
34
35
# File 'lib/congress_person.rb', line 33

def state_machine_id
  @state_machine_id
end

#titleObject (readonly)

Returns the value of attribute title.



33
34
35
# File 'lib/congress_person.rb', line 33

def title
  @title
end

#votesmart_idObject (readonly)

Returns the value of attribute votesmart_id.



33
34
35
# File 'lib/congress_person.rb', line 33

def votesmart_id
  @votesmart_id
end

#webformObject (readonly)

Returns the value of attribute webform.



33
34
35
# File 'lib/congress_person.rb', line 33

def webform
  @webform
end

#websiteObject (readonly)

Returns the value of attribute website.



33
34
35
# File 'lib/congress_person.rb', line 33

def website
  @website
end

#website_urlObject (readonly)

Returns the value of attribute website_url.



33
34
35
# File 'lib/congress_person.rb', line 33

def website_url
  @website_url
end

Class Method Details

.convert_to_congressperson(sunlight_legislator) ⇒ Object



69
70
71
72
73
74
# File 'lib/congress_person.rb', line 69

def self.convert_to_congressperson(sunlight_legislator)
  cong_person = CongressPerson.new
  leg_hash = sunlight_legislator
  copy_sun_properties(leg_hash, cong_person)
  cong_person
end

.copy_sun_properties(sunlight_hash, cong_person) ⇒ Object



62
63
64
65
66
67
# File 'lib/congress_person.rb', line 62

def self.copy_sun_properties(sunlight_hash, cong_person)
  SUNLIGHT_MAP.each do |name, attribute|
    val = sunlight_hash[attribute]
    cong_person.instance_variable_set("@#{name}", val)
  end
end

.find_by_crp_id(crpId) ⇒ Object

Raises:

  • (ArgumentError)


101
102
103
104
105
106
107
108
109
110
111
# File 'lib/congress_person.rb', line 101

def self.find_by_crp_id(crpId)
  raise ArgumentError, 'Crp-Id should be a string' unless crpId.kind_of?(String)
  sunlight_hash = GovSdk.sunlight_api.legislators_get(:crp_id => crpId)
  if !sunlight_hash.nil?
    cong_person = CongressPerson.new
    copy_sun_properties(sunlight_hash, cong_person)
    cong_person
  else
    nil
  end
end

.find_by_names(firstname, lastname) ⇒ Object

Return an array of Congress People, searching using first and last name



83
84
85
86
87
88
89
# File 'lib/congress_person.rb', line 83

def self.find_by_names(firstname, lastname)
  assume_string lastname
  assume_string firstname
  assume_uses_sunl_api
  result_array = GovSdk.sunlight_api.legislators_getlist(:firstname => firstname, :lastname => lastname)
  result_array.collect { |leg| convert_to_congressperson(leg["legislator"])}
end

.find_by_query(query) ⇒ Object



95
96
97
98
99
# File 'lib/congress_person.rb', line 95

def self.find_by_query(query)
  assume_hash query
  result_array = GovSdk.sunlight_api.legislators_getlist(query)
  result_array.collect { |leg| convert_to_congressperson(leg["legislator"])}
end

.find_by_zipcode(zip) ⇒ Object

Raises:

  • (ArgumentError)


91
92
93
# File 'lib/congress_person.rb', line 91

def self.find_by_zipcode(zip)
  raise ArgumentError, 'zipcodes must be Strings' unless zip.is_a?(String)
end

.fuzzy_find_by_name(name) ⇒ Object

Raises:

  • (ArgumentError)


76
77
78
79
80
# File 'lib/congress_person.rb', line 76

def self.fuzzy_find_by_name(name)
  raise ArgumentError, 'names must be Strings' unless name.is_a?(String)
  sunlight_hash = GovSdk.sunlight_api.legislators_search(name)
  sunlight_hash.collect {|leg| convert_to_congressperson(leg["result"]["legislator"])}
end

Instance Method Details

#blog_urlObject

Return url of the Congress Person’s web site, or nil if we can’t find one.



138
139
140
141
142
143
144
145
146
147
# File 'lib/congress_person.rb', line 138

def blog_url
  assume_uses_google_api
  return nil if website.nil?
  blog = GovSdk.google_api.lookup_feed_url(website)
  if blog.nil? || blog.empty?
    nil
  else
    blog
  end
end

#get_fundraising_summary(electionCycle = nil) ⇒ Object

Raises:

  • (ArgumentError)


113
114
115
116
117
# File 'lib/congress_person.rb', line 113

def get_fundraising_summary(electionCycle = nil)
  raise ArgumentError, 'election cycle should be nil or an integer' unless electionCycle.nil? || electionCycle.kind_of?(Integer)
  fund_summary_hash = GovSdk.opensecrets_api.get_cand_summary_for_crpID(crp_id, electionCycle)
  fr_summary = FundraisingSummary.new(fund_summary_hash)
end

#get_positions_held(electionCycle = nil) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
# File 'lib/congress_person.rb', line 119

def get_positions_held(electionCycle = nil)
  assume_nil_or_integer electionCycle
  positions = GovSdk.opensecrets_api.get_cand_pfd_positions_held(crp_id, electionCycle)
  if positions.class == Array
    return positions.collect {|pos| Positions.new(pos)}
  elsif positions.class == Hash
    return [Positions.new(positions)]
  else
    []
  end
end

#photo_urlObject

Return url to the photo of this Congress Person. This method uses the votesmart.org/candphoto/1234.jpg resource



132
133
134
135
# File 'lib/congress_person.rb', line 132

def photo_url
  assume_not_blank votesmart_id, "Votesmart_id cannot be blank when calling get_photo_url"
  "http://www.votesmart.org/canphoto/#{@votesmart_id}.jpg"
end