lib/cachai.rb in cachai-0.2.3 vs lib/cachai.rb in cachai-0.2.4
- old
+ new
@@ -23,10 +23,11 @@
end
def initialize(app, opts = {})
domain = opts.delete(:domain) or raise 'Domain required.'
redis_host = opts.delete(:redis_host) || 'localhost'
+ @duration = opts.delete(:duration)
Cachai.boot(domain, redis_host)
if key = opts.delete(:akismet_key)
@akismet = Akismet.new(:api_key => key, :blog => "http://#{Cachai.domain}")
@@ -76,29 +77,38 @@
data['protocol'] = url.schema
data['path'] = url.path
end
check_domain!(data['domain'])
- halt(400, "Missing params") if data['protocol'].blank? or data['path'].blank?
+ if data['protocol'].blank? or data['path'].blank?
+ return halt(422, "Missing params.")
+ end
- headers['Access-Control-Allow-Origin'] = data['protocol'] + '//' + data['domain']
+ post = Post.find_or_create_by_path(data['path'])
+ if @duration && post.created_at < @duration.days.ago
+ return halt(400, "Comments closed.")
+ end
+
permalink = 'http://' + data['domain'] + data['path']
- halt(400, "No spam allowed") if is_spam?(data, permalink, request)
+ if is_spam?(data, permalink, request)
+ return halt(401, "No spam allowed.")
+ end
+
attrs = {
:content => data['content'],
:author_name => data['author_name'],
:author_email => data['author_email'],
:author_url => data['author_url'],
:parent_id => data['parent_id'].to_i,
:author_ip => request.ip
}
- post = Post.find_or_create_by_path(data['path'])
response = Response.create!(attrs.merge(:post_id => post.id))
Cachai.clear_cache(data['path'])
notify_new_response(response, data['path']) if @recipient
+ headers['Access-Control-Allow-Origin'] = data['protocol'] + '//' + data['domain']
json({ :status => 'ok', :comment => response })
rescue JSON::ParserError
status 400 and json({ :error => 'Invalid JSON.' })
rescue ActiveRecord::RecordInvalid => e