diff --git a/.gitignore b/.gitignore index d87d4be..223f6f8 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,18 @@ spec/reports test/tmp test/version_tmp tmp + +*.swo +*.swp + +# Ignore IDE +.idea + +# Ignore bundler config +.bundle + +#mac os +.DS_Store +._.DS_Store + +.project diff --git a/bin/console b/bin/console new file mode 100755 index 0000000..a6a475c --- /dev/null +++ b/bin/console @@ -0,0 +1,11 @@ +#!/usr/bin/env ruby + +require "bundler/setup" +require "china_sms" + +# You can add fixtures and/or initialization code here to make experimenting +# with your gem easier. You can also use a different console, if you like. + +require 'irb' + +IRB.start diff --git a/china_sms.gemspec b/china_sms.gemspec index 65afb31..7290b3b 100644 --- a/china_sms.gemspec +++ b/china_sms.gemspec @@ -23,5 +23,6 @@ Gem::Specification.new do |spec| spec.add_development_dependency "bundler", "~> 1.2" spec.add_development_dependency "rake" spec.add_development_dependency "rspec" + spec.add_development_dependency "rspec-its" spec.add_development_dependency "webmock" end diff --git a/lib/china_sms/service/sendcloud.rb b/lib/china_sms/service/sendcloud.rb new file mode 100644 index 0000000..1d11c00 --- /dev/null +++ b/lib/china_sms/service/sendcloud.rb @@ -0,0 +1,96 @@ +# encoding: utf-8 +module ChinaSMS + module Service + module Sendcloud + extend self + + URL = 'http://www.sendcloud.net/smsapi/send'.freeze + + MESSAGES = { + '500' => '发送失败, 手机空号', + '510' => '发送失败, 手机停机', + '550' => '发送失败, 该模板内容被投诉', + '580' => '发送失败, 手机关机', + '590' => '发送失败, 其他原因', + '200' => '请求成功', + '311' => '部分号码请求成功', + '312' => '全部请求失败', + '411' => '手机号不能为空', + '412' => '手机号格式错误', + '413' => '有重复的手机号', + '421' => '签名参数错误', + '422' => '签名错误', + '431' => '模板不存在', + '432' => '模板未提交审核或者未审核通过', + '433' => '模板ID不能为空', + '441' => '替换变量格式错误', + '451' => '定时发送时间的格式错误', + '452' => '定时发送时间早于服务器时间, 时间已过去', + '461' => '时间戳无效, 与服务器时间间隔大于6秒', + '471' => 'smsUser不存在', + '472' => 'smsUser不能为空', + '473' => '没有权限, 免费用户不能发送短信', + '474' => '用户不存在', + '481' => '手机号和替换变量不能为空', + '482' => '手机号和替换变量格式错误', + '483' => '替换变量长度不能超过32个字符', + '496' => '一分钟内给当前手机号发信太多', + '498' => '一天内给当前手机号发信太多', + '499' => '账户额度不够', + '501' => '服务器异常', + '601' => '你没有权限访问' + } + + module MSG_TYPE + SMS = 0 + + MMS = 1 + end + + def get_signature(params) + param_str = [] + password = params.delete 'password' + params.keys.sort.each do |key| + param_str << "#{key}=#{params[key]}" + end + + sign_str = "#{password}&#{param_str.join('&')}&#{password}" + + Digest::MD5.hexdigest sign_str + end + + def to(phone, content = '', options = {}) + options[:type] ||= MSG_TYPE::SMS + + params = { + 'phone' => phone, + 'vars' => options[:content].to_json, + 'msgType' => options[:type], + 'smsUser' => options[:username], + 'templateId' => options[:template_id], + 'password' => options[:password] + } + + params.merge! 'signature' => get_signature(params) + + puts "Request params: #{params}" + + res = Net::HTTP.post_form(URI.parse(URL), params) + + puts "Response Body: #{res.body}" + + result JSON.parse(res.body) + end + + def result(response) + code = response['statusCode'] + + { + success: (code == 200), + code: code, + message: response['message'] + } + end + end + end +end diff --git a/spec/service/sendcloud_spec.rb b/spec/service/sendcloud_spec.rb new file mode 100644 index 0000000..08c5400 --- /dev/null +++ b/spec/service/sendcloud_spec.rb @@ -0,0 +1,38 @@ +# encoding: utf-8 +require 'spec_helper' + +describe "Sendcloud" do + let(:username) { 'user' } + let(:password) { 'xxxxtemrejmijfeijixwewe' } + + describe "#to" do + let(:url) { 'http://www.sendcloud.net/smsapi/send' } + let(:template_id) { 3126 } + let(:content) { {} } + subject { ChinaSMS::Service::Sendcloud.to phone, '', {content: content, template_id: template_id , username: username, password: password} } + + describe 'single phone' do + let(:phone) { '13601647905' } + + before do + body = {"phone"=> phone, "vars"=> content.to_json, "msgType"=> '0', "smsUser"=> username, "templateId"=>template_id, "signature"=>"16f795fd3514ed89bdd9fe577fb2601a" } + stub_request(:post, url). + with(body: URI.encode_www_form(body)). + to_return(body: "{\"info\":{\"successCount\":1,\"smsIds\":[\"1478579653941_49236_566_3126_clrsg5$13601647905\"]},\"statusCode\":200,\"message\":\"请求成功\",\"result\":true}") + end + + its([:success]) { should eql true } + its([:code]) { should eql 200 } + its([:message]) { should eql "请求成功" } + end + end + + describe "#get_signature" do + let(:phone) { "13601647905" } + let(:content) { {}.to_json } + let(:params) { {"phone"=> phone, "vars"=> content, "msgType"=>0, "smsUser"=> username, "templateId"=>3126, 'password'=> password} } + subject { ChinaSMS::Service::Sendcloud.get_signature params } + + it { should eql "16f795fd3514ed89bdd9fe577fb2601a" } + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index c5adbe2..7e831db 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -4,6 +4,7 @@ require 'webmock/rspec' require 'china_sms' +require 'rspec/its' #WebMock.allow_net_connect! RSpec.configure do |config|