Class: VoteSmart::Official

Inherits:
Common
  • Object
show all
Defined in:
lib/vote_smart/official.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Common

construct_url, get_json_data, hash2get, #initialize, parallelize!, request, response_child, session, set_attribute_map, #update_attributes

Constructor Details

This class inherits a constructor from VoteSmart::Common

Instance Attribute Details

#districtObject

Returns the value of attribute district.



8
9
10
# File 'lib/vote_smart/official.rb', line 8

def district
  @district
end

#district_idObject

Returns the value of attribute district_id.



5
6
7
# File 'lib/vote_smart/official.rb', line 5

def district_id
  @district_id
end

#district_nameObject

Returns the value of attribute district_name.



5
6
7
# File 'lib/vote_smart/official.rb', line 5

def district_name
  @district_name
end

#election_partiesObject

Returns the value of attribute election_parties.



5
6
7
# File 'lib/vote_smart/official.rb', line 5

def election_parties
  @election_parties
end

#first_nameObject

Returns the value of attribute first_name.



5
6
7
# File 'lib/vote_smart/official.rb', line 5

def first_name
  @first_name
end

#idObject

Returns the value of attribute id.



5
6
7
# File 'lib/vote_smart/official.rb', line 5

def id
  @id
end

#last_nameObject

Returns the value of attribute last_name.



5
6
7
# File 'lib/vote_smart/official.rb', line 5

def last_name
  @last_name
end

#middle_nameObject

Returns the value of attribute middle_name.



5
6
7
# File 'lib/vote_smart/official.rb', line 5

def middle_name
  @middle_name
end

#nick_nameObject

Returns the value of attribute nick_name.



5
6
7
# File 'lib/vote_smart/official.rb', line 5

def nick_name
  @nick_name
end

#officeObject

Returns the value of attribute office.



8
9
10
# File 'lib/vote_smart/official.rb', line 8

def office
  @office
end

#office_idObject

Returns the value of attribute office_id.



8
9
10
# File 'lib/vote_smart/official.rb', line 8

def office_id
  @office_id
end

#office_partiesObject

Returns the value of attribute office_parties.



5
6
7
# File 'lib/vote_smart/official.rb', line 5

def office_parties
  @office_parties
end

#partyObject

Returns the value of attribute party.



8
9
10
# File 'lib/vote_smart/official.rb', line 8

def party
  @party
end

#state_idObject

Returns the value of attribute state_id.



5
6
7
# File 'lib/vote_smart/official.rb', line 5

def state_id
  @state_id
end

#suffixObject

Returns the value of attribute suffix.



5
6
7
# File 'lib/vote_smart/official.rb', line 5

def suffix
  @suffix
end

#titleObject

Returns the value of attribute title.



5
6
7
# File 'lib/vote_smart/official.rb', line 5

def title
  @title
end

Class Method Details

.find_all_by_address(address, city, state, zip) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/vote_smart/official.rb', line 50

def self.find_all_by_address address, city, state, zip
  require 'ym4r/google_maps/geocoding'

  placemark = Ym4r::GoogleMaps::Geocoding.get("#{address} #{city}, #{state} #{zip}").first
  
  return [] unless placemark
  
  state ||= placemark.administrative_area
  
  placemark ? find_all_by_state_and_latitude_and_longitude(state, placemark.latitude, placemark.longitude) : []
end

.find_all_by_state_and_latitude_and_longitude(state, latitude, longitude) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/vote_smart/official.rb', line 62

def self.find_all_by_state_and_latitude_and_longitude state, latitude, longitude
  require "mcll4r"

  response = Mcll4r.new.district_lookup(latitude, longitude)
  response = response["response"] if response

  state_upper = (response["state_upper"] || {})["district"]
  state_lower = (response["state_lower"] || {})["district"]

  find_all_state_wide_officials(state) + find_state_legislators(state, state_upper, state_lower, state_lower)
end

.find_all_state_wide_officials(state_id) ⇒ Object



74
75
76
77
78
79
80
81
82
83
# File 'lib/vote_smart/official.rb', line 74

def self.find_all_state_wide_officials state_id
  state_wide_office_names = ["Secretary of State", "Secretary of the Commonwealth", "Attorney General", "Secretary of Education",
                             "Education Secretary", "Treasurer", "Auditor"]

  state_wide_type = Office::Type.find_by_name("State Wide")

  state_wide_offices = state_wide_type ? state_wide_type.offices_by_name(state_wide_office_names) : []

  state_wide_offices.collect { |office| office.official_by_state_id(state_id) }.compact
end

.find_by_district(district) ⇒ Object



30
31
32
33
34
35
# File 'lib/vote_smart/official.rb', line 30

def self.find_by_district district
  official = find_by_district_id district.id
  official.district = district if official
  official.office = district.office if official
  official
end

.find_by_district_id(district_id) ⇒ Object



37
38
39
40
# File 'lib/vote_smart/official.rb', line 37

def self.find_by_district_id district_id
  response = response_child(get_by_district(district_id), "candidateList", "candidate")
  Official.new(response) unless response.empty?
end

.find_by_office_id_and_state_id(office_id, state_id) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/vote_smart/official.rb', line 42

def self.find_by_office_id_and_state_id office_id, state_id
  response = response_child(get_by_office_state(office_id, state_id), "candidateList", "candidate")
  official = Official.new(response) unless response.empty?
  official.office_id = office_id if official
  official.state_id ||= state_id if official
  official
end

.find_state_legislators(state_id, senate_district, house_district, assembly_district) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/vote_smart/official.rb', line 85

def self.find_state_legislators state_id, senate_district, house_district, assembly_district
  officials = []

  state_leg_type = Office::Type.find_by_name("State Legislature")

  {"State Senate" => senate_district,
   "State House" => house_district,
   "State Assembly" => assembly_district}.each do |office_name, district_number|

     office = state_leg_type.office_by_name(office_name) if state_leg_type

     district = office.district_by_state_id_and_number(state_id, district_number) if office

     official = district.official if district

     officials.push official if official
  end

  officials
end

.get_by_district(district_id) ⇒ Object

Returns incumbents in the provided district_id



131
132
133
# File 'lib/vote_smart/official.rb', line 131

def self.get_by_district district_id
  request("Officials.getByDistrict", "districtId" => district_id)
end

.get_by_election(election_id) ⇒ Object

Returns incumbents in the provided election_id



126
127
128
# File 'lib/vote_smart/official.rb', line 126

def self.get_by_election election_id
  request("Officials.getByElection", "electionId" => election_id)
end

.get_by_lastname(lastname) ⇒ Object

Searches for incumbents with exact lastname matches



116
117
118
# File 'lib/vote_smart/official.rb', line 116

def self.get_by_lastname lastname
  request("Officials.getByLastname", "lastName" => lastname)
end

.get_by_levenstein(lastname) ⇒ Object

Searches for incumbents with fuzzy lastname match



121
122
123
# File 'lib/vote_smart/official.rb', line 121

def self.get_by_levenstein lastname
  request("Officials.getByLevenstein", "lastName" => lastname)
end

.get_by_office_state(office_id, state_id = 'NA') ⇒ Object

Returns a list of incumbents that fit the criteria



111
112
113
# File 'lib/vote_smart/official.rb', line 111

def self.get_by_office_state office_id, state_id = 'NA'
  request("Officials.getByOfficeState", "officeId" => office_id, "stateId" => state_id)
end

.get_by_zip(zip5, election_year = nil, zip4 = nil, stage_id = nil) ⇒ Object

Returns incumbents in the provided zip code, with optional zip+4 and stage



136
137
138
# File 'lib/vote_smart/official.rb', line 136

def self.get_by_zip zip5, election_year=nil, zip4=nil, stage_id=nil
  request("Officials.getByZip", "zip5" => zip5, "electionYear" => election_year, "zip4" => zip4, "stageId" => stage_id)
end

.get_statewide(state_id = 'NA') ⇒ Object



106
107
108
# File 'lib/vote_smart/official.rb', line 106

def self.get_statewide(state_id = 'NA')
  request("Officials.getStatewide", "stateId" => state_id)
end

Instance Method Details

#inspectObject



22
23
24
# File 'lib/vote_smart/official.rb', line 22

def inspect
  "Official: " + [title, first_name, last_name].compact.join(' ')
end

#officesObject



15
16
17
18
19
20
# File 'lib/vote_smart/official.rb', line 15

def offices
  offices = Official.response_child(Address.get_office(self.id), "address", "office")
  (offices.is_a?(Array) ? offices : [offices]).collect {|office| 
    CandidateOffice.new(office)
  }
end