sinch_sms 1.2 → 2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/sinch_sms.rb +82 -26
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7cb7097cc3d86618e96b1023b6d69806da25bdcb
|
4
|
+
data.tar.gz: b16bf148813e7c566bcee5e0606a6ce2ffa8bd49
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bf41767ed56ebf07871d8d767a709e44b52fad1fb079fc5875d2fa089458181b55f1420513166e0133e0ea36aef67eb1e03fce6f369d6e2f120f87b44421c554
|
7
|
+
data.tar.gz: 082768ef797c08862776eadea4b1f6d042e1fdd48d78c2010a7918b8d0b8d36ba1e33bb95b3b8da50973ef4ce651e36994ca36db46ee7f491bc861bf253278f9
|
data/lib/sinch_sms.rb
CHANGED
@@ -1,31 +1,87 @@
|
|
1
|
-
require
|
2
|
-
require
|
3
|
-
require
|
1
|
+
require "base64"
|
2
|
+
require "openssl"
|
3
|
+
require "time"
|
4
|
+
require "net/http"
|
5
|
+
require "uri"
|
6
|
+
require "json"
|
4
7
|
|
5
8
|
class SinchSms
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
9
|
+
def self.send(key, secret, message, to)
|
10
|
+
body = "{\"message\":\"#{message}\"}"
|
11
|
+
timestamp = Time.now.iso8601
|
12
|
+
authorization = auth(key, secret, "/v1/sms/#{to}", "POST", timestamp, body)
|
13
|
+
|
14
|
+
uri = URI.parse("https://messagingApi.sinch.com/v1/sms/" + to)
|
15
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
16
|
+
http.use_ssl = true
|
17
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
18
|
+
headers = {"content-type" => "application/json", "x-timestamp" => timestamp, "authorization" => authorization}
|
19
|
+
request = Net::HTTP::Post.new(uri.request_uri)
|
20
|
+
request.initialize_http_header(headers)
|
21
|
+
request.body = body
|
22
|
+
return JSON.parse(http.request(request).body)
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.status(key, secret, id)
|
26
|
+
timestamp = Time.now.iso8601
|
27
|
+
authorization = self.auth(key, secret, "/v1/message/status/#{id}", "GET", timestamp)
|
21
28
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
29
|
+
uri = URI.parse("https://messagingApi.sinch.com/v1/message/status/" + id)
|
30
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
31
|
+
http.use_ssl = true
|
32
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
33
|
+
headers = {"content-type" => "application/json", "x-timestamp" => timestamp, "authorization" => authorization}
|
34
|
+
request = Net::HTTP::Get.new(uri.request_uri)
|
35
|
+
request.initialize_http_header(headers)
|
28
36
|
|
29
|
-
|
37
|
+
return JSON.parse(http.request(request).body)
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
def self.auth(key, secret, path, http_verb, timestamp, body=nil)
|
42
|
+
scheme = "Application"
|
43
|
+
content_type = "application/json"
|
44
|
+
digest = OpenSSL::Digest.new('sha256')
|
45
|
+
canonicalized_headers = "x-timestamp:" + timestamp
|
46
|
+
|
47
|
+
if body
|
48
|
+
content_md5 = Base64.encode64(Digest::MD5.digest(body.encode("UTF-8"))).strip
|
49
|
+
else
|
50
|
+
content_md5 = ""
|
30
51
|
end
|
31
|
-
|
52
|
+
|
53
|
+
string_to_sign = http_verb + "\n" + content_md5 + "\n" + content_type + "\n" + canonicalized_headers + "\n" + path
|
54
|
+
signature = Base64.encode64(OpenSSL::HMAC.digest(digest, Base64.decode64(secret), string_to_sign.encode("UTF-8"))).strip
|
55
|
+
|
56
|
+
return "Application #{key}:#{signature}"
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
|