cs_comments_service/app.rb

170 строки
4.1 KiB
Ruby
Исходник Обычный вид История

2012-06-21 02:25:35 +04:00
require 'rubygems'
require 'bundler'
Bundler.setup
Bundler.require
2012-06-21 02:25:35 +04:00
env_index = ARGV.index("-e")
env_arg = ARGV[env_index + 1] if env_index
2012-07-27 03:58:39 +04:00
environment = env_arg || ENV["SINATRA_ENV"] || "development"
RACK_ENV = environment
module CommentService
class << self; attr_accessor :config; end
end
CommentService.config = YAML.load_file("config/application.yml")
Mongoid.load!("config/mongoid.yml")
Mongoid.logger.level = Logger::INFO
Dir[File.dirname(__FILE__) + '/models/*.rb'].each {|file| require file}
2012-07-24 00:15:44 +04:00
Dir[File.dirname(__FILE__) + '/lib/**/*.rb'].each {|file| require file}
delete '/api/v1/:commentable_id/threads' do |commentable_id|
2012-07-27 03:12:30 +04:00
commentable.comment_threads.destroy_all
{}.to_json
2012-06-21 02:25:35 +04:00
end
get '/api/v1/:commentable_id/threads' do |commentable_id|
2012-07-27 03:12:30 +04:00
commentable.comment_threads.map{|t| t.to_hash(recursive: params["recursive"])}.to_json
2012-06-21 02:25:35 +04:00
end
post '/api/v1/:commentable_id/threads' do |commentable_id|
2012-07-18 22:01:04 +04:00
thread = CommentThread.new(params.slice(*%w[title body course_id]).merge(commentable_id: commentable_id))
2012-07-28 21:07:05 +04:00
thread.tags = params["tags"] || ""
2012-07-27 03:12:30 +04:00
thread.author = user
thread.save!
2012-07-27 03:12:30 +04:00
if params["auto_subscribe"] and author
author.subscribe(thread)
end
thread.to_hash.to_json
2012-06-21 02:25:35 +04:00
end
2012-07-29 02:21:03 +04:00
get '/api/v1/threads/tags' do
CommentThread.tags.to_json
end
get '/api/v1/threads/:thread_id' do |thread_id|
thread.to_hash(recursive: params["recursive"]).to_json
2012-06-21 02:25:35 +04:00
end
put '/api/v1/threads/:thread_id' do |thread_id|
thread.update_attributes!(params.slice(*%w[title body]))
2012-07-28 21:07:05 +04:00
if params["tags"]
thread.tags = params["tags"]
thread.save!
end
thread.to_hash.to_json
2012-06-22 00:47:14 +04:00
end
post '/api/v1/threads/:thread_id/comments' do |thread_id|
comment = thread.comments.new(params.slice(*%w[body course_id]))
2012-07-27 03:12:30 +04:00
comment.author = user
comment.save!
2012-07-27 03:12:30 +04:00
if params["auto_subscribe"] and author
author.subscribe(thread)
end
comment.to_hash.to_json
end
delete '/api/v1/threads/:thread_id' do |thread_id|
thread.destroy
thread.to_hash.to_json
2012-06-21 02:25:35 +04:00
end
get '/api/v1/comments/:comment_id' do |comment_id|
comment.to_hash(recursive: params["recursive"]).to_json
end
2012-06-21 02:25:35 +04:00
put '/api/v1/comments/:comment_id' do |comment_id|
comment.update_attributes!(params.slice(*%w[body endorsed]))
comment.to_hash.to_json
2012-06-21 02:25:35 +04:00
end
post '/api/v1/comments/:comment_id' do |comment_id|
sub_comment = comment.children.new(params.slice(*%w[body course_id]))
2012-07-27 03:12:30 +04:00
sub_comment.author = user
sub_comment.comment_thread = comment.comment_thread
sub_comment.save!
sub_comment.to_hash.to_json
end
delete '/api/v1/comments/:comment_id' do |comment_id|
comment.destroy
comment.to_hash.to_json
end
2012-07-17 03:35:34 +04:00
put '/api/v1/comments/:comment_id/votes' do |comment_id|
2012-07-17 05:31:42 +04:00
vote_for comment
2012-06-21 02:25:35 +04:00
end
2012-07-17 03:35:34 +04:00
delete '/api/v1/comments/:comment_id/votes' do |comment_id|
2012-07-18 22:01:04 +04:00
undo_vote_for comment
end
2012-07-17 03:35:34 +04:00
put '/api/v1/threads/:thread_id/votes' do |thread_id|
2012-07-17 05:31:42 +04:00
vote_for thread
end
2012-07-17 03:35:34 +04:00
delete '/api/v1/threads/:thread_id/votes' do |thread_id|
2012-07-18 21:02:17 +04:00
undo_vote_for thread
end
2012-07-25 22:58:33 +04:00
get '/api/v1/users/:user_id' do |user_id|
user.to_hash(complete: params["complete"]).to_json
end
2012-07-17 03:35:34 +04:00
get '/api/v1/users/:user_id/notifications' do |user_id|
user.notifications.map(&:to_hash).to_json
end
2012-07-17 03:35:34 +04:00
post '/api/v1/users/:user_id/subscriptions' do |user_id|
2012-07-17 07:05:16 +04:00
user.subscribe(source).to_hash.to_json
end
2012-07-17 03:35:34 +04:00
delete '/api/v1/users/:user_id/subscriptions' do |user_id|
2012-07-17 07:05:16 +04:00
user.unsubscribe(source).to_hash.to_json
end
2012-07-24 00:15:44 +04:00
# GET /api/v1/search
# params:
# text: text to search for
# commentable_id: search within a commentable
#
get '/api/v1/search' do
if params["text"]
CommentThread.solr_search do
fulltext(params["text"])
if params["commentable_id"]
with(:commentable_id, params["commentable_id"])
end
end.results.map(&:to_hash).to_json
else
{}.to_json
end
end
2012-07-27 03:58:39 +04:00
if environment.to_s == "development"
2012-06-21 02:25:35 +04:00
get '/api/v1/clean' do
Comment.delete_all
CommentThread.delete_all
User.delete_all
2012-07-17 03:35:34 +04:00
Notification.delete_all
2012-07-17 07:05:16 +04:00
Subscription.delete_all
2012-06-21 02:25:35 +04:00
{}.to_json
end
end
2012-07-17 03:35:34 +04:00
2012-07-27 03:12:30 +04:00
error BSON::InvalidObjectId do
2012-07-27 03:58:39 +04:00
error 400, "requested object not found"
end
2012-07-27 04:17:19 +04:00
error Mongoid::Errors::DocumentNotFound do
error 400, "requested object not found"
end
2012-07-27 03:58:39 +04:00
error ValueError do
error 400, env['sinatra.error'].message
2012-07-17 03:35:34 +04:00
end