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, request, response_child, response_child_array, 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

#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



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

def self.find_all_by_address address, city, state, zip
  placemark = 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



59
60
61
62
63
64
65
66
67
# File 'lib/vote_smart/official.rb', line 59

def self.find_all_by_state_and_latitude_and_longitude state, latitude, longitude
  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



69
70
71
72
73
74
75
76
77
78
# File 'lib/vote_smart/official.rb', line 69

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



27
28
29
30
31
32
# File 'lib/vote_smart/official.rb', line 27

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



34
35
36
37
# File 'lib/vote_smart/official.rb', line 34

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



39
40
41
42
43
44
45
# File 'lib/vote_smart/official.rb', line 39

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



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/vote_smart/official.rb', line 80

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



123
124
125
# File 'lib/vote_smart/official.rb', line 123

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



118
119
120
# File 'lib/vote_smart/official.rb', line 118

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



108
109
110
# File 'lib/vote_smart/official.rb', line 108

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

.get_by_levenstein(lastname) ⇒ Object

Searches for incumbents with fuzzy lastname match



113
114
115
# File 'lib/vote_smart/official.rb', line 113

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



103
104
105
# File 'lib/vote_smart/official.rb', line 103

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

Instance Method Details

#inspectObject



19
20
21
# File 'lib/vote_smart/official.rb', line 19

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

#officesObject



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

def offices
  Official.response_child_array(Address.get_office(self.id), "address", "office").collect {|office| CandidateOffice.new(office) }
end