Class: VoteSmart::Office

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

Defined Under Namespace

Classes: Type

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

#branch_idObject

Returns the value of attribute branch_id.



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

def branch_id
  @branch_id
end

#idObject

Returns the value of attribute id.



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

def id
  @id
end

#level_idObject

Returns the value of attribute level_id.



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

def level_id
  @level_id
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#officeObject

Returns the value of attribute office.



7
8
9
# File 'lib/vote_smart/office.rb', line 7

def office
  @office
end

#short_titleObject

Returns the value of attribute short_title.



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

def short_title
  @short_title
end

#titleObject

Returns the value of attribute title.



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

def title
  @title
end

#type_idObject

Returns the value of attribute type_id.



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

def type_id
  @type_id
end

Class Method Details

.allObject



32
33
34
# File 'lib/vote_smart/office.rb', line 32

def self.all
  Type.all.collect { |type| find_all_by_type(type) }.uniq.flatten
end

.find_all_by_name(names) ⇒ Object



48
49
50
51
# File 'lib/vote_smart/office.rb', line 48

def self.find_all_by_name names
  offices = Office.all
  names.collect {|name| offices.find {|office| office.name == name }}
end

.find_all_by_type(type) ⇒ Object



36
37
38
# File 'lib/vote_smart/office.rb', line 36

def self.find_all_by_type type
  find_all_by_type_id(type.id)
end

.find_all_by_type_id(type_id) ⇒ Object



40
41
42
# File 'lib/vote_smart/office.rb', line 40

def self.find_all_by_type_id type_id
  response_child(get_offices_by_type(type_id), "offices", "office").collect {|attributes| Office.new(attributes)}
end

.find_by_name(name) ⇒ Object



44
45
46
# File 'lib/vote_smart/office.rb', line 44

def self.find_by_name name
  Office.all.collect {|office| office if office.name == name }.compact.first
end

.get_branchesObject

Returns branches of government and their respective IDs



59
60
61
# File 'lib/vote_smart/office.rb', line 59

def self.get_branches
  request("Office.getBranches")
end

.get_levelsObject

Returns levels of government and their respective IDs



64
65
66
# File 'lib/vote_smart/office.rb', line 64

def self.get_levels
  request("Office.getLevels")
end

.get_offices_by_branch_level(branch_id, level_id) ⇒ Object

Returns a list of offices by branch and level of government



84
85
86
# File 'lib/vote_smart/office.rb', line 84

def self.get_offices_by_branch_level branch_id, level_id
  request("Office.getOfficesByBranchLevel", "branchId" => branch_id, "levelId" => level_id)
end

.get_offices_by_level(level_id) ⇒ Object

Returns a list of offices by level of government



74
75
76
# File 'lib/vote_smart/office.rb', line 74

def self.get_offices_by_level level_id
  request("Office.getOfficesByLevel", "levelId" => level_id)
end

.get_offices_by_type(type_id) ⇒ Object

Returns a list of offices by office type



69
70
71
# File 'lib/vote_smart/office.rb', line 69

def self.get_offices_by_type type_id
  request("Office.getOfficesByType", "officeTypeId" => type_id)
end

.get_offices_by_type_level(type_id, level_id) ⇒ Object

Returns a list of offices by office type and level of government



79
80
81
# File 'lib/vote_smart/office.rb', line 79

def self.get_offices_by_type_level type_id, level_id
  request("Office.getOfficesByTypeLevel", "levelId" => level_id, "officeTypeId" => type_id)
end

.get_typesObject

Gets a list of office types



54
55
56
# File 'lib/vote_smart/office.rb', line 54

def self.get_types
  request("Office.getTypes")
end

Instance Method Details

#district_by_state_id_and_number(state_id, number) ⇒ Object



19
20
21
22
23
# File 'lib/vote_smart/office.rb', line 19

def district_by_state_id_and_number state_id, number
  number = number.to_i
  districts = districts_by_state_id(state_id)
  districts.find {|district| district.number == number}
end

#districts_by_state_id(state_id) ⇒ Object



12
13
14
15
16
17
# File 'lib/vote_smart/office.rb', line 12

def districts_by_state_id state_id
  @districts_by_state ||= {}
  districts = District.find_all_by_office_id_and_state_id(self.id, state_id)
  districts.each { |district| district.office = self }
  @districts_by_state[state_id] ||= districts
end

#official_by_state_id(state_id) ⇒ Object



25
26
27
28
29
30
# File 'lib/vote_smart/office.rb', line 25

def official_by_state_id state_id
  @official_by_state ||= {}
  official = Official.find_by_office_id_and_state_id(self.id, state_id)
  official.office = self if official
  @official_by_state[state_id] ||= official
end