Skip to content

Commit

Permalink
Added GovKit and Sunlight gems, as well as some POC code.
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeSc committed May 7, 2012
1 parent a1ad1b2 commit db04475
Show file tree
Hide file tree
Showing 19 changed files with 203 additions and 15 deletions.
5 changes: 5 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
source 'http://rubygems.org'

gem 'rails', '3.1.3'
gem 'json'
gem 'ym4r'
gem 'sunlight'
gem 'govkit', :git => 'git://github.com/opengovernment/govkit.git'
gem 'haml'

# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'
Expand Down
31 changes: 31 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
GIT
remote: git://github.com/opengovernment/govkit.git
revision: 7279f1e6c7e3fd404f758fbfcd859eec8af0bb71
specs:
govkit (0.7.2)
activesupport
fastercsv (>= 1.5.3)
httparty
httparty (>= 0.7.4)
i18n
json (>= 1.4.3)
nokogiri (>= 1.4.4)
nokogiri

GEM
remote: http://rubygems.org/
specs:
Expand Down Expand Up @@ -41,7 +55,13 @@ GEM
erubis (2.7.0)
execjs (1.3.1)
multi_json (~> 1.0)
fastercsv (1.5.4)
geoip-rails (0.1.2)
haml (3.1.4)
hike (1.2.1)
httparty (0.8.3)
multi_json (~> 1.0)
multi_xml
i18n (0.6.0)
jquery-rails (1.0.19)
railties (~> 3.0)
Expand All @@ -53,6 +73,8 @@ GEM
treetop (~> 1.4.8)
mime-types (1.18)
multi_json (1.3.4)
multi_xml (0.4.4)
nokogiri (1.5.2)
polyglot (0.3.3)
rack (1.3.6)
rack-cache (1.2)
Expand Down Expand Up @@ -92,6 +114,9 @@ GEM
rack (~> 1.0)
tilt (~> 1.1, != 1.3.0)
sqlite3 (1.3.6)
sunlight (1.1.0)
json (>= 1.1.3)
ym4r (>= 0.6.1)
thor (0.14.6)
tilt (1.3.3)
treetop (1.4.10)
Expand All @@ -101,15 +126,21 @@ GEM
uglifier (1.2.4)
execjs (>= 0.3.0)
multi_json (>= 1.0.2)
ym4r (0.6.1)

PLATFORMS
ruby

DEPENDENCIES
coffee-rails (~> 3.1.1)
geoip-rails
govkit!
haml
jquery-rails
json
rails (= 3.1.3)
sass-rails (~> 3.1.5)
sqlite3
sunlight
uglifier (>= 1.0.3)
ym4r
20 changes: 20 additions & 0 deletions app/assets/javascripts/home.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
var locale;

function receiveLocation() {
locale = google.loader.ClientLocation;
$('#message').html('Finding representatives for '+ locale.address.city +'.');

$.get('/reps/find_by_latlong', {lat: locale.latitude, long: locale.longitude}, receiveReps);
}


function receiveReps(data) {
$('#message').html('Found your representatives for '+ locale.address.city + ':');
var reps = $.parseJSON(data);
var repContainer = $('#repContainer');
console.log(data[0]);
$.each(data, function(index, value) {
repContainer.append($('<div class="rep_name">'+ value +'</div>'));
});
}

3 changes: 3 additions & 0 deletions app/assets/stylesheets/home.css.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the Home controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
6 changes: 6 additions & 0 deletions app/controllers/home_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class HomeController < ApplicationController

def index
end

end
5 changes: 5 additions & 0 deletions app/controllers/reps_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ def dashboard
format.json { render :json => @reps }
end
end


def find_by_latlong
return render :json => Rep.find_by_latlong(params[:lat], params[:long])
end


# GET /reps
Expand Down
2 changes: 2 additions & 0 deletions app/helpers/home_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module HomeHelper
end
15 changes: 15 additions & 0 deletions app/models/mention.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# A model to contain mentions of the :owner in the media
class Mention < ActiveRecord::Base
belongs_to :owner, :polymorphic => true

scope :since, lambda { |d| where(["mentions.date > ?", d]) }

# Returns the mentions in JSON form
#
# @params [Hash] A hash of options
# @return The mentions in JSON form
def as_json(opts = {})
default_opts = {:except => [:owner_id, :owner_type]}
super(default_opts.merge(opts))
end
end
30 changes: 30 additions & 0 deletions app/models/rep.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,33 @@
class Rep < ActiveRecord::Base
has_one :user

def self.find_by_latlong(lat, long)
logger.info 'Looking up rep for ' + lat.to_s + ' ' + long.to_s

state = self.find_state_reps_by_latlong(lat, long)
federal = self.find_federal_reps_by_latlong(lat, long)
state.merge(federal)
end

def self.find_state_reps_by_latlong(lat, long)
GovKit::OpenStates
results = GovKit::OpenStatesResource.get_uri('/legislators/geo?', :query => {:lat => lat, :long => long})
representatives = Hash.new
results.each{ |rep|
representatives[rep.chamber + ' ' + rep.district] = rep.full_name
}
representatives
end


def self.find_federal_reps_by_latlong(lat, long)
federal = Sunlight::Legislator.all_for(:latitude => lat, :longitude => long)
representatives = Hash.new
federal.each{ |rep|
full_name = rep[1].firstname + ' ' + rep[1].lastname
representatives[rep[1].govtrack_id] = full_name
}
representatives
end

end
5 changes: 5 additions & 0 deletions app/views/home/index.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

#message
Finding your representatives.

#repContainer
14 changes: 0 additions & 14 deletions app/views/layouts/application.html.erb

This file was deleted.

16 changes: 16 additions & 0 deletions app/views/layouts/application.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
!!! 5
%html
%head
%title
AsqUs

= stylesheet_link_tag "application"
= javascript_include_tag "application"
= javascript_include_tag "http://www.google.com/jsapi"
= csrf_meta_tags
:javascript
google.load("maps", "2", {callback: receiveLocation});
%body

= yield

3 changes: 3 additions & 0 deletions config/environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@

# Initialize the rails application
AsqUs::Application.initialize!

SUNLIGHT_API_KEY = 'f4838aad396e47fca55ae4f292273584'

24 changes: 24 additions & 0 deletions config/initializers/govkit.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
if defined? GovKit
GovKit.configure do |config|
# Get an API key for Sunlight's Open States project here:
# http://services.sunlightlabs.com/accounts/register/
config.sunlight_apikey = 'f4838aad396e47fca55ae4f292273584'

##API key for Votesmart
# http://votesmart.org/services_api.php
#config.votesmart_apikey = 'YOUR_VOTESMART_API_KEY'

# API key for NIMSP. Request one here:
# http://www.followthemoney.org/membership/settings.phtml
#config.ftm_apikey = 'YOUR_FTM_API_KEY'

# Technorati API key
# config.technorati_apikey = 'YOUR_TECHNORATI_APIKEY'

# Bing App ID
#config.bing_appid = 'YOUR_BING_APPID'

# Other things you could set here include alternate URLs for
# the APIs. See GovKit::Configuration for available attributes.
end
end
4 changes: 4 additions & 0 deletions config/initializers/sunlight.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
require 'rubygems'
require 'sunlight'
Sunlight::Base.api_key = 'f4838aad396e47fca55ae4f292273584'

3 changes: 2 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
AsqUs::Application.routes.draw do

match 'reps/dashboard' => 'reps#dashboard', :as => :rep_dashboard
match 'reps/find_by_latlong' => 'reps#find_by_latlong'

resources :users
resources :reps
Expand Down Expand Up @@ -54,7 +55,7 @@

# You can have the root of your site routed with "root"
# just remember to delete public/index.html.
root :to => 'reps#index'
root :to => 'home#index'

# See how all your routes lay out with "rake routes"

Expand Down
21 changes: 21 additions & 0 deletions db/migrate/20120507084733create_mentions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class CreateMentions < ActiveRecord::Migration
def self.up
create_table :mentions, :force => true do |t|
t.string "url", :limit => 8000
t.string "excerpt", :limit => 4000
t.string "title", :limit => 1000
t.string "source"
t.datetime "date"
t.float "weight"
t.integer "owner_id"
t.string "owner_type"
t.string "search_source"
t.datetime "created_at"
t.datetime "updated_at"
end
end

def self.down
drop_table :mentions
end
end
7 changes: 7 additions & 0 deletions test/functional/home_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'test_helper'

class HomeControllerTest < ActionController::TestCase
# test "the truth" do
# assert true
# end
end
4 changes: 4 additions & 0 deletions test/unit/helpers/home_helper_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
require 'test_helper'

class HomeHelperTest < ActionView::TestCase
end

0 comments on commit db04475

Please sign in to comment.