From 2ec5f8e82aad84c0807fe5a181fae974271030e6 Mon Sep 17 00:00:00 2001 From: yafeilee Date: Tue, 1 Apr 2014 17:09:10 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=A2=E9=98=85=E5=8A=9F=E8=83=BD,=20?= =?UTF-8?q?=E7=94=B1=E4=BA=8E=20sendcloud=20=E7=9A=84=E9=97=AE=E9=A2=98?= =?UTF-8?q?=E6=9C=AA=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Gemfile | 3 +++ app/models/comment.rb | 2 +- app/models/post.rb | 6 ++++++ app/models/subscribe.rb | 11 +++++++++++ app/workers/new_post_worker.rb | 28 ++++++++++++++++++++++++++++ spec/factories/subscribes.rb | 8 ++++++++ spec/models/subscribe_spec.rb | 5 +++++ 7 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 app/models/subscribe.rb create mode 100644 app/workers/new_post_worker.rb create mode 100644 spec/factories/subscribes.rb create mode 100644 spec/models/subscribe_spec.rb diff --git a/Gemfile b/Gemfile index 4506faf..2d3e4b4 100644 --- a/Gemfile +++ b/Gemfile @@ -26,6 +26,8 @@ gem 'nokogiri' gem 'angularjs-rails' gem 'figaro' gem 'rqrcode-with-patches', require: 'rqrcode' +gem 'sidekiq' +gem 'rest-client' group :development do gem 'quiet_assets' @@ -39,6 +41,7 @@ group :test do gem 'capybara' gem 'mongoid-rspec', :require => false gem 'database_cleaner' + gem 'rspec-sidekiq' end group :test, :development do diff --git a/app/models/comment.rb b/app/models/comment.rb index 726081f..563add8 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -9,7 +9,7 @@ class Comment belongs_to :post validates :name, presence: true - validates :email, confirmation: true,:format => /@/ + validates :email, presence: true,:format => /@/ validates :content, presence: true validates_presence_of :post_id end diff --git a/app/models/post.rb b/app/models/post.rb index 1a15a6b..2e47256 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -22,6 +22,12 @@ class Post validates :content, :presence=>true, :length => { :minimum=> 30 } validates :type, :presence=>true, :inclusion => { :in => [ TECH, LIFE, CREATOR ] } + after_create do + unless ENV['SENDCLOUD_USER'] + NewPostWorker.perform_async(self.title, Subscribe.subscribe_list) + end + end + def content_html self.class.render_html(self.content) end diff --git a/app/models/subscribe.rb b/app/models/subscribe.rb new file mode 100644 index 0000000..0128a44 --- /dev/null +++ b/app/models/subscribe.rb @@ -0,0 +1,11 @@ +class Subscribe + include Mongoid::Document + field :email, type: String + field :enable, type: Mongoid::Boolean, default: true + + validates :email, presence: true, format: /@/ + + def self.subscribe_list + Subscribe.all.map(&:email).join(";") + end +end diff --git a/app/workers/new_post_worker.rb b/app/workers/new_post_worker.rb new file mode 100644 index 0000000..b847109 --- /dev/null +++ b/app/workers/new_post_worker.rb @@ -0,0 +1,28 @@ +class NewPostWorker + include Sidekiq::Worker + + def perform(title, to) + send_mail(title, to) + end + + def send_mail(title, to) + response = RestClient.post "https://sendcloud.sohu.com/webapi/mail.send.xml", + { + :api_user => "postmaster@#{ENV['SENDCLOUD_USER']}.sendcloud.org", + :api_key => ENV['SENDCLOUD_PASSWORD'], + :from => ENV['SENDCLOUD_FROM'], + :fromname => ENV['SENDCLOUD_FROMNAME'], + :to => to, + :subject => "#{ENV['SITE_NAME']} 又写了新博客" + :html => <<-EOF +

hi, 我是李亚飞, 很高兴告诉你:

+ +

#{ENV['SITE_NAME']} 新博客到了: #{title}

+ +

具体内容请访问: http://yafeilee.me

+ EOF + } + return response + end +end + diff --git a/spec/factories/subscribes.rb b/spec/factories/subscribes.rb new file mode 100644 index 0000000..f517ea7 --- /dev/null +++ b/spec/factories/subscribes.rb @@ -0,0 +1,8 @@ +# Read about factories at https://github.com/thoughtbot/factory_girl + +FactoryGirl.define do + factory :subscribe do + email "MyString" + enable false + end +end diff --git a/spec/models/subscribe_spec.rb b/spec/models/subscribe_spec.rb new file mode 100644 index 0000000..0df4fbb --- /dev/null +++ b/spec/models/subscribe_spec.rb @@ -0,0 +1,5 @@ +require 'spec_helper' + +describe Subscribe do + pending "add some examples to (or delete) #{__FILE__}" +end