sinch_sms 1.2 → 2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/sinch_sms.rb +82 -26
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7120041fd731da04fc0a99f1ffba2404ebc9f71f
4
- data.tar.gz: 2d4955c592e411b1f4ab9fdf7bb45d23a742658b
3
+ metadata.gz: 7cb7097cc3d86618e96b1023b6d69806da25bdcb
4
+ data.tar.gz: b16bf148813e7c566bcee5e0606a6ce2ffa8bd49
5
5
  SHA512:
6
- metadata.gz: ff0ad320f2ff8b53b5d6ff28eb33b59da05cd399329f712e4f4a827430004c372f18fd0a6f774dfc3a250bb1b942bdfb3d19010eb3d5a787a6dbf536c7886aa5
7
- data.tar.gz: 692b847ba1a8f7fc40c649a472cc104776f6197e066f063bd4983107864a8231052cb8369654ebfd16a7081a1a37d7f89df6300755e67f9828cf9eec4f50fa3f
6
+ metadata.gz: bf41767ed56ebf07871d8d767a709e44b52fad1fb079fc5875d2fa089458181b55f1420513166e0133e0ea36aef67eb1e03fce6f369d6e2f120f87b44421c554
7
+ data.tar.gz: 082768ef797c08862776eadea4b1f6d042e1fdd48d78c2010a7918b8d0b8d36ba1e33bb95b3b8da50973ef4ce651e36994ca36db46ee7f491bc861bf253278f9
@@ -1,31 +1,87 @@
1
- require 'net/http'
2
- require 'net/https'
3
- require 'json'
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
- def self.send(key, secret, message, to, from=nil)
7
- http = Net::HTTP.new("messagingApi.sinch.com",443)
8
- req = Net::HTTP::Post.new("/v1/sms/" + to)
9
- http.use_ssl = true
10
- req.basic_auth "application:" + key, secret
11
- req["Content-Type"] = "application/json"
12
-
13
- if (from)
14
- req.body = {"Message" => message, "From" => from}.to_json
15
- else
16
- req.body = {"Message" => message}.to_json
17
- end
18
-
19
- return http.request(req).body
20
- end
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
- def self.status(key, secret, id)
23
- http = Net::HTTP.new("messagingApi.sinch.com",443)
24
- req = Net::HTTP::Get.new("/v1/message/status/" + id)
25
- http.use_ssl = true
26
- req.basic_auth "application:" + key, secret
27
- req["Content-Type"] = "application/json"
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
- return http.request(req).body
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
- end
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
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sinch_sms
3
3
  version: !ruby/object:Gem::Version
4
- version: '1.2'
4
+ version: '2.0'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jordan Hamel