lib/cachai.rb in cachai-0.1.2 vs lib/cachai.rb in cachai-0.1.3
- old
+ new
@@ -32,13 +32,14 @@
@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)
- require 'sendgrid-ruby'
- @sendgrid = SendGrid::Client.new(sendgrid_opts)
+ 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
@@ -86,11 +87,11 @@
}
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 @sendgrid
+ notify_new_response(response, data['path']) if @recipient
json({ :status => 'ok', :comment => response })
rescue JSON::ParserError
status 400 and json({ :error => 'Invalid JSON.' })
@@ -190,20 +191,17 @@
false
end
def notify_new_response(response, path)
- mail = SendGrid::Mail.new do |m|
- m.to = @recipient
- m.from = 'comments@' + Cachai.domain
- m.reply_to = response.author_email
- m.subject = "New comment from #{response.author_name} at #{path}"
- m.text = "#{response.content}\n\n--\nhttp://#{Cachai.domain}/#{path}"
- end
-
- puts @sendgrid.send(mail)
- rescue SendGrid::Exception => e
- puts e.inspect
+ RestClient.post "https://api:#{@mailgun_api_key}"\
+ "@api.mailgun.net/v3/#{@mailgun_domain}/messages",
+ :from => 'comments@' + Cachai.domain
+ :to => @recipient,
+ :subject => "New comment from #{response.author_name} at #{path}",
+ :text => "#{response.content}\n\n--\nhttp://#{Cachai.domain}/#{path}"
+ rescue => e
+ puts "MAIL ERROR: #{e.message}"
end
end
class Admin < Sinatra::Base