Skip to content

Commit

Permalink
Initial plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
spf13 committed Jul 14, 2012
0 parents commit 2486dce
Show file tree
Hide file tree
Showing 53 changed files with 17,960 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
source :rubygems
gem "mongo"
gem "bson_ext"
gem "sinatra"
gem "haml"
gem "shotgun"
gem "googlestaticmap"
31 changes: 31 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
GEM
remote: http://rubygems.org/
specs:
bson (1.6.4)
bson_ext (1.6.4)
bson (~> 1.6.4)
googlestaticmap (1.1.3)
haml (3.1.4)
mongo (1.6.4)
bson (~> 1.6.4)
rack (1.4.1)
rack-protection (1.2.0)
rack
shotgun (0.9)
rack (>= 1.0)
sinatra (1.3.2)
rack (~> 1.3, >= 1.3.6)
rack-protection (~> 1.2)
tilt (~> 1.3, >= 1.3.3)
tilt (1.3.3)

PLATFORMS
ruby

DEPENDENCIES
bson_ext
googlestaticmap
haml
mongo
shotgun
sinatra
30 changes: 30 additions & 0 deletions README
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

Milieu is an open source app similar to foursquare or facebook checkins.

It was initially built for OSCON 2012 as a demo app for building an app based on MongoDB.

It is built in Ruby using Sinatra and Haml.

This has been built by spf13 http://spf13.com for 10gen

It is based on the following Gems
- Bundler
- Shotgun
- Rack
- Haml
- Mongo
- Vlad the Deployer

To get up and running all that should be needed is to install the Ruby gem
Bundler if up don't already have it installed then run:
- $ bundle install

That should install everything that is need for the app to run. To start it
run:
- $ shotgun

As is the case with any Shotgun app you can update the config.ru file to
change Shotgun settings. Like wise /config/deploy.rb for the Vlad settings and
Gemfile for your bundler install settings

enjoy!
7 changes: 7 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
begin
require 'vlad'
Vlad.load(app: nil, scm: 'git')
rescue LoadError
# do nada
end

129 changes: 129 additions & 0 deletions app.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
require 'sinatra'
require './helpers/sinatra'
require './helpers/milieu'
require './model/mongodb'
require 'haml'
require 'digest/md5'
require 'googlestaticmap'


configure do
enable :sessions
end

before do
unless session[:user] == nil
@suser = session[:user]
end
end

get '/' do
haml :index
end

get '/user' do
redirect '/user/' + session[:user].email + '/profile'
end

get '/about' do
haml :about
end

get '/login' do
haml :login
end

post '/login' do
if session[:user] = User.auth(params["email"], params["password"])
flash("Login successful")
redirect "/user/" << session[:user].email << "/dashboard"
else
flash("Login failed - Try again")
redirect '/login'
end
end

get '/logout' do
session[:user] = nil
flash("Logout successful")
redirect '/'
end

get '/register' do
haml :register
end

post '/register' do
u = User.new
u.email = params[:email]
u.password = params[:password]
u.name = params[:name]
#u.email_hash = Digest::MD5.hexdigest(params[:email].downcase)

if u.save()
flash("User created")
session[:user] = User.auth( params["email"], params["password"])
redirect '/user/' << session[:user].email.to_s << "/dashboard"
else
tmp = []
u.errors.each do |e|
tmp << (e.join("<br/>"))
end
flash(tmp)
redirect '/create'
end
end

get '/user/:email/dashboard' do
haml :user_dashboard
end

get '/user/:email/profile' do
@user = User.new_from_email(params[:email])
haml :user_profile
end

get '/list' do
@users = USERS.find
haml :list
end

get '/venues/?:page?' do
@page = params.fetch('page', 1).to_i
num_per_page = 10
@venues = VENUES.find.skip(( @page - 1 ) * num_per_page).limit(num_per_page)
@total_pages = (VENUES.count.to_i / num_per_page).ceil
haml :venues
end

get '/venue/:_id' do
object_id = BSON::ObjectId.from_string(params[:_id])
@venue = VENUES.find_one({ :_id => object_id })
@user = User.new_from_email(@suser.email)
@nearby_venues = VENUES.find(
{ :'location.geo' =>
{ :$near => [ @venue['location']['geo'][0],
@venue['location']['geo'][1]]
}
}).limit(4).skip(1)
haml :venue
end

# Add lots of comments here
get '/venue/:_id/checkin' do
object_id = BSON::ObjectId.from_string(params[:_id])
@venue = VENUES.find_one({ :_id => object_id })
user = USERS.find_and_modify(:query => { :_id => @suser._id}, :update => {:$inc => { "venues." << object_id.to_s => 1 } }, :new => 1)
if user['venues'][params[:_id]] == 1
VENUES.update({ :_id => @venue['_id']}, { :$inc => { :'stats.usersCount' => 1, :'stats.checkinsCount' => 1}})
else
VENUES.update({ _id: @venue['_id']}, { :$inc => { :'stats.checkinsCount' => 1}})
end
flash('Thanks for checking in')
redirect '/venue/' + params[:_id]
end

get '/checkin/:location' do
@venues = VENUES.find( )
haml :venues
end
7 changes: 7 additions & 0 deletions config.ru
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
path = File.expand_path("../", __FILE__)

require 'rubygems'
require 'sinatra'
require "#{path}/app"

run Sinatra::Application
5 changes: 5 additions & 0 deletions config/deploy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
set :application, "your/application"
set :user, "user"
set :repository, "git@yourgitrepo:project"
set :domain, "yourdomain.com"
set :deploy_to, "~/sites/#{application}"
49 changes: 49 additions & 0 deletions helpers/milieu.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
helpers do
def user_times_at
if logged_in?
times = 'You have checked in here '
if !@user.venues.nil? && !@user.venues[params[:_id]].nil?
times << @user.venues[params[:_id]].to_s
else
times << '0'
end
times << ' times'
else
times = 'Please <a href=\'/login\'>login</a> to join them.'
end
end

def gmap_url(venue, options = {})
maplocation = MapLocation.new(:longitude => venue['location']['geo'][0], :latitude => venue['location']['geo'][1] )
default_options = {:zoom => 16, :center => maplocation}
map = GoogleStaticMap.new(default_options.merge(options))
map.markers << MapMarker.new(:color => "blue", :location => maplocation)
map.url(:auto)
end

def pager(cur_path)
str_out = '<div class="pagination pagination-centered"> <ul>'
if @page != 1
str_out << "<li><a href='#{cur_path}/" << (@page - 1).to_s << "'>Prev</a></li>"
else
str_out << "<li class='disabled'><a>Prev</a></li>"
end

(1 .. @total_pages).each do |i|
if @page == i
str_out << "<li class='active'><a href='#{cur_path}/#{i}'>#{i}</a></li>"
else
str_out << "<li><a href='#{cur_path}/#{i}'>#{i}</a></li>"
end
end

if @page != @total_pages
str_out << "<li><a href='#{cur_path}/" << (@page + 1).to_s << "'>Next</a></li>"
else
str_out << "<li class='disabled'><a>Next</a></li>"
end

str_out << "</ul></div>"
str_out
end
end
34 changes: 34 additions & 0 deletions helpers/sinatra.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
helpers do
def logged_in?
return true if session[:user]
nil
end

def link_to(name, location, alternative = false)
if alternative and alternative[:condition]
"<a href=#{alternative[:location]}>#{alternative[:name]}</a>"
else
"<a href=#{location}>#{name}</a>"
end
end

def random_string(len)
chars = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a
str = ""
1.upto(len) { |i| str << chars[rand(chars.size-1)] }
return str
end

def flash(msg)
session[:flash] = msg
end

def show_flash
if session[:flash]
tmp = session[:flash]
session[:flash] = false
"<div class=\"container-fluid\"><div class=\"row-fluid\"><div class='span3'>&nbsp;</div><div class=\"alert alert-info span6\" align='center'>#{tmp}</div><div class='span3'>&nbsp;</div></div></div>"
end
end

end
30 changes: 30 additions & 0 deletions model/mongoModule.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module MongoModule

attr_accessor :collection, :updated_at

def initialize(hash = nil)
self.init_collection

unless hash == nil
hash.each do |k,v|
self.instance_variable_set("@#{k}", v)
self.class.send(:define_method, k, proc{self.instance_variable_get("@#{k}")})
self.class.send(:define_method, "#{k}=", proc{|v| self.instance_variable_set("@#{k}", v)})
end
end
end

def save()
col = DB[self.collection]
@updated_at = Time.now
col.save(self.to_hash)
end

def to_hash
h = {}
instance_variables.each {|var| h[var.to_s.delete("@")] = instance_variable_get(var) }
h.delete("collection")
h
end

end
10 changes: 10 additions & 0 deletions model/mongodb.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require 'mongo'
require './model/mongoModule'
require './model/user'

CONNECTION = Mongo::Connection.new("localhost", 27017)
DB = CONNECTION.db('milieu')
USERS = DB['users']
VENUES = DB['venues']
CHECKINS = DB['checkins']

49 changes: 49 additions & 0 deletions model/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
class User
include MongoModule

attr_accessor :_id, :name, :email, :email_hash, :salt, :hashed_password, :venues

def init_collection
@collection = 'users'
end

def email=(an_email = nil)
if an_email == nil
@email.downcase
else
@email = an_email.downcase
@email_hash = Digest::MD5.hexdigest(@email)
end
end

def password=(pass)
@salt = random_string(10) unless @salt
@hashed_password = User.encrypt(pass, @salt)
end

def self.encrypt(pass, salt)
Digest::SHA1.hexdigest(pass + salt)
end

def self.auth(email, pass)
u = USERS.find_one("email" => email.downcase)
return nil if u.nil?
return User.new(u) if User.encrypt(pass, u['salt']) == u['hashed_password']
nil
end

def self.new_from_email(email)
u = USERS.find_one("email" => email.downcase)
return nil if u.nil?
return User.new(u)
nil
end

def random_string(len)
chars = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a
str = ""
1.upto(len) { |i| str << chars[rand(chars.size-1)] }
return str
end

end
Binary file added public/.DS_Store
Binary file not shown.
Loading

0 comments on commit 2486dce

Please sign in to comment.