Class: MailSender

Inherits:
Object
  • Object
show all
Includes:
SuckerPunch::Job
Defined in:
lib/sinatra/extensions/mailsender.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#access_tokenObject (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

#expireObject (readonly)

Returns the value of attribute expire.



8
9
10
# File 'lib/sinatra/extensions/mailsender.rb', line 8

def expire
  @expire
end

#refresh_tokenObject (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_mailObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/sinatra/extensions/mailsender.rb', line 24

def _authorize_mail
  params = [ [ "grant_type", "password" ], [ "client_id", "MailApp" ], [ "client_secret", "MailAppPRD2018!" ] ]
  retry_times = 0
  begin
    response = _do_connect("https://comunicacion.hexacta.com:4443/apptoken",params)
  rescue StandardError => bang
    response = nil
  end
  until response.is_a?( Net::HTTPSuccess ) || retry_times > 10 do
    begin
      retry_times = retry_times + 1
      sleep(retry_times)
      response = _do_connect("https://comunicacion.hexacta.com:4443/apptoken",params)
    rescue StandardError => bang
    end
  end
  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"])
    return true
  else
    NotificationSender.instance.send_error(nil,'Authorize mail failed',response.body)
    return false
  end
end

#_build_map_values(mail_values) ⇒ Object



52
53
54
55
56
57
58
59
60
# File 'lib/sinatra/extensions/mailsender.rb', line 52

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



62
63
64
65
66
67
68
69
70
# File 'lib/sinatra/extensions/mailsender.rb', line 62

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}].Identifier", 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

Returns:

  • (Boolean)


72
73
74
# File 'lib/sinatra/extensions/mailsender.rb', line 72

def _expired?
  @expire.nil? || DateTime.now < @expire
end

#do_send(mail) ⇒ Object



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
# File 'lib/sinatra/extensions/mailsender.rb', line 77

def do_send(mail)
  authorized = _expired?? _authorize_mail : true

  if authorized
    params = [ [ "ApplicationCode", 2009 ], [ "Name", mail.template ], [ "Subject", mail.subject ], [ "EmailAddress", mail.from ] ]
    params = params + _build_map_values(mail.values) + _build_to_map(mail.to)

    retry_times = 0
    begin
      response = _do_connect("https://comunicacion.hexacta.com:4444/api/app/TemplateNotification/SendMail",params,@access_token)
    rescue StandardError => bang
      response = nil
    end
    until response.is_a?( Net::HTTPSuccess ) || retry_times > 10 do
      begin
        retry_times = retry_times + 1
        sleep(retry_times)
        response = _do_connect("https://comunicacion.hexacta.com:4444/api/app/TemplateNotification/SendMail",params,@access_token)
      rescue StandardError => bang
      end
    end
    if( !response.is_a?( Net::HTTPSuccess ) )
      NotificationSender.instance.send_error(nil,'Send mail failed',response.body)
    end
  end
end

#perform(mail) ⇒ Object

For async method call



109
110
111
# File 'lib/sinatra/extensions/mailsender.rb', line 109

def perform(mail)
  do_send(mail)
end

#send(mail) ⇒ Object



104
105
106
# File 'lib/sinatra/extensions/mailsender.rb', line 104

def send(mail)
  run(mail) #Async call
end