Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Justin Palmer
committed
Jan 20, 2009
0 parents
commit 7aecd66
Showing
25 changed files
with
672 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.sunlight_api_key | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
== 0.1.0 2009-01-16 | ||
|
||
* 1 major enhancement: | ||
* Initial release |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
History.txt | ||
Manifest.txt | ||
PostInstall.txt | ||
README.rdoc | ||
Rakefile | ||
examples/basic.rb | ||
examples/districts.rb | ||
lib/sunshine.rb | ||
lib/sunshine/base.rb | ||
lib/sunshine/district.rb | ||
lib/sunshine/legislator.rb | ||
lib/sunshine/lobbyist.rb | ||
script/console | ||
script/destroy | ||
script/generate | ||
spec/district_spec.rb | ||
spec/fixtures/pelosi.json | ||
spec/fixtures/senators.json | ||
spec/legislator_spec.rb | ||
spec/spec.opts | ||
spec/spec_helper.rb | ||
spec/sunshine_spec.rb | ||
sunshine.gemspec | ||
tasks/rspec.rake |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
|
||
For more information on the Sunlight Labs API, see http://wiki.sunlightlabs.com/index.php?title=Sunlight_API_Documentation | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
= sunshine | ||
|
||
* FIX (url) | ||
|
||
== DESCRIPTION: | ||
|
||
FIX (describe your package) | ||
|
||
== FEATURES/PROBLEMS: | ||
|
||
* FIX (list of features or problems) | ||
|
||
== SYNOPSIS: | ||
|
||
FIX (code sample of usage) | ||
|
||
== REQUIREMENTS: | ||
|
||
* FIX (list of requirements) | ||
|
||
== INSTALL: | ||
|
||
* FIX (sudo gem install, anything else) | ||
|
||
== LICENSE: | ||
|
||
(The MIT License) | ||
|
||
Copyright (c) 2009 Justin Palmer | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining | ||
a copy of this software and associated documentation files (the | ||
'Software'), to deal in the Software without restriction, including | ||
without limitation the rights to use, copy, modify, merge, publish, | ||
distribute, sublicense, and/or sell copies of the Software, and to | ||
permit persons to whom the Software is furnished to do so, subject to | ||
the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be | ||
included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | ||
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY | ||
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, | ||
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | ||
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
%w[rubygems rake rake/clean fileutils newgem rubigen].each { |f| require f } | ||
require File.dirname(__FILE__) + '/lib/sunshine' | ||
|
||
# Generate all the Rake tasks | ||
# Run 'rake -T' to see list of generated tasks (from gem root directory) | ||
$hoe = Hoe.new('sunshine', Sunshine::VERSION) do |p| | ||
p.developer('Justin Palmer', 'FIXME email') | ||
p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n") | ||
p.post_install_message = 'PostInstall.txt' # TODO remove if post-install message not required | ||
p.rubyforge_name = p.name # TODO this is default value | ||
# p.extra_deps = [ | ||
# ['activesupport','>= 2.0.2'], | ||
# ] | ||
p.extra_dev_deps = [ | ||
['newgem', ">= #{::Newgem::VERSION}"] | ||
] | ||
|
||
p.clean_globs |= %w[**/.DS_Store tmp *.log] | ||
path = (p.rubyforge_name == p.name) ? p.rubyforge_name : "\#{p.rubyforge_name}/\#{p.name}" | ||
p.remote_rdoc_dir = File.join(path.gsub(/^#{p.rubyforge_name}\/?/,''), 'rdoc') | ||
p.rsync_args = '-av --delete --ignore-errors' | ||
end | ||
|
||
require 'newgem/tasks' # load /tasks/*.rake | ||
Dir['tasks/**/*.rake'].each { |t| load t } | ||
|
||
# TODO - want other tests/tasks run by default? Add them to the list | ||
# task :default => [:spec, :features] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
require 'pp' | ||
dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib')) | ||
require File.join(dir, 'sunshine') | ||
|
||
Sunshine.api_key = File.read(File.join(dir, '..', '.sunlight_api_key')) | ||
|
||
# pelosi = Sunshine::Legislator.find(:all, {:title => 'Rep'}) | ||
# pelosi.each do |l| | ||
# puts "#{l.full_name}: #{l.district}" | ||
# end | ||
# | ||
# many_dudes = Sunshine::Legislator.search('smith') | ||
# many_dudes.each do |score, leg| | ||
# puts leg.full_name | ||
# end | ||
# | ||
# zipdudes = Sunshine::Legislator.find_by_zipcode(97209) | ||
# zipdudes.each do |dude| | ||
# puts dude.full_name | ||
# end | ||
|
||
#democrats = Sunshine::Legislator.find(:all, :party => 'D') | ||
#pp Sunshine::Legislator.find(:one, :firstname => 'nancy') | ||
|
||
legislators = Sunshine::Legislator.find_by_lat_long("36.234822", "-115.180664") | ||
legislators.each do |l| | ||
puts l.full_name | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
require 'pp' | ||
dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib')) | ||
require File.join(dir, 'sunshine') | ||
|
||
Sunshine.api_key = File.read(File.join(dir, '..', '.sunlight_api_key')) | ||
|
||
district = Sunshine::District.find_by_zipcode(97209).first | ||
pp district.state | ||
|
||
district = Sunshine::District.find_by_lat_long("36.234822", "-115.180664").first | ||
pp district | ||
pp district.zipcodes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#-- | ||
# Copyright (c) 2009 Justin Palmer | ||
# | ||
# Permission is hereby granted, free of charge, to any person obtaining | ||
# a copy of this software and associated documentation files (the | ||
# "Software"), to deal in the Software without restriction, including | ||
# without limitation the rights to use, copy, modify, merge, publish, | ||
# distribute, sublicense, and/or sell copies of the Software, and to | ||
# permit persons to whom the Software is furnished to do so, subject to | ||
# the following conditions: | ||
# | ||
# The above copyright notice and this permission notice shall be | ||
# included in all copies or substantial portions of the Software. | ||
# | ||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
#++ | ||
|
||
$:.unshift(File.dirname(__FILE__)) unless | ||
$:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__))) | ||
|
||
|
||
module Sunshine | ||
# Set your Sunlight API key. | ||
# | ||
def api_key=(key) | ||
@api_key = key | ||
end | ||
module_function :api_key= | ||
|
||
# Get the API key. | ||
def api_key | ||
@api_key | ||
end | ||
module_function :api_key | ||
|
||
VERSION = '0.1.0' | ||
end | ||
|
||
require 'sunshine/base' | ||
require 'sunshine/district' | ||
require 'sunshine/legislator' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
require 'rubygems' | ||
gem 'httparty' | ||
require 'httparty' | ||
|
||
module Sunshine | ||
class Base | ||
include HTTParty | ||
|
||
base_uri "http://services.sunlightlabs.com/api" | ||
|
||
attr_reader :raw_data | ||
|
||
def initialize(data) | ||
data.each do |key, value| | ||
instance_variable_set("@#{key}", value) | ||
self.class.class_eval do | ||
define_method(key) { instance_variable_get("@#{key}") } | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def self.fetch(method, param_info) | ||
param_info = param_info.to_params if param_info.is_a?(Hash) | ||
begin | ||
get("/#{method}.json?#{param_info}&apikey=#{Sunshine.api_key}") | ||
rescue Net::HTTPServerException => e | ||
puts "#{e.response.code} ERROR: #{e.response.body_parsed}" | ||
return | ||
end | ||
end | ||
|
||
def self.initialize_one(obj) | ||
new(obj) | ||
end | ||
|
||
def self.initialize_many(results, key) | ||
results['response'][key].collect do |item| | ||
initialize_one(item) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
module Sunshine | ||
class District < Base | ||
def zipcodes | ||
@zipcodes ||= self.class.zipcodes_for_state_and_number(state, number) | ||
end | ||
|
||
class << self | ||
def find_by_zipcode(zip) | ||
response = fetch('districts.getDistrictsFromZip', :zip => zip) | ||
initialize_districts(response) | ||
end | ||
|
||
def find_by_lat_long(lat, long) | ||
response = fetch('districts.getDistrictFromLatLong', :latitude => lat, :longitude => long) | ||
initialize_districts(response) | ||
end | ||
|
||
def zipcodes_for_state_and_number(state, number) | ||
response = fetch('districts.getZipsFromDistrict', :state => state, :district => number) | ||
response['response']['zips'] | ||
end | ||
|
||
private | ||
def initialize_districts(response) | ||
results = [] | ||
response['response']['districts'].each do |district| | ||
results << initialize_one(district['district']) | ||
end | ||
results | ||
end | ||
|
||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
module Sunshine | ||
class Legislator < Base | ||
|
||
def full_name | ||
"#{firstname} #{lastname}" | ||
end | ||
|
||
class << self | ||
def senators | ||
find(:all, :title => 'Sen') | ||
end | ||
|
||
def representatives | ||
find(:all, :title => 'Rep') | ||
end | ||
|
||
def search(query) | ||
results = [] | ||
response = fetch('legislators.search', {:name => query}) | ||
response['response']['results'].each do |item| | ||
results << [item['result']['score'], initialize_one(item['result']['legislator'])] | ||
end | ||
results | ||
end | ||
|
||
def find(amount, params) | ||
legislators = [] | ||
response = fetch('legislators.getList', params) | ||
if response | ||
legislators = initialize_legislators(response) | ||
end | ||
return legislators.first if amount == :one | ||
legislators | ||
end | ||
|
||
def find_by_zipcode(zipcode) | ||
response = fetch('legislators.allForZip', {:zip => zipcode}) | ||
initialize_legislators(response) | ||
end | ||
|
||
def find_by_lat_long(lat, long) | ||
districts = District.find_by_lat_long(lat, long) | ||
state = districts.first.state | ||
url_parts = ["state=#{state}"] | ||
districts.each do |district| | ||
url_parts << "district=#{district.number}" | ||
end | ||
senators = find(:all, url_parts.first) | ||
reps = find(:all, url_parts.join("&")) | ||
[senators, reps].flatten | ||
end | ||
|
||
private | ||
|
||
def initialize_legislators(response) | ||
response['response']['legislators'].collect do |legislator| | ||
initialize_one(legislator['legislator']) | ||
end | ||
end | ||
end | ||
end | ||
end |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#!/usr/bin/env ruby | ||
# File: script/console | ||
irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb' | ||
|
||
libs = " -r irb/completion" | ||
# Perhaps use a console_lib to store any extra methods I may want available in the cosole | ||
# libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}" | ||
libs << " -r #{File.dirname(__FILE__) + '/../lib/sunshine.rb'}" | ||
puts "Loading sunshine gem" | ||
exec "#{irb} #{libs} --simple-prompt" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#!/usr/bin/env ruby | ||
APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..')) | ||
|
||
begin | ||
require 'rubigen' | ||
rescue LoadError | ||
require 'rubygems' | ||
require 'rubigen' | ||
end | ||
require 'rubigen/scripts/destroy' | ||
|
||
ARGV.shift if ['--help', '-h'].include?(ARGV[0]) | ||
RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit] | ||
RubiGen::Scripts::Destroy.new.run(ARGV) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#!/usr/bin/env ruby | ||
APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..')) | ||
|
||
begin | ||
require 'rubigen' | ||
rescue LoadError | ||
require 'rubygems' | ||
require 'rubigen' | ||
end | ||
require 'rubigen/scripts/generate' | ||
|
||
ARGV.shift if ['--help', '-h'].include?(ARGV[0]) | ||
RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit] | ||
RubiGen::Scripts::Generate.new.run(ARGV) |
Oops, something went wrong.