Skip to content

Commit

Permalink
Initial import.
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Bleigh committed Nov 13, 2010
0 parents commit 10f2b85
Show file tree
Hide file tree
Showing 16 changed files with 635 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/tmp
log/*.log
.DS_Store
.bundle
8 changes: 8 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
source 'http://rubygems.org'

gem 'i18n'
gem 'bson_ext'
gem 'mongo_mapper'
gem 'oa-oauth'
gem 'sinatra'
gem 'grape', '0.0.0.beta.1', :path => '/Users/mbleigh/gems/grape'
66 changes: 66 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
PATH
remote: /Users/mbleigh/gems/grape
specs:
grape (0.0.0.beta.1)
multi_json
multi_xml
rack
rack-jsonp
rack-mount

GEM
remote: http://rubygems.org/
specs:
activesupport (3.0.1)
addressable (2.2.2)
bson (1.1.2)
bson_ext (1.1.2)
faraday (0.4.6)
addressable (>= 2.1.1)
rack (>= 1.0.1)
i18n (0.4.1)
jnunemaker-validatable (1.8.4)
activesupport (>= 2.3.4)
mongo (1.1.2)
bson (>= 1.1.1)
mongo_mapper (0.8.6)
activesupport (>= 2.3.4)
jnunemaker-validatable (~> 1.8.4)
plucky (~> 0.3.6)
multi_json (0.0.5)
multi_xml (0.2.0)
nokogiri (1.4.3.1)
oa-core (0.1.4)
rack (~> 1.1)
oa-oauth (0.1.4)
multi_json (~> 0.0.2)
nokogiri (~> 1.4.2)
oa-core (= 0.1.4)
oauth (~> 0.4.0)
oauth2 (~> 0.0.10)
oauth (0.4.3)
oauth2 (0.0.13)
faraday (~> 0.4.1)
multi_json (>= 0.0.4)
plucky (0.3.6)
mongo (~> 1.1)
rack (1.2.1)
rack-jsonp (1.1.0)
rack
rack-mount (0.6.13)
rack (>= 1.0.0)
sinatra (1.1.0)
rack (~> 1.1)
tilt (~> 1.1)
tilt (1.1)

PLATFORMS
ruby

DEPENDENCIES
bson_ext
grape (= 0.0.0.beta.1)!
i18n
mongo_mapper
oa-oauth
sinatra
73 changes: 73 additions & 0 deletions api.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
require 'grape'

module ConfAsk
class API < Grape::API
prefix 'api'
version 'v1'

helpers do
def current_user
@current_user ||= User.find_by_id(env['rack.session'][:user_id])
end

def authenticate!
error!("Must be logged in.", 401) unless current_user
end
end

get '/me' do
authenticate!
current_user
end

resources :questions do
get do
current_user
Question.sort([['$natural', -1]]).all
end

post do
authenticate!
Question.create(:text => params[:text], :screen_name => current_user.screen_name)
end

helpers do
def question
@question ||= Question.find_by_id(params[:id])
end
end

get '/:id/votes' do
authenticate!
question.vote!
end
end

# Only administrators can access the resources
# in the `admin` namespace.
namespace :admin do
http_basic do |u,p|
u == 'admin' && p == (ENV['ADMIN_PASSWORD'] || 'rubyconf')
end

# Get a list of users who have signed into the
# application.
get '/users' do
[{:hey => 'you'},{:there => 'bar'},{:foo => 'baz'}]
end

resources :votes do
get do

end

# Delete a vote directly by providing its ID.
#
# @param [String ID] id The ID of the vote to be deleted.
delete '/:id' do
"Deleting #{params[:id]}"
end
end
end
end
end
31 changes: 31 additions & 0 deletions application.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
require 'sinatra'

module ConfAsk
class Application < Sinatra::Base
helpers do
def current_user
@current_user ||= User.find_by_id(session[:user_id])
end

def current_user=(user)
session[:user_id] = user.id
@current_user = user
end
end

get '/' do
erb :home
end

get '/auth/twitter/callback' do
self.current_user = User.authenticate(request.env['omniauth.auth'])
redirect '/'
end

get '/sign_out' do
session.delete(:user_id)
@current_user = nil
redirect '/'
end
end
end
18 changes: 18 additions & 0 deletions config.ru
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
require 'environment'
require 'rack'

if ENV['RACK_ENV'] == 'development'
log = File.new("log/development.log", "a")
STDOUT.reopen(log)
STDERR.reopen(log)
end

require 'omniauth/oauth'

use Rack::Session::Cookie
use OmniAuth::Strategies::Twitter, ENV['TWITTER_KEY'], ENV['TWITTER_SECRET']

run Rack::Cascade.new([
ConfAsk::Application,
ConfAsk::API
])
17 changes: 17 additions & 0 deletions environment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require 'rubygems'
require 'bundler'

Bundler.setup :default, (ENV['RACK_ENV'] || 'development')

require 'mongo_mapper'
require 'application'
require 'api'
require 'models'

if ENV['MONGOHQ_URL']
MongoMapper.config = {ENV['RACK_ENV'] => {'uri' => ENV['MONGOHQ_URL']}}
else
MongoMapper.config = {ENV['RACK_ENV'] => {'uri' => 'mongodb://localhost/confask'}}
end

MongoMapper.connect(ENV['RACK_ENV'])
2 changes: 2 additions & 0 deletions models.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
autoload :User, 'models/user'
autoload :Question, 'models/question'
13 changes: 13 additions & 0 deletions models/question.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require 'mongo_mapper'

class Question
include MongoMapper::Document

key :screen_name, String, :required => true
key :text, String, :required => true
timestamps!

def user
User.find_by_screen_name(screen_name)
end
end
25 changes: 25 additions & 0 deletions models/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require 'mongo_mapper'

class User
include MongoMapper::Document

key :uid, String, :required => true
key :screen_name, String, :required => true

def questions
Question.where(:screen_name => screen_name)
end

def self.authenticate(auth_hash)
user = User.find_by_uid(auth_hash['uid'].to_s)
user ||= User.create!(:uid => auth_hash['uid'].to_s, :screen_name => auth_hash['user_info']['nickname'])
user
end

def serializable_hash(include_questions = true)
{
:screen_name => self.screen_name,
:questions => self.questions
}
end
end
Binary file added public/images/vote_button.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
50 changes: 50 additions & 0 deletions public/javascripts/application.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
$(function() {
$('#loading').ajaxStart(function() {
$(this).fadeIn('slow');
});

$('#loading').ajaxStop(function() {
$(this).fadeOut('slow');
});

$(document).bind('questions:reload', function() {
$.ajax({
type:'GET',
url:'/api/v1/questions',
dataType:'json',
success:function(data) {
var build = $('<ul/>')
$.each(data, function() {
build.append("<li data-id='" + this.id + "'><img class='avatar' src='http://img.tweetimag.es/i/" + this.screen_name + "_n'/> " + this.text + "</li>");
});

$('#question_list').html(build.html());
},
error:function() {
alert("Unable to refresh the question list.");
}
});
});

$(document).trigger('questions:reload');
setInterval(function() {
$(document).trigger('questions:reload');
}, 30000);

$('form#ask').submit(function() {
$.ajax({
type:'POST',
url:'/api/v1/questions',
data:$('form#ask').serialize(),
dataType:'json',
success:function(data) {
$(document).trigger('questions:reload');
$('form#ask')[0].reset();
},
error:function() {
alert("There was a problem asking your question.");
}
});
return false;
});
});
Loading

0 comments on commit 10f2b85

Please sign in to comment.