Class: Sunlight::Congress::Legislator

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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(options = {})
  options.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, options)
  uri = URI("#{BASE_URI}/#{location}")
  uri.query = URI.encode_www_form(options.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(options = {})
  encode_uri('legislators', options)
end

.locate(options) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/sunlight/congress/legislator.rb', line 32

def locate(options)
  if options.is_a?(String) || options[:address]
    coords = Geocoder.coordinates(options)
    response = Net::HTTP.get_response(locate_uri(latitude: coords[0], longitude: coords[1]))
  else
    response = Net::HTTP.get_response(locate_uri(options))
  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(options = {})
  encode_uri('legislators/locate', options)
end