sendcloud-sms 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/main.rb +65 -0
  3. metadata +59 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ff4a98adbbb4e76271226a54f49744bed450092d
4
+ data.tar.gz: 790152493dccb572c61260806b555210f428a34e
5
+ SHA512:
6
+ metadata.gz: a6885f4f029bcf6527693c747d70ea6a20f176f22573ea0eb1d9983a41db35db0c9b7c1c001cccdae1f7a9efcf2ab802d03d24ab962f5880e9d1b5130fa4d5d2
7
+ data.tar.gz: 4c3e061f48e210b04ad12f05757c8cfd2df35cc08cec5c9ba7ed476f8af25242840c5fd1cd65f178e6b9bb029bc76c99a2fe1d4042d9bc9084c2b37531fce78c
data/lib/main.rb ADDED
@@ -0,0 +1,65 @@
1
+ require 'digest'
2
+ require 'rest-client'
3
+ require 'json'
4
+ require 'yaml'
5
+
6
+ module SendCloud
7
+ class SMS
8
+
9
+ def self.load!(file, environment)
10
+ config = YAML.load_file(file)
11
+ self.auth(config[environment.to_s]['user'], config[environment.to_s]['api_key'])
12
+ end
13
+
14
+ def self.auth(user, api_key)
15
+ @user = user
16
+ @api_key = api_key
17
+ end
18
+
19
+ def self.sign(template, phone, vars)
20
+ param_str = "#{@api_key}&"
21
+ {
22
+ smsUser: @user,
23
+ templateId: template,
24
+ msgType: 0,
25
+ phone: (phone.is_a?Array) ? phone.join(',') : phone,
26
+ vars: vars.to_json
27
+ }.sort {|a, b| a.to_s <=> b.to_s}.map { |item| param_str << "#{item[0]}=#{item[1]}&" }
28
+ param_str << @api_key
29
+ Digest::MD5.new.update(param_str)
30
+ end
31
+
32
+ def self.sign_voice(phone, code)
33
+ param_str = "#{@api_key}&"
34
+ {
35
+ smsUser: @user,
36
+ phone: phone,
37
+ code: code
38
+ }.sort {|a, b| a.to_s <=> b.to_s}.map { |item| param_str << "#{item[0]}=#{item[1]}&" }
39
+ param_str << @api_key
40
+ Digest::MD5.new.update(param_str)
41
+ end
42
+
43
+ def self.send(template, phone, vars)
44
+ signature = sign(template, phone, vars)
45
+ response = RestClient.post 'http://sendcloud.sohu.com/smsapi/send?',
46
+ smsUser: @user,
47
+ templateId: template,
48
+ msgType: 0,
49
+ phone: (phone.is_a?Array) ? phone.join(',') : phone,
50
+ vars: vars.to_json,
51
+ signature: signature
52
+ JSON.parse(response.to_s)['statusCode']
53
+ end
54
+
55
+ def self.send_voice(phone, code)
56
+ signature = sign_voice(phone, code)
57
+ response = RestClient.post 'http://sendcloud.sohu.com/smsapi/sendVoice?',
58
+ smsUser: @user,
59
+ phone: phone,
60
+ code: code,
61
+ signature: signature
62
+ JSON.parse(response.to_s)['statusCode']
63
+ end
64
+ end
65
+ end
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sendcloud-sms
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - HeckPsi Lab
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-07-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rest-client
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: An unofficial gem to send SMS with SendCloud API.
28
+ email: business@heckpsi.com
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - lib/main.rb
34
+ homepage: https://github.com/heckpsi-lab/sendcloud-sms
35
+ licenses:
36
+ - MIT
37
+ metadata: {}
38
+ post_install_message:
39
+ rdoc_options: []
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ requirements: []
53
+ rubyforge_project:
54
+ rubygems_version: 2.4.8
55
+ signing_key:
56
+ specification_version: 4
57
+ summary: An unofficial gem to send SMS with SendCloud API.
58
+ test_files: []
59
+ has_rdoc: