Class: MailSender
- Inherits:
-
Object
- Object
- MailSender
- Includes:
- SuckerPunch::Job
- Defined in:
- lib/sinatra/extensions/mailsender.rb
Instance Attribute Summary collapse
-
#access_token ⇒ Object
readonly
Returns the value of attribute access_token.
-
#expire ⇒ Object
readonly
Returns the value of attribute expire.
-
#refresh_token ⇒ Object
readonly
Returns the value of attribute refresh_token.
Instance Method Summary collapse
- #_authorize_mail ⇒ Object
- #_build_map_values(mail_values) ⇒ Object
- #_build_to_map(to_map) ⇒ Object
- #_do_connect(uri, form = {}, token = nil) ⇒ Object
- #_expired? ⇒ Boolean
- #do_send(mail) ⇒ Object
-
#perform(mail) ⇒ Object
For async method call.
- #send(mail) ⇒ Object
Instance Attribute Details
#access_token ⇒ Object (readonly)
Returns the value of attribute access_token.
8 9 10 |
# File 'lib/sinatra/extensions/mailsender.rb', line 8 def access_token @access_token end |
#expire ⇒ Object (readonly)
Returns the value of attribute expire.
8 9 10 |
# File 'lib/sinatra/extensions/mailsender.rb', line 8 def expire @expire end |
#refresh_token ⇒ Object (readonly)
Returns the value of attribute refresh_token.
8 9 10 |
# File 'lib/sinatra/extensions/mailsender.rb', line 8 def refresh_token @refresh_token end |
Instance Method Details
#_authorize_mail ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/sinatra/extensions/mailsender.rb', line 23 def params = [ [ "grant_type", "password" ], [ "client_id", "MailApp" ], [ "client_secret", "MailAppPRD2018!" ] ] response = _do_connect("https://comunicacion.hexacta.com:4443/apptoken",params) if( response.is_a?( Net::HTTPSuccess ) ) token = JSON.parse(response.body) @access_token = token["access_token"] @refresh_token = token["refresh_token"] @expire = DateTime.parse(token[".expires"]) else NotificationSender.instance.send_error(nil,'Authorize mail failed',response.body) end end |
#_build_map_values(mail_values) ⇒ Object
36 37 38 39 40 41 42 43 44 |
# File 'lib/sinatra/extensions/mailsender.rb', line 36 def _build_map_values(mail_values) values = [] for key in mail_values.keys index = mail_values.keys.index(key) values << ["MailValues[#{index}].Key", key] values << ["MailValues[#{index}].Value", mail_values[key]] end values end |
#_build_to_map(to_map) ⇒ Object
46 47 48 49 50 51 52 53 54 |
# File 'lib/sinatra/extensions/mailsender.rb', line 46 def _build_to_map(to_map) values = [] for key in to_map.keys index = to_map.keys.index(key) values << ["ResourcesRequest[#{index}].TypeGroup", to_map[key]] values << ["ResourcesRequest[#{index}].Name", key] end values end |
#_do_connect(uri, form = {}, token = nil) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/sinatra/extensions/mailsender.rb', line 10 def _do_connect(uri, form={},token=nil) url = URI.parse("#{uri}") http = Net::HTTP.new(url.host, url.port) http.read_timeout = 1000 http.use_ssl = (url.scheme == 'https') http.verify_mode = OpenSSL::SSL::VERIFY_NONE header = { 'Content-Type' => 'application/x-www-form-urlencoded' } header["Authorization"] = "Bearer #{token}" unless token.nil? request = Net::HTTP::Post.new(url, header) request.form_data = form http.start { |http| http.request(request) } end |
#_expired? ⇒ Boolean
56 57 58 |
# File 'lib/sinatra/extensions/mailsender.rb', line 56 def _expired? expire.nil? || DateTime.now < expire end |
#do_send(mail) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/sinatra/extensions/mailsender.rb', line 60 def do_send(mail) begin if _expired? end params = [ [ "ApplicationCode", 2009 ], [ "Name", mail.template ], [ "Subject", mail.subject ], [ "EmailAddress", mail.from ] ] params = params + _build_map_values(mail.values) + _build_to_map(mail.to) response = _do_connect("https://comunicacion.hexacta.com:4444/api/app/TemplateNotification/SendMail",params,@access_token) if( !response.is_a?( Net::HTTPSuccess ) ) NotificationSender.instance.send_error(nil,'Send mail failed',response.body) end rescue StandardError => error = error.backtrace.join(','); NotificationSender.instance.send_error(nil,"Mail send error",) end end |
#perform(mail) ⇒ Object
For async method call
85 86 87 |
# File 'lib/sinatra/extensions/mailsender.rb', line 85 def perform(mail) do_send(mail) end |
#send(mail) ⇒ Object
80 81 82 |
# File 'lib/sinatra/extensions/mailsender.rb', line 80 def send(mail) run(mail) #Async call end |