rughetto-sunlight 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGES.textile ADDED
@@ -0,0 +1,3 @@
1
+ h3. 0.1.0 / 2008-08-20
2
+
3
+ * Initial version
data/README.textile ADDED
@@ -0,0 +1,135 @@
1
+ h1. Sunlight Labs API Gem
2
+
3
+ NOTE: This file is formatted in Textile and best viewed on "GitHub":http://github.com/rughetto/sunlight.
4
+
5
+ h2. Description
6
+
7
+ A little gem that integrates with the Sunlight Labs API, originally build by "luigi":http://github.com/luigi. From the "official Sunlight site":http://services.sunlightlabs.com/api/:
8
+
9
+ bq. The Sunlight Labs API provides methods for obtaining basic information on Members of Congress, legislator IDs used by various websites, and lookups between places and the politicians that represent them. The primary purpose of the API is to facilitate mashups involving politicians and the various other APIs that are out there.
10
+
11
+ Full API documentation available "here":http://services.sunlightlabs.com/api/docs/.
12
+
13
+ Also, the Sunlight gem integrates with the Google Maps API for "street address geocoding":http://code.google.com/apis/maps/documentation/services.html#Geocoding. Because some zip codes overlap two or more congressional districts, passing in a latitude/longitude will give users the most accurate information. Since it's highly unlikely a user would know their exact lat/long, the Google Maps API is used to translate a street address string into a lat/long pair.
14
+
15
+ This version of the library has been grabbed nearly verbatim from "github.com/luigi/sunlight":http://github.com/luigi/sunlight. Changes were made to make this work more reliably in Merb. In particular the base class SunlightObject was changed to include methods for the constants and accessors originally held in the Sunlight module. Now instead of including the module in another class, you can call the Sunlight::Legislator and Sunlight::District objects directly.
16
+
17
+ h2. Installation
18
+
19
+ The following gems are required:
20
+
21
+ * json
22
+ * ym4r
23
+
24
+ @$ sudo gem install json ym4r@
25
+
26
+ The latest development version can be installed using:
27
+
28
+ @$ sudo gem install rughetto-sunlight --source=http://gems.github.com@
29
+
30
+ To install the stable gem, go to "github.com/luigi/sunlight":http://github.com/luigi/sunlight.
31
+
32
+ h2. Set Up
33
+
34
+ First, register for an API key "here":http://services.sunlightlabs.com/api/register/.
35
+
36
+ Then, you'll want to stick the following lines somewhere in your Ruby environment.
37
+
38
+ <pre><code>
39
+ require 'rubygems'
40
+ require 'sunlight'
41
+ Sunlight::SunlightObject.api_key = 'yourapikeyfromtheurlabove'
42
+ # or it will read a file in your config directory called sunlight.api
43
+ # each object can be initialized to a different key by calling the object's class method #api_key=
44
+ </code></pre>
45
+
46
+ This library has been changed to use either Rails or Merb. For Rails, add the gem requirement in your particular environment file, and the Sunlight::Legislator and Sunlight::District classes will be available throughout the application.
47
+
48
+ If using merb, add the dependency requirement to your dependency.rb file:
49
+ <pre><code>
50
+ dependency "rughetto-sunlight", :require_as => "sunlight"
51
+ </code></pre>
52
+ The gem will be automatically packaged into your application when thor merb:gem:install in run next.
53
+
54
+ h2. Usage
55
+
56
+ Now, it's time to get to the good stuff. The most useful method is @Legislator#all_for@:
57
+
58
+ <pre><code>
59
+ congresspeople = Legislator.all_for(:address => "123 Fifth Ave New York, NY 10003")
60
+ senior_senator = congresspeople[:senior_senator]
61
+ junior_senator = congresspeople[:junior_senator]
62
+ representative = congresspeople[:representative]
63
+
64
+ junior_senator.firstname # returns "Hillary"
65
+ junior_senator.lastname # returns "Clinton"
66
+ junior_senator.congress_office # returns "476 Russell Senate Office Building"
67
+ junior_senator.phone # returns "202-224-4451"
68
+ </code></pre>
69
+
70
+ Note that you should make the best attempt to get a full street address, as that is geocoded behind the scenes into a lat/long pair. A five-digit zip code alone won't cut it, as there may be multiple congressional districts in a single zip code (there are ways to deal with this below). If you pass in a zip+4, then you'll get much better geocoding results.
71
+
72
+ So @Legislator#all_for@ returns a hash of @Legislator@ objects, and the keys are @:senior_senator@, @:junior_senator@, and @:representative@. Make sure to review all the available fields from the "Sunlight Labs API":http://services.sunlightlabs.com/api/docs/legislators/. You can also pass in a lat/long pair:
73
+
74
+ <pre><code>
75
+ congresspeople = Legislator.all_for(:latitude => 33.876145, :longitude => -84.453789)
76
+ </code></pre>
77
+
78
+ This bypasses the geocoding necessary by the Google Maps API. For social networks and other applications with a User object, it makes sense to geocode the user's address up front and save the lat/long data in the local database. Then, use the lat/long pair instead of address, which cuts a substantial bit of time from the @Legislator#all_for@ request since the Google Maps API Geocoding function doesn't have to be called.
79
+
80
+ You can also use the @Legislator#all_where@ method for searching based on available fields. You'll get back an array of @Legislator@ objects:
81
+
82
+ <pre><code>
83
+ johns = Legislator.all_where(:firstname => "John")
84
+ floridians = Legislator.all_where(:state => "FL")
85
+ dudes = Legislator.all_where(:gender => "M")
86
+
87
+ johns.each do |john|
88
+ # do stuff
89
+ end
90
+ </code></pre>
91
+
92
+ There's also the @District@ object. @District#get@ takes in either lat/long or an address and does it's best to return the correct Congressional District:
93
+
94
+ <pre><code>
95
+ district = District.get(:latitude => 33.876145, :longitude => -84.453789)
96
+ district.state # returns "GA"
97
+ district.number # returns "6"
98
+
99
+ district = District.get(:address => "123 Fifth Ave New York, NY")
100
+ </code></pre>
101
+
102
+ Finally, two more methods, @District.all_from_zipcode@ and @District.zipcodes_in@, help you out when all you have is a five-digit zip code and want to make sure you account for all the districts in a zip code, or if you want to get back all zipcodes in a given district.
103
+
104
+ <pre><code>
105
+ districts = District.all_from_zipcode(90210) # returns array of District objects
106
+ zipcodes = District.zipcodes_in("NY", "10") # returns array of zip codes as strings ["11201", "11202", "11203",...]
107
+ </code></pre>
108
+
109
+
110
+ h2. License
111
+
112
+ See the terms of usage for the "Sunlight Labs API":http://services.sunlightlabs.com/api/ and the "Google Maps API":http://code.google.com/apis/maps/terms.html.
113
+
114
+ Copyright &copy; 2008 by Rue the Ghetto under the MIT License.
115
+
116
+ Permission is hereby granted, free of charge, to any person
117
+ obtaining a copy of this software and associated documentation
118
+ files (the "Software"), to deal in the Software without
119
+ restriction, including without limitation the rights to use,
120
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
121
+ copies of the Software, and to permit persons to whom the
122
+ Software is furnished to do so, subject to the following
123
+ conditions:
124
+
125
+ The above copyright notice and this permission notice shall be
126
+ included in all copies or substantial portions of the Software.
127
+
128
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
129
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
130
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
131
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
132
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
133
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
134
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
135
+ OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,108 @@
1
+ module Sunlight
2
+
3
+ class District < SunlightObject
4
+
5
+ attr_accessor :state, :number
6
+
7
+ def initialize(state, number)
8
+ @state = state
9
+ @number = number
10
+ end
11
+
12
+
13
+ # Usage:
14
+ # District.get(:latitude => 33.876145, :longitude => -84.453789) # returns one District object or nil
15
+ # District.get(:address => "123 Fifth Ave New York, NY") # returns one District object or nil
16
+ #
17
+ def self.get(params)
18
+
19
+ if (params[:latitude] and params[:longitude])
20
+
21
+ get_from_lat_long(params[:latitude], params[:longitude])
22
+
23
+ elsif (params[:address])
24
+
25
+ # get the lat/long from Google
26
+ placemarks = Geocoding::get(params[:address])
27
+
28
+ unless placemarks.empty?
29
+ placemark = placemarks[0]
30
+ get_from_lat_long(placemark.latitude, placemark.longitude)
31
+ end
32
+
33
+ else
34
+ nil # appropriate params not found
35
+ end
36
+
37
+ end
38
+
39
+
40
+
41
+ # Usage:
42
+ # District.get_from_lat_long(-123, 123) # returns District object or nil
43
+ #
44
+ def self.get_from_lat_long(latitude, longitude)
45
+
46
+ url = construct_url("districts.getDistrictFromLatLong", {:latitude => latitude, :longitude => longitude})
47
+
48
+ if (result = get_json_data(url))
49
+
50
+ districts = []
51
+ result["response"]["districts"].each do |district|
52
+ districts << District.new(district["district"]["state"], district["district"]["number"])
53
+ end
54
+
55
+ districts.first
56
+
57
+ else
58
+ nil
59
+ end # if response.class
60
+
61
+ end
62
+
63
+
64
+
65
+ # Usage:
66
+ # District.all_from_zipcode(90210) # returns array of District objects
67
+ #
68
+ def self.all_from_zipcode(zipcode)
69
+
70
+ url = construct_url("districts.getDistrictsFromZip", {:zip => zipcode})
71
+
72
+ if (result = get_json_data(url))
73
+
74
+ districts = []
75
+ result["response"]["districts"].each do |district|
76
+ districts << District.new(district["district"]["state"], district["district"]["number"])
77
+ end
78
+
79
+ districts
80
+
81
+ else
82
+ nil
83
+ end # if response.class
84
+
85
+ end
86
+
87
+
88
+
89
+ # Usage:
90
+ # District.zipcodes_in("NY", 29) # returns ["14009", "14024", "14029", ...]
91
+ #
92
+ def self.zipcodes_in(state, number)
93
+
94
+ url = construct_url("districts.getZipsFromDistrict", {:state => state, :district => number})
95
+
96
+ if (result = get_json_data(url))
97
+ result["response"]["zips"]
98
+ else
99
+ nil
100
+ end # if response.class
101
+
102
+ end
103
+
104
+
105
+
106
+ end # class District
107
+
108
+ end # module Sunlight
@@ -0,0 +1,110 @@
1
+ module Sunlight
2
+ class Legislator < SunlightObject
3
+
4
+ attr_accessor :title, :firstname, :middlename, :lastname, :name_suffix, :nickname,
5
+ :party, :state, :district, :gender, :phone, :fax, :website, :webform,
6
+ :email, :congress_office, :bioguide_id, :votesmart_id, :fec_id,
7
+ :govtrack_id, :crp_id, :event_id, :congresspedia_url, :youtube_url,
8
+ :twitter_id
9
+
10
+ # Takes in a hash where the keys are strings (the format passed in by the JSON parser)
11
+ #
12
+ def initialize(params)
13
+ params.each do |key, value|
14
+ instance_variable_set("@#{key}", value) if Legislator.instance_methods.include? key
15
+ end
16
+ end
17
+
18
+
19
+
20
+ #
21
+ # Useful for getting the exact Legislators for a given district.
22
+ #
23
+ # Returns:
24
+ #
25
+ # A Hash of the three Members of Congress for a given District: Two
26
+ # Senators and one Representative.
27
+ #
28
+ # You can pass in lat/long or address. The district will be
29
+ # determined for you:
30
+ #
31
+ # officials = Legislator.all_for(:latitude => 33.876145, :longitude => -84.453789)
32
+ # senior = officials[:senior_senator]
33
+ # junior = officials[:junior_senator]
34
+ # rep = officials[:representative]
35
+ #
36
+ # Legislator.all_for(:address => "123 Fifth Ave New York, NY 10003")
37
+ # Legislator.all_for(:address => "90210") # not recommended, but it'll work
38
+ #
39
+ def self.all_for(params)
40
+
41
+ if (params[:latitude] and params[:longitude])
42
+ Legislator.all_in_district(District.get(:latitude => params[:latitude], :longitude => params[:longitude]))
43
+ elsif (params[:address])
44
+ Legislator.all_in_district(District.get(:address => params[:address]))
45
+ else
46
+ nil # appropriate params not found
47
+ end
48
+
49
+ end
50
+
51
+
52
+ #
53
+ # A helper method for all_for. Use that instead, unless you
54
+ # already have the district object, then use this.
55
+ #
56
+ # Usage:
57
+ #
58
+ # officials = Legislator.all_in_district(District.new("NJ", "7"))
59
+ #
60
+ def self.all_in_district(district)
61
+
62
+ senior_senator = Legislator.all_where(:state => district.state, :district => "Senior Seat").first
63
+ junior_senator = Legislator.all_where(:state => district.state, :district => "Junior Seat").first
64
+ representative = Legislator.all_where(:state => district.state, :district => district.number).first
65
+
66
+ {:senior_senator => senior_senator, :junior_senator => junior_senator, :representative => representative}
67
+
68
+ end
69
+
70
+
71
+ #
72
+ # A more general, open-ended search on Legislators than #all_for.
73
+ # See the Sunlight API for list of conditions and values:
74
+ #
75
+ # http://services.sunlightlabs.com/api/docs/legislators/
76
+ #
77
+ # Returns:
78
+ #
79
+ # An array of Legislator objects that matches the conditions
80
+ #
81
+ # Usage:
82
+ #
83
+ # johns = Legislator.all_where(:firstname => "John")
84
+ # floridians = Legislator.all_where(:state => "FL")
85
+ # dudes = Legislator.all_where(:gender => "M")
86
+ #
87
+ def self.all_where(params)
88
+
89
+ url = construct_url("legislators.getList", params)
90
+
91
+ if (result = get_json_data(url))
92
+
93
+ legislators = []
94
+ result["response"]["legislators"].each do |legislator|
95
+ legislators << Legislator.new(legislator["legislator"])
96
+ end
97
+
98
+ legislators
99
+
100
+ else
101
+ nil
102
+ end # if response.class
103
+
104
+ end
105
+
106
+
107
+
108
+ end # class Legislator
109
+
110
+ end # module Sunlight
@@ -0,0 +1,60 @@
1
+ module Sunlight
2
+ # Houses general methods to work with the Sunlight and Google Maps APIs
3
+ class SunlightObject
4
+ def self.api_url
5
+ "http://services.sunlightlabs.com/api/"
6
+ end
7
+
8
+ def self.api_format
9
+ "json"
10
+ end
11
+
12
+ def self.api_key
13
+ @api_key ||= get_key
14
+ end
15
+
16
+ def self.api_key=(key)
17
+ @api_key = key
18
+ end
19
+
20
+ def self.get_key
21
+ if self == Sunlight::SunlightObject
22
+ File.read( "#{app_root}/config/sunlight.api" )
23
+ else
24
+ Sunlight::SunlightObject.api_key
25
+ end
26
+ end
27
+
28
+ def self.app_root
29
+ defined?(RAILS_ROOT) ? RAILS_ROOT : Merb.root
30
+ end
31
+
32
+ # Constructs a Sunlight API-friendly URL
33
+ def self.construct_url(api_method, params)
34
+ "#{api_url}#{api_method}.#{api_format}?apikey=#{api_key}#{hash2get(params)}"
35
+ end
36
+
37
+ # Converts a hash to a GET string
38
+ def self.hash2get(h)
39
+ get_string = ""
40
+ h.each_pair do |key, value|
41
+ get_string += "&#{key.to_s}=#{CGI::escape(value.to_s)}"
42
+ end
43
+ get_string
44
+ end # def hash2get
45
+
46
+
47
+ # Use the Net::HTTP and JSON libraries to make the API call
48
+ #
49
+ # Usage:
50
+ # District.get_json_data("http://someurl.com") # returns Hash of data or nil
51
+ def self.get_json_data(url)
52
+ response = Net::HTTP.get_response(URI.parse(url))
53
+ if response.class == Net::HTTPOK
54
+ result = JSON.parse(response.body)
55
+ else
56
+ nil
57
+ end
58
+ end # self.get_json_data
59
+ end # class SunlightObject
60
+ end # module Sunlight
data/lib/sunlight.rb ADDED
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'json'
3
+ require 'cgi'
4
+ require 'ym4r/google_maps/geocoding'
5
+ require 'net/http'
6
+ include Ym4r::GoogleMaps
7
+
8
+ require File.dirname(__FILE__) + '/sunlight/sunlight_object'
9
+ require File.dirname(__FILE__) + '/sunlight/district'
10
+ require File.dirname(__FILE__) + '/sunlight/legislator'
data/sunlight.gemspec ADDED
@@ -0,0 +1,20 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "sunlight"
3
+ s.version = "0.1.2"
4
+ s.date = "2009-01-28"
5
+ s.summary = "Library for accessing the Sunlight Labs API."
6
+ s.email = "ru_ghetto@rubyghetto.com"
7
+ s.homepage = "http://github.com/rughetto/sunlight"
8
+ s.authors = ["Luigi Montanez", "Rue the Ghetto"]
9
+ s.files = [
10
+ 'sunlight.gemspec',
11
+ 'lib/sunlight.rb',
12
+ 'lib/sunlight/sunlight_object.rb',
13
+ 'lib/sunlight/district.rb',
14
+ 'lib/sunlight/legislator.rb',
15
+ 'README.textile',
16
+ 'CHANGES.textile']
17
+ s.add_dependency("json", [">= 1.1.3"])
18
+ s.add_dependency("ym4r", [">= 0.6.1"])
19
+ s.has_rdoc = true
20
+ end
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rughetto-sunlight
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ platform: ruby
6
+ authors:
7
+ - Luigi Montanez
8
+ - Rue the Ghetto
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2009-01-28 00:00:00 -08:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: json
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.1.3
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: ym4r
27
+ version_requirement:
28
+ version_requirements: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 0.6.1
33
+ version:
34
+ description:
35
+ email: ru_ghetto@rubyghetto.com
36
+ executables: []
37
+
38
+ extensions: []
39
+
40
+ extra_rdoc_files: []
41
+
42
+ files:
43
+ - sunlight.gemspec
44
+ - lib/sunlight.rb
45
+ - lib/sunlight/sunlight_object.rb
46
+ - lib/sunlight/district.rb
47
+ - lib/sunlight/legislator.rb
48
+ - README.textile
49
+ - CHANGES.textile
50
+ has_rdoc: true
51
+ homepage: http://github.com/rughetto/sunlight
52
+ post_install_message:
53
+ rdoc_options: []
54
+
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: "0"
62
+ version:
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: "0"
68
+ version:
69
+ requirements: []
70
+
71
+ rubyforge_project:
72
+ rubygems_version: 1.2.0
73
+ signing_key:
74
+ specification_version: 2
75
+ summary: Library for accessing the Sunlight Labs API.
76
+ test_files: []
77
+