Class: SunlightCongress

Inherits:
Object
  • Object
show all
Defined in:
lib/sunlightcongress.rb

Instance Method Summary collapse

Constructor Details

#initialize(apikey) ⇒ SunlightCongress

Returns a new instance of SunlightCongress.



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

def initialize(apikey)
  @apikey = apikey
end

Instance Method Details

#get_amendments(name) ⇒ Object

Get all amendments sponsored by a congressperson



33
34
35
36
37
38
# File 'lib/sunlightcongress.rb', line 33

def get_amendments(name)
  id = legislator_id(name)
  options = {:query => {:apikey => @apikey} }
  data = HTTParty.get("http://congress.api.sunlightfoundation.com/amendments?sponsor_type=person&sponsor_id=" + id.to_s , options)["results"]
  return data.to_json
end

#get_bills(name) ⇒ Object

Get all bills sponsored by a congressperson



41
42
43
44
45
46
# File 'lib/sunlightcongress.rb', line 41

def get_bills(name)
  id = legislator_id(name)
  options = {:query => {:apikey => @apikey} }
  data = HTTParty.get("http://congress.api.sunlightfoundation.com/bills?sponsor_id=" + id.to_s , options)["results"]
  return data.to_json
end

#get_committees(name) ⇒ Object

Get all committees a congressperson is on



57
58
59
60
61
62
# File 'lib/sunlightcongress.rb', line 57

def get_committees(name)
  id = legislator_id(name)
  options = {:query => {:apikey => @apikey} }
  data = HTTParty.get("http://congress.api.sunlightfoundation.com/committees?member_ids=" + id.to_s , options)["results"]
  return data.to_json
end

#get_events(name) ⇒ Object

Get all events (hearings, votes, bills, amendments, floor updates) for a congressperson and output JSON



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/sunlightcongress.rb', line 85

def get_events(name)
  id = legislator_id(name)

  # Get votes
  votes = JSON.parse(get_votes(id))
  votearray = Array.new
  votes.each do |v|
    savehash = Hash.new
    vhash = Hash[*v.flatten]

    savehash["start time"] = vhash["voted_at"].to_s
    savehash["end time"] = nil
    savehash["headline"] = "Vote(" + id + "): " + vhash["question"].to_s
    savehash["text"] = "roll_type: " + vhash["roll_type"].to_s + " result: " + vhash["result"].to_s + " vote_type: " + vhash["vote_type"].to_s + " url: " + vhash["url"].to_s

    votearray.push(savehash)
  end
  
  # Get amendments
  amendments = JSON.parse(get_amendments(id))
  amendmentarray = Array.new
  amendments.each do |a|
    savehash = Hash.new
    ahash = Hash[*a.flatten]
    
    savehash["start time"] = ahash["introduced_on"].to_s
    savehash["end time"] = ahash["last_action_at"].to_s
    savehash["headline"] = "Amendment(" + id + "): " + ahash["purpose"].to_s
    savehash["text"] = "description: " + ahash["description"].to_s + " amends_bill_id: " + ahash["amends_bill_id"].to_s

    amendmentarray.push(savehash)
  end

  # Get bills
  bills = JSON.parse(get_bills(id))
  billarray = Array.new
  bills.each do |b|
    savehash = Hash.new
    bhash = Hash[*b.flatten]

    savehash["start time"] = bhash["introduced_on"].to_s
    savehash["end time"] = bhash["last_vote_at"].to_s
    savehash["headline"] = "Bill(" + id + "): " + bhash["short_title"].to_s
    savehash["text"] = "official_title: " + bhash["official_title"].to_s + " bill_id: " + bhash["bill_id"].to_s

    billarray.push(savehash)
  end
  
  # Get updates
  updates = JSON.parse(get_updates(id))
  updatearray = Array.new
  updates.each do |u|
    savehash = Hash.new
    uhash = Hash[*u.flatten]

    savehash["start time"] = uhash["timestamp"].to_s
    savehash["end time"] = nil
    savehash["headline"] = "Update(" + id + ")"
    savehash["text"] = "update: " + uhash["update"].to_s

    updatearray.push(savehash)
  end
  
  # Get hearings
  hearings = JSON.parse(get_hearings_json(get_committees(id)))
  hearingarray = Array.new
  hearings.each do |h|
    savehash = Hash.new
    hhash = Hash[*h.flatten]
    
    savehash["start time"] = hhash["occurs_at"].to_s
    savehash["end time"] = nil
    savehash["headline"] = "Committee Hearing(" + id + "): " + hhash["description"].to_s
    savehash["text"] = "committee_id: " + hhash["committee_id"].to_s + " url: " + hhash["url"].to_s
      
    hearingarray.push(savehash)
  end

  combinedata = votearray + amendmentarray + billarray + updatearray + hearingarray
  combinedata.to_json
end

#get_hearings(cid) ⇒ Object

Get all hearings for a committee



65
66
67
68
69
# File 'lib/sunlightcongress.rb', line 65

def get_hearings(cid)
  options = {:query => {:apikey => @apikey} }
  data = HTTParty.get("http://congress.api.sunlightfoundation.com/hearings?committee_id=" + cid.to_s , options)["results"]
  return data.to_json
end

#get_hearings_json(json_input) ⇒ Object

Get hearings for a particular committee (JSON input)



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/sunlightcongress.rb', line 72

def get_hearings_json(json_input)
  jinput = JSON.parse(json_input)
  savedata = Array.new

  jinput.each do |l|
    jhash = Hash[*l.flatten]
    cid = jhash["committee_id"]
    savedata = savedata + JSON.parse(get_hearings(cid))
  end
  return savedata.to_json
end

#get_updates(name) ⇒ Object

Get all floor updates that mention a congressperson



49
50
51
52
53
54
# File 'lib/sunlightcongress.rb', line 49

def get_updates(name)
  id = legislator_id(name)
  options = {:query => {:apikey => @apikey} }
  data = HTTParty.get("http://congress.api.sunlightfoundation.com/floor_updates?legislator_ids=" + id.to_s , options)["results"]
  return data.to_json
end

#get_votes(name) ⇒ Object

Get all votes by particular congressperson



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

def get_votes(name)
  id = legislator_id(name)
  options = {:query => {:apikey => @apikey} }
  data = HTTParty.get("http://congress.api.sunlightfoundation.com/votes?voter_ids." + id.to_s + "__exists=true", options)["results"]
  return data.to_json
end

#legislator_id(name) ⇒ Object

Get legislator ID



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/sunlightcongress.rb', line 10

def legislator_id(name)
  options = {:query => {:apikey => @apikey} }
  namearray = name.split(" ")
  data = HTTParty.get("http://congress.api.sunlightfoundation.com/legislators?query="+namearray.last, options)["results"]

  data.each do |l| 
    dhash = Hash[*l.flatten]
    if data.length > 1
      return dhash["bioguide_id"] if dhash["first_name"] == namearray.first
    else return dhash["bioguide_id"]
    end
  end
end