class Cachai::Middleware

Public Class Methods

new(app, opts = {}) click to toggle source
Calls superclass method
# File lib/cachai.rb, line 25
def initialize(app, opts = {})
  domain       = opts.delete(:domain) or raise 'Domain required.'
  redis_host   = opts.delete(:redis_host) || 'localhost'
  @duration    = opts.delete(:duration)
  @blocked_ips = opts.delete(:blocked_ips) || []

  Cachai.boot(domain, redis_host)

  if key = opts.delete(:akismet_key)
    @akismet = Akismet.new(:api_key => key, :blog => "http://#{Cachai.domain}")
  else
    puts "No Akismet key found! Will not check comments for spam."
  end

  if mailgun_opts = opts.delete(:mailgun)
    require 'rest-client'
    @mailgun_domain  = mailgun_opts[:domain]
    @mailgun_api_key = mailgun_opts[:api_key]
    @recipient = opts.delete(:recipient) or raise "No recipient set!"
  end

  super(app)
end

Private Instance Methods

check_domain!(domain) click to toggle source

def set_cache(timestamp)

return if timestamp.nil?
last_modified timestamp
cache_control :public, :must_revalidate, :max_age => 60

end

def prevent_cache

cache_control :public, :no_cache, :no_store, :must_revalidate, :max_age => 0
# expires 1.year.ago

end

# File lib/cachai.rb, line 145
def check_domain!(domain)
  halt(400, 'Invalid domain.') unless domain == Cachai.domain
end
is_blocked?(user_ip) click to toggle source
# File lib/cachai.rb, line 158
def is_blocked?(user_ip)
  @blocked_ips.include?(user_ip)
end
is_spam?(data, link, request) click to toggle source
# File lib/cachai.rb, line 162
def is_spam?(data, link, request)
  return false unless @akismet
  # return true if blacklisted?(name, email, content)

  comment = {
    :user_ip              => request.ip,
    :referrer             => request.referrer,
    :user_agent           => request.user_agent,
    :permalink            => link,
    :blog                 => 'http://' + data['domain'],
    :comment_type         => 'comment',
    :comment_content      => data['content'],
    :comment_author       => data['author_name'],
    :comment_author_url   => data['author_url'],
    :comment_author_email => data['author_email']
  }

  if resp = @akismet.check_comment(comment)
    # puts resp.inspect
    return resp[:spam]
  end

  false
end
json(obj) click to toggle source
# File lib/cachai.rb, line 153
def json(obj)
  content_type 'application/json'
  return obj.is_a?(String) ? obj : obj.to_json
end
not_found(message = nil) click to toggle source
# File lib/cachai.rb, line 149
def not_found(message = nil)
  halt(404, message || 'Not found.')
end
notify_new_response_to_admin(response, path) click to toggle source
# File lib/cachai.rb, line 187
def notify_new_response_to_admin(response, path)
  subject = "Nuevo comentario de #{response.author_name} at #{path}"
  send_email(content: response.content, to: @recipient, path: path, subject: subject)
end
notify_new_response_to_parent(response, path) click to toggle source
# File lib/cachai.rb, line 192
def notify_new_response_to_parent(response, path)
  subject = "Respuesta de #{response.author_name} a tu comentario en #{path}"
  send_email(content: response.content, to: response.parent.author_email, path: path, subject: subject)
end
send_email(data) click to toggle source
# File lib/cachai.rb, line 197
def send_email(data)
  RestClient.post "https://api:#{@mailgun_api_key}"\
    "@api.mailgun.net/v3/#{@mailgun_domain}/messages",
    :from    => data[:from] || 'comments@' + Cachai.domain,
    :to      => data[:to],
    :subject => data[:subject],
    :text    => "#{data[:content]}\n\n--\nhttp://#{Cachai.domain}#{data[:path]}"
# rescue => e
#  puts "MAIL ERROR: #{e.message}"
end