Class: Sunlight::Legislator

Inherits:
Base
  • Object
show all
Defined in:
lib/sunlight/legislator.rb

Constant Summary

Constants inherited from Base

Base::API_FORMAT, Base::API_URL

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

api_key, api_key=, construct_url, get_json_data, hash2get

Constructor Details

#initialize(params) ⇒ Legislator

Takes in a hash where the keys are strings (the format passed in by the JSON parser)


14
15
16
17
18
19
# File 'lib/sunlight/legislator.rb', line 14

def initialize(params)
  params.each do |key, value|
    value = Time.parse(value) if key == "birthdate" && value && value.size > 0
    instance_variable_set("@#{key}", value) if Legislator.instance_methods.map { |m| m.to_sym }.include? key.to_sym
  end
end

Instance Attribute Details

#bioguide_idObject

Returns the value of attribute bioguide_id


6
7
8
# File 'lib/sunlight/legislator.rb', line 6

def bioguide_id
  @bioguide_id
end

#birthdateObject

Returns the value of attribute birthdate


6
7
8
# File 'lib/sunlight/legislator.rb', line 6

def birthdate
  @birthdate
end

#congress_officeObject

Returns the value of attribute congress_office


6
7
8
# File 'lib/sunlight/legislator.rb', line 6

def congress_office
  @congress_office
end

#congresspedia_urlObject

Returns the value of attribute congresspedia_url


6
7
8
# File 'lib/sunlight/legislator.rb', line 6

def congresspedia_url
  @congresspedia_url
end

#crp_idObject

Returns the value of attribute crp_id


6
7
8
# File 'lib/sunlight/legislator.rb', line 6

def crp_id
  @crp_id
end

#districtObject

Returns the value of attribute district


6
7
8
# File 'lib/sunlight/legislator.rb', line 6

def district
  @district
end

#emailObject

Returns the value of attribute email


6
7
8
# File 'lib/sunlight/legislator.rb', line 6

def email
  @email
end

#facebook_idObject

Returns the value of attribute facebook_id


6
7
8
# File 'lib/sunlight/legislator.rb', line 6

def facebook_id
  @facebook_id
end

#faxObject

Returns the value of attribute fax


6
7
8
# File 'lib/sunlight/legislator.rb', line 6

def fax
  @fax
end

#fec_idObject

Returns the value of attribute fec_id


6
7
8
# File 'lib/sunlight/legislator.rb', line 6

def fec_id
  @fec_id
end

#firstnameObject

Returns the value of attribute firstname


6
7
8
# File 'lib/sunlight/legislator.rb', line 6

def firstname
  @firstname
end

#fuzzy_scoreObject

Returns the value of attribute fuzzy_score


6
7
8
# File 'lib/sunlight/legislator.rb', line 6

def fuzzy_score
  @fuzzy_score
end

#genderObject

Returns the value of attribute gender


6
7
8
# File 'lib/sunlight/legislator.rb', line 6

def gender
  @gender
end

#govtrack_idObject

Returns the value of attribute govtrack_id


6
7
8
# File 'lib/sunlight/legislator.rb', line 6

def govtrack_id
  @govtrack_id
end

#in_officeObject

Returns the value of attribute in_office


6
7
8
# File 'lib/sunlight/legislator.rb', line 6

def in_office
  @in_office
end

#lastnameObject

Returns the value of attribute lastname


6
7
8
# File 'lib/sunlight/legislator.rb', line 6

def lastname
  @lastname
end

#middlenameObject

Returns the value of attribute middlename


6
7
8
# File 'lib/sunlight/legislator.rb', line 6

def middlename
  @middlename
end

#name_suffixObject

Returns the value of attribute name_suffix


6
7
8
# File 'lib/sunlight/legislator.rb', line 6

def name_suffix
  @name_suffix
end

#nicknameObject

Returns the value of attribute nickname


6
7
8
# File 'lib/sunlight/legislator.rb', line 6

def nickname
  @nickname
end

#partyObject

Returns the value of attribute party


6
7
8
# File 'lib/sunlight/legislator.rb', line 6

def party
  @party
end

#phoneObject

Returns the value of attribute phone


6
7
8
# File 'lib/sunlight/legislator.rb', line 6

def phone
  @phone
end

#senate_classObject

Returns the value of attribute senate_class


6
7
8
# File 'lib/sunlight/legislator.rb', line 6

def senate_class
  @senate_class
end

#stateObject

Returns the value of attribute state


6
7
8
# File 'lib/sunlight/legislator.rb', line 6

def state
  @state
end

#titleObject

Returns the value of attribute title


6
7
8
# File 'lib/sunlight/legislator.rb', line 6

def title
  @title
end

#twitter_idObject

Returns the value of attribute twitter_id


6
7
8
# File 'lib/sunlight/legislator.rb', line 6

def twitter_id
  @twitter_id
end

#votesmart_idObject

Returns the value of attribute votesmart_id


6
7
8
# File 'lib/sunlight/legislator.rb', line 6

def votesmart_id
  @votesmart_id
end

#webformObject

Returns the value of attribute webform


6
7
8
# File 'lib/sunlight/legislator.rb', line 6

def webform
  @webform
end

#websiteObject

Returns the value of attribute website


6
7
8
# File 'lib/sunlight/legislator.rb', line 6

def website
  @website
end

#youtube_urlObject

Returns the value of attribute youtube_url


6
7
8
# File 'lib/sunlight/legislator.rb', line 6

def youtube_url
  @youtube_url
end

Class Method Details

.all_for(params) ⇒ Object

Useful for getting the exact Legislators for a given district.

Returns:

A Hash of the three Members of Congress for a given District: Two Senators and one Representative.

You can pass in lat/long or address. The district will be determined for you:

officials = Legislator.all_for(:latitude => 33.876145, :longitude => -84.453789)
senior = officials[:senior_senator]
junior = officials[:junior_senator]
rep = officials[:representative]

Sunlight::Legislator.all_for(:address => "123 Fifth Ave New York, NY 10003")
Sunlight::Legislator.all_for(:address => "90210") # it'll work, but use all_in_zip instead

66
67
68
69
70
71
72
73
74
75
76
# File 'lib/sunlight/legislator.rb', line 66

def self.all_for(params)

  if (params[:latitude] and params[:longitude])
    Legislator.all_in_district(District.get(:latitude => params[:latitude], :longitude => params[:longitude]))
  elsif (params[:address])
    Legislator.all_in_district(District.get(:address => params[:address]))
  else
    nil # appropriate params not found
  end

end

.all_in_district(district) ⇒ Object

A helper method for all_for. Use that instead, unless you already have the district object, then use this.

Usage:

officials = Sunlight::Legislator.all_in_district(District.new("NJ", "7"))

87
88
89
90
91
92
93
94
95
# File 'lib/sunlight/legislator.rb', line 87

def self.all_in_district(district)

  senior_senator = Legislator.all_where(:state => district.state, :district => "Senior Seat").first
  junior_senator = Legislator.all_where(:state => district.state, :district => "Junior Seat").first
  representative = Legislator.all_where(:state => district.state, :district => district.number).first

  {:senior_senator => senior_senator, :junior_senator => junior_senator, :representative => representative}

end

.all_in_zipcode(zipcode) ⇒ Object

When you only have a zipcode (and could not get address from the user), use this. It specifically accounts for the case where more than one Representative's district is in a zip code.

If possible, ask for full address for proper geocoding via Legislator#all_for, which gives you a nice hash.

Returns:

An array of Legislator objects

Usage:

legislators = Sunlight::Legislator.all_in_zipcode(90210)

137
138
139
140
141
142
143
# File 'lib/sunlight/legislator.rb', line 137

def self.all_in_zipcode(zipcode)

  url = construct_url("legislators.allForZip", {:zip => zipcode})

  legislators_from_url(url)

end

.all_where(params) ⇒ Object

A more general, open-ended search on Legislators than #all_for. See the Sunlight API for list of conditions and values:

services.sunlightlabs.com/api/docs/legislators/

Returns:

An array of Legislator objects that matches the conditions

Usage:

johns = Sunlight::Legislator.all_where(:firstname => "John")
floridians = Sunlight::Legislator.all_where(:state => "FL")
dudes = Sunlight::Legislator.all_where(:gender => "M")

114
115
116
117
118
119
# File 'lib/sunlight/legislator.rb', line 114

def self.all_where(params)

  url = construct_url("legislators.getList", params)

  legislators_from_url(url)
end

.legislators_from_url(url) ⇒ Object


146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/sunlight/legislator.rb', line 146

def self.legislators_from_url(url)
  if (result = get_json_data(url))

    legislators = []
    result["response"]["legislators"].each do |legislator|
      legislators << Legislator.new(legislator["legislator"])
    end

    legislators

  else  
    nil
  end # if response.class
end

.search_by_name(name, threshold = '0.8') ⇒ Object

Fuzzy name searching. Returns possible matching Legislators along with a confidence score. Confidence scores below 0.8 mean the Legislator should not be used.

The API documentation explains it best:

wiki.sunlightlabs.com/index.php/Legislators.search

Returns:

An array of Legislators, with the fuzzy_score set as an attribute

Usage:

legislators = Sunlight::Legislator.search_by_name("Teddy Kennedey")
legislators = Sunlight::Legislator.search_by_name("Johnny Kerry", 0.9)

178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/sunlight/legislator.rb', line 178

def self.search_by_name(name, threshold='0.8')
  
  url = construct_url("legislators.search", {:name => name, :threshold => threshold})
  
  if (response = get_json_data(url))
    
    legislators = []
    response["response"]["results"].each do |result|
      if result
        legislator = Legislator.new(result["result"]["legislator"])
        fuzzy_score = result["result"]["score"]
        
        if threshold.to_f < fuzzy_score.to_f
          legislator.fuzzy_score = fuzzy_score.to_f
          legislators << legislator
        end
      end
    end
    
    if legislators.empty?
      nil
    else
      legislators 
    end
    
  else
    nil
  end
  
end

Instance Method Details

#committeesObject

Get the committees the Legislator sits on

Returns:

An array of Committee objects, each possibly having its own subarray of subcommittees


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

def committees
  url = Sunlight::Base.construct_url("committees.allForLegislator", {:bioguide_id => self.bioguide_id})

   if (result = Sunlight::Base.get_json_data(url))
     committees = []
     result["response"]["committees"].each do |committee|
       committees << Sunlight::Committee.new(committee["committee"])
     end
   else
     nil # appropriate params not found
   end
   committees
end

#youtube_idObject

Convenience method for getting out the youtube_id from the youtube_url


22
23
24
# File 'lib/sunlight/legislator.rb', line 22

def youtube_id
  /http:\/\/(?:www\.)?youtube\.com\/(?:user\/)?(.*?)\/?$/.match(youtube_url)[1] unless youtube_url.nil?
end