Module: OrangeSmsApi::HttpInterceptor
- Included in:
- Client
- Defined in:
- lib/orange_sms_api/interceptor.rb
Instance Method Summary collapse
- #access_token_validity? ⇒ Boolean
- #api_configured? ⇒ Boolean
- #get_token ⇒ Object
- #post(endpoint, message) ⇒ Object
Instance Method Details
#access_token_validity? ⇒ Boolean
19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/orange_sms_api/interceptor.rb', line 19 def access_token_validity? access_token_date = OrangeSmsApi.configuration.access_token_date if OrangeSmsApi.configuration.access_token_date current_date = Date.today if access_token_date.present? && (current_date - access_token_date) <= 90 return true else return false end end |
#api_configured? ⇒ Boolean
7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/orange_sms_api/interceptor.rb', line 7 def api_configured? base_url = OrangeSmsApi.configuration.base_url = OrangeSmsApi.configuration. authentication_endpoint = OrangeSmsApi.configuration.authentication_endpoint if base_url.present? && .present? && authentication_endpoint.present? return true else return false end end |
#get_token ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/orange_sms_api/interceptor.rb', line 31 def get_token if api_configured? unless access_token_validity? # Inialize a new connection. conn = Faraday.new(OrangeSmsApi.configuration.base_url, ssl: { ca_path: "/usr/lib/ssl/certs"} ) # Making a http post request response = conn.post do |req| req.url OrangeSmsApi.configuration.authentication_endpoint req.headers['Content-Type'] = 'application/x-www-form-urlencoded' req.headers['Authorization'] = OrangeSmsApi.configuration. req.body = "grant_type=client_credentials" end if response.status == 200 response_body = JSON.parse(response.body) OrangeSmsApi.configuration.access_token = response_body["access_token"] OrangeSmsApi.configuration.access_token_date = Date.today puts "LE TOKEN: #{OrangeSmsApi.configuration.access_token}" puts "TOKEN DATE: #{OrangeSmsApi.configuration.access_token_date}" elsif response.status == 401 puts "RESPONSE BODY: #{response_body}" end end else puts "API configuration error" end end |
#post(endpoint, message) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/orange_sms_api/interceptor.rb', line 72 def post(endpoint, ) if api_configured? get_token # Inialize a new connection. conn = Faraday.new(OrangeSmsApi.configuration.base_url) payload = {:outboundSMSMessageRequest => { :address => "tel:+#{[:recipient_phone_number]}" , :senderAddress => "tel:+#{OrangeSmsApi.configuration.dev_phone_number}", :outboundSMSTextMessage => { :message => [:body] } } } response = conn.post do |req| req.url endpoint + "/tel%3A%2B#{OrangeSmsApi.configuration.dev_phone_number}/requests" req.headers['Content-Type'] = 'application/json' req.headers['Authorization'] = 'Bearer ' + OrangeSmsApi.configuration.access_token req.body = payload.to_json end if response.status == 200 puts "LA REPONSE DE LA REQUETTE EST: #{response.body}" return response elsif response.status == 401 puts "LA REPONSE DE LA REQUETTE EST: #{response.body}" #get_token end else render text: "Invalid API Base!" end end |