sunlight-api 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +66 -0
- data/Rakefile +2 -0
- data/lib/sunlight-api.rb +3 -0
- data/lib/sunlight/base.rb +7 -0
- data/lib/sunlight/congress.rb +7 -0
- data/lib/sunlight/congress/legislator.rb +90 -0
- data/lib/sunlight/version.rb +3 -0
- data/sunlight-api.gemspec +24 -0
- metadata +97 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 56f8123b9b8d42b35e0b22031b693ec15100c00c
|
4
|
+
data.tar.gz: 20db42fec5c5cbe1d01ca65416f28a390a9097a7
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1f0e60b00a35da1c664aff76371064b8a8ea1feed0deb21023e87d5732860206a920cd2b646e5052a54530689540d19c22c647d1a5d96114ba2c6a6f9346d173
|
7
|
+
data.tar.gz: b3a02752ab6cc2aab829ef4fd7221f8fe02fe49fb831c370bc1d7778a3881be02ec95cc02078defba984783b27234f5ca6c83a9dfb9109ee1df491a433fdb09e
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Kyle Moore
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
# Sunlight Api
|
2
|
+
|
3
|
+
The [Sunlight Foundation](http://sunlightfoundation.com/about/) is a nonprofit, nonpartisan group that "advocates for open government globally and uses technology to make government more accountable to all." They provide multiple great APIs for political data, and this gem provides a **clean, simple wrapper for the Sunlight Foundation APIs**. It's currently a work in progress, and will be updated frequently to support more of their APIs.
|
4
|
+
|
5
|
+
## Usage
|
6
|
+
|
7
|
+
#### Congress API
|
8
|
+
##### Legislators
|
9
|
+
|
10
|
+
To find the representatives for a certain location
|
11
|
+
```ruby
|
12
|
+
Sunlight::Congress::Legislator.locate_by_zip(23219)
|
13
|
+
Sunlight::Congress::Legislator.locate(zip: 23219)
|
14
|
+
Sunlight::Congress::Legislator.locate(latitude: 37.538821, longitude: -77.433574)
|
15
|
+
Sunlight::Congress::Legislator.locate(address: '1000 Bank St, Richmond, VA')
|
16
|
+
Sunlight::Congress::Legislator.locate('37.538821, -77.433574') # Not recommended
|
17
|
+
Sunlight::Congress::Legislator.locate('1000 Bank St, Richmond, VA')
|
18
|
+
```
|
19
|
+
A zip code may intersect with multiple congressional districts, so to find all the legislators in a certain zip code, use the ```locate_by_zip``` method. The ```locate``` method can take parameters as either a string or hash of options (recommended for latitude/longitude or zip codes). All string queries will be geocoded into lat/long pairs.
|
20
|
+
|
21
|
+
To find a specific representative, you have multiple options for searching
|
22
|
+
```ruby
|
23
|
+
Sunlight::Congress::Legislator.find('Warner') # Searches on the first, middle, last, and nickname fields
|
24
|
+
Sunlight::Congress::Legislator.find(gender: 'F', party: 'R') # All current female, republican legislators
|
25
|
+
Sunlight::Congress::Legislator.find(gender: 'F', party: 'R', all_legislators: true) # All past and present female, republican legislators
|
26
|
+
Sunlight::Congress::Legislator.find_by_state_rank('senior') # All legislators with a senior rank
|
27
|
+
```
|
28
|
+
The ```find``` method accepts either a string or hash of parameters. Strings will only query the first, middle, last, and nicknames of legislators. You can also ```find_by_``` certain attributes. To see a list of accepted attributes (also applies to parameters passed into the ```find``` method), [look here](https://sunlightlabs.github.io/congress/legislators.html)
|
29
|
+
|
30
|
+
Legislator location and searching methods return an array of ```Legislator``` objects
|
31
|
+
```ruby
|
32
|
+
legislators = Sunlight::Congress::Legislator.find(gender: 'F', party: 'R')
|
33
|
+
legislators #=> [#<Sunlight::Congress::Legislator:0x007f8fb7834b88 @bioguide_id="E000295", @birthday="1970-07-01", @chamber="senate"...>, #<Sunlight::Congress::Legislator:0x007f8fb58b15e0 @bioguide_id="C001105", @birthday="1959-06-30", @chamber="house"...>, ...]
|
34
|
+
legislators[0].office #=> "825 B&c Hart Senate Office Building"
|
35
|
+
```
|
36
|
+
|
37
|
+
|
38
|
+
## Installation
|
39
|
+
|
40
|
+
Add this line to your application's Gemfile:
|
41
|
+
|
42
|
+
```ruby
|
43
|
+
gem 'sunlight-api'
|
44
|
+
```
|
45
|
+
|
46
|
+
And then execute:
|
47
|
+
|
48
|
+
$ bundle
|
49
|
+
|
50
|
+
Or install it yourself as:
|
51
|
+
|
52
|
+
$ gem install sunlight-api
|
53
|
+
|
54
|
+
Get [your API key](http://sunlightfoundation.com/api/accounts/register/) and run:
|
55
|
+
|
56
|
+
```ruby
|
57
|
+
Sunlight::Base.api_key = 'your_api_key'
|
58
|
+
```
|
59
|
+
|
60
|
+
## Contributing
|
61
|
+
|
62
|
+
1. Fork it ( https://github.com/kylerm42/sunlight-api/fork )
|
63
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
64
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
65
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
66
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/lib/sunlight-api.rb
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
require 'geocoder'
|
2
|
+
|
3
|
+
module Sunlight
|
4
|
+
class Congress
|
5
|
+
class Legislator
|
6
|
+
|
7
|
+
SEARCH_ATTRIBUTES = [:first_name, :middle_name, :last_name, :name_suffix,
|
8
|
+
:nickname, :state, :state_name, :state_rank, :party,
|
9
|
+
:chamber, :title, :senate_class, :district, :term_start,
|
10
|
+
:term_end, :birthday, :fax, :in_office, :bioguide_id,
|
11
|
+
:crp_id, :govtrack_id, :icpsr_id, :fec_ids, :lis_id,
|
12
|
+
:ocd_id, :thomas_id, :votesmart_id, :oc_email]
|
13
|
+
|
14
|
+
attr_accessor *SEARCH_ATTRIBUTES.concat([:phone, :website, :office, :contact_form,
|
15
|
+
:fax, :twitter_id, :youtube_id, :facebook_id])
|
16
|
+
|
17
|
+
def initialize(options = {})
|
18
|
+
options.each do |attribute, value|
|
19
|
+
instance_variable_set("@#{attribute}", value)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
class << self
|
24
|
+
def locate_by_zip(zip)
|
25
|
+
response = Net::HTTP.get_response(locate_uri(zip: zip))
|
26
|
+
|
27
|
+
JSON.parse(response.body)['results'].map do |legislator_attrs|
|
28
|
+
self.new(legislator_attrs)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def locate(options)
|
33
|
+
if options.is_a?(String) || options[:address]
|
34
|
+
coords = Geocoder.coordinates(options)
|
35
|
+
response = Net::HTTP.get_response(locate_uri(latitude: coords[0], longitude: coords[1]))
|
36
|
+
else
|
37
|
+
response = Net::HTTP.get_response(locate_uri(options))
|
38
|
+
end
|
39
|
+
|
40
|
+
create_from_response(response)
|
41
|
+
end
|
42
|
+
|
43
|
+
def locate_uri(options = {})
|
44
|
+
encode_uri('legislators/locate', options)
|
45
|
+
end
|
46
|
+
|
47
|
+
def find(query)
|
48
|
+
if query.is_a?(String)
|
49
|
+
response = Net::HTTP.get_response(find_uri(query: query))
|
50
|
+
else
|
51
|
+
response = Net::HTTP.get_response(find_uri(query))
|
52
|
+
end
|
53
|
+
|
54
|
+
create_from_response(response)
|
55
|
+
end
|
56
|
+
|
57
|
+
SEARCH_ATTRIBUTES.each do |param|
|
58
|
+
define_method("find_by_#{param}") do |query|
|
59
|
+
response = Net::HTTP.get_response(find_uri("#{param}" => query))
|
60
|
+
|
61
|
+
create_from_response(response)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def find_uri(options = {})
|
66
|
+
encode_uri('legislators', options)
|
67
|
+
end
|
68
|
+
|
69
|
+
def encode_uri(location, options)
|
70
|
+
uri = URI("#{BASE_URI}/#{location}")
|
71
|
+
uri.query = URI.encode_www_form(options.merge(apikey: '6d4acf5a753c40e1824857bb12679d89'))
|
72
|
+
|
73
|
+
uri
|
74
|
+
end
|
75
|
+
|
76
|
+
def create_from_response(response)
|
77
|
+
json = JSON.parse(response.body)
|
78
|
+
|
79
|
+
if results = json['results']
|
80
|
+
results.map do |legislator_attrs|
|
81
|
+
self.new(legislator_attrs)
|
82
|
+
end
|
83
|
+
elsif errors = json['error']
|
84
|
+
json
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'sunlight/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "sunlight-api"
|
8
|
+
spec.version = Sunlight::VERSION
|
9
|
+
spec.authors = ["Kyle Moore"]
|
10
|
+
spec.email = ["kylerm42@gmail.com"]
|
11
|
+
spec.summary = %q{Gem for interacting with various Sunlight Foundation APIs}
|
12
|
+
spec.homepage = "https://github.com/kylerm42/sunlight-api"
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0")
|
16
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
21
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
22
|
+
|
23
|
+
spec.add_runtime_dependency 'geocoder'
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sunlight-api
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Kyle Moore
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-02-09 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: geocoder
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description:
|
56
|
+
email:
|
57
|
+
- kylerm42@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- Gemfile
|
64
|
+
- LICENSE.txt
|
65
|
+
- README.md
|
66
|
+
- Rakefile
|
67
|
+
- lib/sunlight-api.rb
|
68
|
+
- lib/sunlight/base.rb
|
69
|
+
- lib/sunlight/congress.rb
|
70
|
+
- lib/sunlight/congress/legislator.rb
|
71
|
+
- lib/sunlight/version.rb
|
72
|
+
- sunlight-api.gemspec
|
73
|
+
homepage: https://github.com/kylerm42/sunlight-api
|
74
|
+
licenses:
|
75
|
+
- MIT
|
76
|
+
metadata: {}
|
77
|
+
post_install_message:
|
78
|
+
rdoc_options: []
|
79
|
+
require_paths:
|
80
|
+
- lib
|
81
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
82
|
+
requirements:
|
83
|
+
- - ">="
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
requirements: []
|
92
|
+
rubyforge_project:
|
93
|
+
rubygems_version: 2.2.2
|
94
|
+
signing_key:
|
95
|
+
specification_version: 4
|
96
|
+
summary: Gem for interacting with various Sunlight Foundation APIs
|
97
|
+
test_files: []
|