lib/cachai.rb in cachai-0.0.7 vs lib/cachai.rb in cachai-0.1.0

- old
+ new

@@ -19,21 +19,20 @@ # use Rack::Static, :urls => %w(/css /img /js /favicon.ico), :root => 'public' use ActiveRecord::ConnectionAdapters::ConnectionManagement CACHE_MINUTES = 10 * 60 # 10 minutes - def initialize(app, opts = nil) - opts = opts || {} + def initialize(app, opts = {}) domain = opts.delete(:domain) or raise 'Domain required.' redis_host = opts.delete(:redis_host) || 'localhost' Cachai.domain = domain Cachai.cache = Redis.new(:host => redis_host) Cachai.load_db! if key = opts.delete(:akismet_key) - @akismet = Akismet.new(:api_key => key, :blog => "http://#{domain}") + @akismet = Akismet.new(:api_key => key, :blog => "http://#{Cachai.domain}") else puts "No Akismet key found! Will not check comments for spam." end if sendgrid_opts = opts.delete(:sendgrid) @@ -202,9 +201,69 @@ end puts @sendgrid.send(mail) rescue SendGrid::Exception => e puts e.inspect + end + + end + + class Admin < Sinatra::Base + + # set :public_folder, File.join(settings.root, '..', 'public') + # set :views, File.join(settings.root, 'views') + # use Rack::Static, :urls => %w(/css /img /js /favicon.ico), :root => 'public' + enable :method_override + use ActiveRecord::ConnectionAdapters::ConnectionManagement + + def initialize(opts = {}) + require 'tilt/erb' + Cachai.load_db! + super + end + + get '/' do + redirect to('/posts') + end + + get '/posts' do + @posts = Cachai::Post.all.order(:id => :desc) + deliver @posts, :posts + end + + get '/posts/:id' do + @post = Cachai::Post.find(params[:id]) + @comments = @post.responses.comment + deliver @comments, :comments + end + + get '/comments' do + @comments = Cachai::Response.all.order(:created_at => :desc) + deliver @comments, :comments + end + + get '/comments/:id' do + @comment = Cachai::Response.find(params[:id]) + deliver @comment, :comment + end + + put '/comments/:id' do + @comment = Cachai::Response.find(params[:id]) + @comment.update_attributes(params[:comment]) + redirect back + end + + def deliver(obj, view) + if request.xhr? + json(obj) + else + erb view + end + end + + def json(obj) + content_type 'application/json' + return obj.is_a?(String) ? obj : obj.to_json end end end