lib/cachai.rb in cachai-0.0.2 vs lib/cachai.rb in cachai-0.0.3

- old
+ new

@@ -1,11 +1,11 @@ require 'sinatra/base' require 'redis' require 'json' require 'rake' -require_relative 'comment' +require_relative 'models' require_relative 'akismet' module Cachai class Middleware < Sinatra::Base @@ -21,11 +21,11 @@ def initialize(app, opts = nil) opts = opts || {} @domain = opts.delete(:domain) or raise 'Domain required.' - load_schema unless schema_loaded? + Cachai.load_db! redis_host = opts.delete(:redis_host) || 'localhost' @redis = Redis.new(:host => redis_host) if key = opts.delete(:akismet_key) @@ -35,13 +35,23 @@ end super(app) end + get '/pingbacks.?:format?' do + # TODO + end + + post '/pingbacks.?:format?' do + # TODO + end + get '/comments.?:format?' do check_domain!(params[:domain]) + @redis.del(redis_key(params[:path])) if params[:nocache] + # puts "Comments for: #{params[:domain]}#{params[:path]}" json_list = get_comments(params[:path]) if params[:callback] content_type 'application/javascript' @@ -60,21 +70,23 @@ permalink = 'http://' + data['domain'] + data['path'] halt(400, "No spam allowed") if is_spam?(data, permalink, request) attrs = { - :path => data['path'], :content => data['content'], :author_name => data['author_name'], :author_email => data['author_email'], - :author_url => data['author_url'] + :author_url => data['author_url'], + :parent_id => data['parent_id'], + :author_ip => request.ip } - comment = Comment.create!(attrs) + post = Post.find_by_path!(data['path']) + response = Response.create!(attrs.merge(:post_id => post.id)) @redis.del(redis_key(data['path'])) - json({ :status => 'ok', :comment => comment }) + json({ :status => 'ok', :comment => response }) rescue JSON::ParserError status 400 and json({ :error => 'Invalid JSON.' }) rescue ActiveRecord::RecordInvalid => e status 422 and json({ :error => e.message }) @@ -92,24 +104,10 @@ def prevent_cache cache_control :public, :no_cache, :no_store, :must_revalidate, :max_age => 0 # expires 1.year.ago end - def load_schema - require 'sinatra/activerecord/rake' - require_relative '../db/schema.rb' - end - - def schema_loaded? - Comment.first - true - rescue ActiveRecord::StatementInvalid => e - # SQLite3::SQLException => e - # return !e.message['no such table'] - false - end - def check_domain!(domain) halt(400, 'Invalid domain.') unless domain == @domain end def not_found(message = nil) @@ -123,14 +121,35 @@ def get_comments(path) key = redis_key(path) unless json_list = @redis.get(key) puts "Not cached. Getting from DB: #{path}" - json_list = Comment.where({ :path => path }).to_json + post = Post.find_by_path!(path) + json_list = get_and_sort_comments_for(post).to_json @redis.set(key, json_list) @redis.expire(key, 60 * 60) # one hour end json_list + end + + def get_and_sort_comments_for(post) + result = [] + top_level = post.responses.comment.top_level + nested = post.responses.comment.nested + + puts top_level.count + puts nested.count + + top_level.each_with_index do |comment, i| + obj = comment.as_json + children = nested.select do |nested| + nested.parent_id == comment.id + end + obj.merge!(:children => children) if children.any? + result.push(obj) + end + + result end def redis_key(path) "comments:#{@domain}:#{path}" end