Class: Sunlight::Congress::Legislator
- Inherits:
-
Object
- Object
- Sunlight::Congress::Legislator
- Defined in:
- lib/sunlight/congress/legislator.rb
Constant Summary collapse
- SEARCH_ATTRIBUTES =
[:first_name, :middle_name, :last_name, :name_suffix, :nickname, :state, :state_name, :state_rank, :party, :chamber, :title, :senate_class, :district, :term_start, :term_end, :birthday, :fax, :in_office, :bioguide_id, :crp_id, :govtrack_id, :icpsr_id, :fec_ids, :lis_id, :ocd_id, :thomas_id, :votesmart_id, :oc_email]
Class Method Summary collapse
- .create_from_response(response) ⇒ Object
- .encode_uri(location, options) ⇒ Object
- .find(query) ⇒ Object
- .find_uri(options = {}) ⇒ Object
- .locate(options) ⇒ Object
- .locate_by_zip(zip) ⇒ Object
- .locate_uri(options = {}) ⇒ Object
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Legislator
constructor
A new instance of Legislator.
Constructor Details
#initialize(options = {}) ⇒ Legislator
Returns a new instance of Legislator.
17 18 19 20 21 |
# File 'lib/sunlight/congress/legislator.rb', line 17 def initialize( = {}) .each do |attribute, value| instance_variable_set("@#{attribute}", value) end end |
Class Method Details
.create_from_response(response) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/sunlight/congress/legislator.rb', line 76 def create_from_response(response) json = JSON.parse(response.body) if results = json['results'] results.map do |legislator_attrs| self.new(legislator_attrs) end elsif errors = json['error'] json end end |
.encode_uri(location, options) ⇒ Object
69 70 71 72 73 74 |
# File 'lib/sunlight/congress/legislator.rb', line 69 def encode_uri(location, ) uri = URI("#{BASE_URI}/#{location}") uri.query = URI.encode_www_form(.merge(apikey: '6d4acf5a753c40e1824857bb12679d89')) uri end |
.find(query) ⇒ Object
47 48 49 50 51 52 53 54 55 |
# File 'lib/sunlight/congress/legislator.rb', line 47 def find(query) if query.is_a?(String) response = Net::HTTP.get_response(find_uri(query: query)) else response = Net::HTTP.get_response(find_uri(query)) end create_from_response(response) end |
.find_uri(options = {}) ⇒ Object
65 66 67 |
# File 'lib/sunlight/congress/legislator.rb', line 65 def find_uri( = {}) encode_uri('legislators', ) end |
.locate(options) ⇒ Object
32 33 34 35 36 37 38 39 40 41 |
# File 'lib/sunlight/congress/legislator.rb', line 32 def locate() if .is_a?(String) || [:address] coords = Geocoder.coordinates() response = Net::HTTP.get_response(locate_uri(latitude: coords[0], longitude: coords[1])) else response = Net::HTTP.get_response(locate_uri()) end create_from_response(response) end |
.locate_by_zip(zip) ⇒ Object
24 25 26 27 28 29 30 |
# File 'lib/sunlight/congress/legislator.rb', line 24 def locate_by_zip(zip) response = Net::HTTP.get_response(locate_uri(zip: zip)) JSON.parse(response.body)['results'].map do |legislator_attrs| self.new(legislator_attrs) end end |
.locate_uri(options = {}) ⇒ Object
43 44 45 |
# File 'lib/sunlight/congress/legislator.rb', line 43 def locate_uri( = {}) encode_uri('legislators/locate', ) end |