orange_sms_api 0.1.1 → 0.1.2

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 68f4ace5adcfbc2bdca43d56d66d6c2edb63852afc004bc9f6a24756b9a7fc2b
4
- data.tar.gz: 2ad036d76702540ac5f12a2afd59f9be97e17422f2c724932fe4685292a1002b
3
+ metadata.gz: 04b48a1b630fe2954629e29933e7aa9f70b11dd33bbb4fb3271de3d2d081992c
4
+ data.tar.gz: 00b889d32c506fe40a5dcaf0816c227e74e1612123712080731c025e9744dd1d
5
5
  SHA512:
6
- metadata.gz: 11913fdd2e7c178c104ee5b20684435a649744640c08733226261b350208015cb1246a9bfed8248a4e4ef8958280b8e0adb2f6265802850f09b3f0ef9fbfc416
7
- data.tar.gz: f03b6c592e9550d6ea51c165cdbf6c6d1571cfd15d4f67cd42e2ee2b8e1e28a7859c083ef9a97c89bfe36cbf9ae656d0a3634e41e0d9a270bac01d2995914921
6
+ metadata.gz: 3d2495b83aea6da3494ca00fd946e371db1c43de9eeb642e30ca16ae8981d3a1b79dd7c90bb5eb0f7a4ebc63cc0a13dbc2ee54999dc1f92ac7c2632927a4cd34
7
+ data.tar.gz: 4d8a4b1b48033b36c66ecfcfc89940f76fcf5f50b244a7b43702174d1d35518c74dddccd8bca81a8411ac6378a7b675146822f22580a38245aecbe4b1a9e4b25
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- orange_sms_api (0.1.1)
4
+ orange_sms_api (0.1.2)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -11,6 +11,7 @@ module OrangeSmsApi
11
11
 
12
12
  puts "MESSAGE: #{message}"
13
13
  post(OrangeSmsApi.configuration.send_sms_endpoint, message)
14
+ #get_token
14
15
 
15
16
  end
16
17
  end
@@ -2,7 +2,8 @@ module OrangeSmsApi
2
2
  class Configuration
3
3
  attr_accessor :authorization_header,
4
4
  :base_url,
5
- :access_token,
5
+ :access_token,
6
+ :access_token_date,
6
7
  :send_sms_endpoint,
7
8
  :authentication_endpoint,
8
9
  :dev_phone_number
@@ -3,42 +3,81 @@ module OrangeSmsApi
3
3
  module HttpInterceptor
4
4
 
5
5
 
6
- class << self
7
- attr_accessor :api_base
6
+
7
+ def api_configured?
8
+ base_url = OrangeSmsApi.configuration.base_url
9
+ authorization_header = OrangeSmsApi.configuration.authorization_header
10
+ authentication_endpoint = OrangeSmsApi.configuration.authentication_endpoint
11
+
12
+ if base_url.present? && authorization_header.present? && authentication_endpoint.present?
13
+ return true
14
+ else
15
+ return false
16
+ end
8
17
  end
9
18
 
19
+ def access_token_validity?
20
+ access_token_date = OrangeSmsApi.configuration.access_token_date if OrangeSmsApi.configuration.access_token_date
21
+ current_date = Date.today
22
+ if access_token_date.present? && (current_date - access_token_date) <= 90
23
+
24
+ return true
25
+ else
26
+ return false
27
+
28
+ end
29
+ end
10
30
 
11
31
  def get_token
32
+ if api_configured?
33
+ unless access_token_validity?
34
+ # Inialize a new connection.
35
+ conn = Faraday.new(OrangeSmsApi.configuration.base_url,
36
+ ssl: {
37
+ ca_path: "/usr/lib/ssl/certs"}
38
+ )
12
39
 
13
- # Inialize a new connection.
14
- conn = Faraday.new(
15
- url: OrangeSmsApi.configuration.base_url,
16
- ssl: {
17
- ca_path: "/usr/lib/ssl/certs"}
18
- )
40
+ # Making a http post request
41
+ response = conn.post do |req|
42
+ req.url OrangeSmsApi.configuration.authentication_endpoint
43
+ req.headers['Content-Type'] = 'application/x-www-form-urlencoded'
44
+ req.headers['Authorization'] = OrangeSmsApi.configuration.authorization_header
45
+ req.body = "grant_type=client_credentials"
46
+ end
19
47
 
48
+ if response.status == 200
20
49
 
21
- # Making a http post request
22
- response = conn.post do |req|
23
- req.url = OrangeSmsApi.configuration.authenticate_endpoint
24
- req.headers['Content-Type'] = 'application/x-www-form-urlencoded'
25
- req.headers['Authorization'] = OrangeSmsApi.configuration.authorization
26
- end
50
+ response_body = JSON.parse(response.body)
51
+
52
+ OrangeSmsApi.configuration.access_token = response_body["access_token"]
53
+ OrangeSmsApi.configuration.access_token_date = Date.today
54
+
55
+ puts "LE TOKEN: #{OrangeSmsApi.configuration.access_token}"
56
+ puts "TOKEN DATE: #{OrangeSmsApi.configuration.access_token_date}"
27
57
 
28
- if response.status == 200
29
- response_body = response.body
30
- OrangeSmsApi.configuration.access_token = response_body.access_token
31
- end
58
+ elsif response.status == 401
32
59
 
60
+ puts "RESPONSE BODY: #{response_body}"
61
+ end
62
+
63
+ end
64
+ else
65
+ puts "API configuration error"
66
+ end
33
67
  end
34
68
 
69
+
70
+
71
+
35
72
  def post(endpoint, message)
36
73
 
37
74
 
38
- if OrangeSmsApi.configuration.base_url.present?
39
-
75
+ if api_configured?
76
+
77
+ get_token
78
+
40
79
  # Inialize a new connection.
41
- conn = Faraday.new(url: OrangeSmsApi.configuration.base_url)
80
+ conn = Faraday.new(OrangeSmsApi.configuration.base_url)
42
81
 
43
82
  payload = {:outboundSMSMessageRequest => {
44
83
  :address => "tel:+#{message[:recipient_phone_number]}" ,
@@ -50,20 +89,22 @@ module OrangeSmsApi
50
89
  }
51
90
 
52
91
  response = conn.post do |req|
53
- req.url = endpoint + "/tel%3A%2B#{OrangeSmsApi.configuration.dev_phone_number}/requests"
54
- req.headers['Content-Type'] = 'application/js'
92
+ req.url endpoint + "/tel%3A%2B#{OrangeSmsApi.configuration.dev_phone_number}/requests"
93
+ req.headers['Content-Type'] = 'application/json'
55
94
  req.headers['Authorization'] = 'Bearer ' + OrangeSmsApi.configuration.access_token
56
95
  req.body = payload.to_json
57
96
  end
58
97
 
59
98
 
60
99
  if response.status == 200
61
- puts "LA REPONSE DE LA REQUETTE EST: #{response.status}"
100
+ puts "LA REPONSE DE LA REQUETTE EST: #{response.body}"
101
+
62
102
 
63
103
  return response
64
- else
65
- puts "LA REPONSE DE LA REQUETTE EST: #{response.status}"
66
- get_token
104
+ elsif response.status == 401
105
+ puts "LA REPONSE DE LA REQUETTE EST: #{response.body}"
106
+
107
+ #get_token
67
108
  end
68
109
  else
69
110
  render text: "Invalid API Base!"
@@ -1,3 +1,3 @@
1
1
  module OrangeSmsApi
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: orange_sms_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - thkernel
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-06-24 00:00:00.000000000 Z
11
+ date: 2019-06-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday