Skip to content

Instantly share code, notes, and snippets.

@yoamomonstruos
Created March 23, 2012 15:00
Show Gist options
  • Save yoamomonstruos/2171474 to your computer and use it in GitHub Desktop.
Save yoamomonstruos/2171474 to your computer and use it in GitHub Desktop.
Idea API
require "sinatra"
require "sinatra/reloader"
require "json"
require "mongoid"
set :sessions, true
# ===================================
# = Let's define our configurations =
# ===================================
configure do
Mongoid.configure do |config|
config.master = Mongo::Connection.new.db("versions_dev")
end
end
# ===================================
# = Let's define our helpers =
# ===================================
helpers do
def current_user
User.first(conditions: { id: session[:user] }) if session[:user]
end
def logged_in?
!!session[:user]
end
def has_access?
redirect "/login" unless logged_in?
end
def user
@user ||= User.find(params[:user_id]) || halt(404)
end
def project
@project ||= user.projects.find(params[:project_id]) || halt(404)
end
def thought
@thought ||= project.thoughts.find(params[:thought_id]) || halt(404)
end
end
# =====================================
# = Let's define our before functions =
# =====================================
before do
content_type :json
end
# ================================
# = Let's define all our classes =
# ================================
# User Class
class User
include Mongoid::Document
include Mongoid::Timestamps
attr_accessor :password, :password_confirmation
field :username, type: String
field :email, type: String
field :password_salt, type: String
field :password_hash, type: String
has_many :projects
before_create :generate_password
def generate_password
unless password.blank?
self.password_salt = Digest::SHA1.hexdigest([Time.now, rand].join)
self.password_hash = encrypt_password(password)
end
end
def encrypt_password(password)
Digest::SHA1.hexdigest(password+self.password_salt)
end
def self.authenticate(username, password)
user = first(:conditions => { :username => username })
return user if user and user.matching_password?(password)
end
def matching_password?(password)
self.password_hash == encrypt_password(password)
end
end
# Project Class
class Project
include Mongoid::Document
include Mongoid::Timestamps
field :title, type: String
field :slug, type: String
field :info, type: String
field :public, type: Boolean
belongs_to :user
has_many :thoughts
before_save :generate_slug
def generate_slug
unless self.title.blank?
string = self.title
string = string.strip
string.gsub!(/['`]/,"")
string.gsub!(/\s*@\s*/, " at ")
string.gsub!(/\s*&\s*/, " and ")
string.gsub!(/\s*[^A-Za-z0-9\.\-]\s*/, '-')
string.gsub!(/_+/,"-")
string.gsub!(/\A[_\.]+|[_\.]+\z/,"")
self.slug = string.downcase
end
end
end
# Thought class
class Thought
include Mongoid::Document
include Mongoid::Timestamps
field :title, type: String
field :type, type: String
field :text, type: String
field :parent_id, type: String
field :published, type: Boolean
belongs_to :project
end
# =======================
# = API TIME =
# =======================
# Return all users
get "/api/users" do
users = User.all
if users
users.to_json
else
status 404
end
end
# Create a new user
post "/api/users" do
new_user = User.new(JSON.parse(request.body.read))
if new_user.save
status 201
new_user.to_json
else
error 400
end
end
# Return a user
get "/api/users/:user_id" do
user.to_json
end
# Update a user
put "/api/users/:user_id" do
if user.update_attributes(JSON.parse(request.body.read))
status 200
user.to_json
else
status 400
end
end
# Delete a user
delete "/api/users/:user_id" do
if user.delete
status 200
else
status 400
end
end
# Return one users projects
get "/api/users/:user_id/projects" do
projects = user.projects
if projects
projects.to_json
else
status 404
end
end
# Create a new project
post "/api/users/:user_id/projects" do
new_project = user.projects.new(JSON.parse(request.body.read))
if new_project.save
status 201
new_project.to_json
else
status 400
end
end
# Return a users project
get "/api/users/:user_id/projects/:project_id" do
project.to_json
end
# Update a project
put "/api/users/:user_id/projects/:project_id" do
if project.update_attributes(JSON.parse(request.body.read))
status 200
project.to_json
else
status 400
end
end
# Delete a project
delete "/api/users/:user_id/projects/:project_id" do
if project.delete
status 200
else
status 400
end
end
# Return a projects thoughts
get "/api/users/:user_id/projects/:project_id/thoughts" do
thoughts = project.thoughts
if thoughts
thoughts.to_json
else
status 404
end
end
# Create a new project
post "/api/users/:user_id/projects/:project_id/thoughts" do
new_thought = project.thoughts.new(JSON.parse(request.body.read))
if new_thought.save
status 201
new_thought.to_json
else
status 400
end
end
# Return an individual thought
get "/api/users/:user_id/projects/:project_id/thoughts/:thought_id" do
thought.to_json
end
# Update an individual thought
put "/api/users/:user_id/projects/:project_id/thoughts/:thought_id" do
if thought.update_attributes(JSON.parse(request.body.read))
status 200
thought.to_json
else
status 400
end
end
# Delete a thought from a project
delete "/api/users/:user_id/projects/:project_id/thoughts/:thought_id" do
if thought.delete
status 200
else
status 400
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment