项目地址:https://github.com/lyfeyaj/smart_sms
从公司项目中抽出来的 gem, 方便自己以后的项目使用,也希望能帮到大家。
在 Gemfile
里面添加:
gem 'smart_sms'
然后
bundle
rails g smart_sms:config
# 配置一下api_key, `config/initializers/smart_sms_config.rb`
SmartSMS.configure do |config|
config.api_key = 'your_api_key'
end
最后
# 在现有的 model 中声明 `has_sms_verification`
class User < ActiveRecord::Base
has_sms_verification # 默认使用字段 :phone, :verified_at, 可自定义指定, 例如, :mobile, :confirmed_at
end
于是
# 发送短信验证码
user.deliver # 将会生成一个随机的验证码发送至手机
# 查询历史短信记录
user.messages
# 查询是否已经验证
user.verified?
# 校验验证码
user.verify! '123456'
# 查询最新的一条有效短信记录
user.latest_message
设置 api_key
SmartSMS.configure { |c| c.api_key = 'your_api_key' }
发送短信
SmartSMS.deliver 13522948742, 'SmartSMS WOW!'
https://github.com/lyfeyaj/smart_sms
比较了很多家短信服务商,云片网络的服务比较完善,可以自定义短信模板,查询信息等,而且短信延迟很小,基本上 5s 左右能发送到手机。所以这个 gem 封装了云片网的所有短信功能,有兴趣的童鞋可以看下他们的API 文档
目前这个 gem 已经在公司项目的生产环境中,一切运行良好,放心使用 ^_^