sinatra-hexacta 0.1.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4cf0915f37eb2114c97ebb4b7ff954a90e3c5dac391fe217d3a75053dc228220
4
- data.tar.gz: 2f0a31fcc9b14ddf883c633fc6a1ba7926cbc61bb3549f167871fa494b2ad2f6
3
+ metadata.gz: b224f086c14afb408224b09ae099c98b916a9e7e306bfa378108cce5d69a4ff2
4
+ data.tar.gz: c213e3f241ba7cfc6a99dbc26f6324454e76e014cadd98e442bf916c2d4fd94b
5
5
  SHA512:
6
- metadata.gz: a5bfbf816b5b2f5def09972de96976262de368922ac80a858d61610f17cd91c09f35b41cff946fb8da759be407701c7b4489d60cf44700e19fa2843061a766bf
7
- data.tar.gz: 6bda67520e9f593a7524703e765066af1738b199dfa23870f8ab14ff9d79244e83bf533aaf09b658d04d0a26c82894ebde1f6cd61228ef51ed9043196ec9da5f
6
+ metadata.gz: 732951c6ab91c8d3293ab5b1110a4de7ed423675d694ea41c3208f5ef85dcd9f562426b661a0aa399754a9b6d07b8ea16e2ff3a33e481fff413505c967b1a548
7
+ data.tar.gz: 83194ef3873540372ed76f33bcda69c69aef0eae4ce8f2b5b5018a141ebe373f83f2f34987835d1086fb0bb8ac53f37467cdf446082966ca3356b1cc3536b066
@@ -0,0 +1,26 @@
1
+ # encoding: utf-8
2
+ class GeneralMail < Mail
3
+
4
+ def initialize(args)
5
+ super(args)
6
+ @subject = args[:subject]
7
+ @description = args[:description]
8
+ @link = args[:link]
9
+ end
10
+
11
+ def template
12
+ 'general'
13
+ end
14
+
15
+ def subject
16
+ @subject
17
+ end
18
+
19
+ def values
20
+ data = {}
21
+ data["{description}"] = @description
22
+ data["{link}"] = @link
23
+ data
24
+ end
25
+
26
+ end
@@ -2,3 +2,7 @@
2
2
  require_relative 'date'
3
3
  require_relative 'antiquity'
4
4
  require_relative 'notification'
5
+ require_relative 'mailsender'
6
+ require_relative 'mail'
7
+ require_relative 'generalmail'
8
+ require_relative 'mailbuilder'
@@ -0,0 +1,35 @@
1
+ # encoding: utf-8
2
+ class Mail
3
+
4
+ def initialize(args)
5
+ @receivers = args[:to]
6
+ @sender = args[:from]
7
+ end
8
+
9
+ def template
10
+ raise Exception, "You have to override 'template' method in #{self.class}"
11
+ end
12
+
13
+ def subject
14
+ raise Exception, "You have to override 'subject' method in #{self.class}"
15
+ end
16
+
17
+ def values
18
+ raise Exception, "You have to override 'values' method in #{self.class}"
19
+ end
20
+
21
+ def to
22
+ _receivers = {}
23
+ @receivers.each { |user| _receivers[user.full_name] = "Persons" }
24
+ _receivers
25
+ end
26
+
27
+ def from
28
+ @sender ||= "apps@hexacta.com"
29
+ end
30
+
31
+ def send
32
+ MailSender.perform_async(self)
33
+ end
34
+
35
+ end
@@ -0,0 +1,38 @@
1
+ # encoding: utf-8
2
+ class MailBuilder
3
+
4
+ def to(receivers)
5
+ @to = receivers
6
+ return self
7
+ end
8
+
9
+ def from(sender)
10
+ @from = "#{sender.hxt_id}@hexacta.com"
11
+ return self
12
+ end
13
+
14
+ def subject(a_subject)
15
+ @subject = a_subject
16
+ return self
17
+ end
18
+
19
+ def description(a_description)
20
+ @description = a_description
21
+ return self
22
+ end
23
+
24
+ def link(a_link)
25
+ @link = a_link
26
+ return self
27
+ end
28
+
29
+ def send
30
+ @from ||= "apps@hexacta.com"
31
+ GeneralMail.new({ :to => @to,
32
+ :from => @from,
33
+ :subject => @subject,
34
+ :description => @description,
35
+ :link => @link
36
+ }).send
37
+ end
38
+ end
@@ -0,0 +1,88 @@
1
+ # encoding: utf-8
2
+ require 'singleton'
3
+ require 'sucker_punch'
4
+
5
+ class MailSender
6
+ include SuckerPunch::Job
7
+
8
+ attr_reader :access_token, :expire, :refresh_token
9
+
10
+ def _do_connect(uri, form={},token=nil)
11
+ url = URI.parse("#{uri}")
12
+ http = Net::HTTP.new(url.host, url.port)
13
+ http.read_timeout = 1000
14
+ http.use_ssl = (url.scheme == 'https')
15
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
16
+ header = { 'Content-Type' => 'application/x-www-form-urlencoded' }
17
+ header["Authorization"] = "Bearer #{token}" unless token.nil?
18
+ request = Net::HTTP::Post.new(url, header)
19
+ request.form_data = form
20
+ http.start { |http| http.request(request) }
21
+ end
22
+
23
+ def _authorize_mail
24
+ params = [ [ "grant_type", "password" ], [ "client_id", "MailApp" ], [ "client_secret", "MailAppPRD2018!" ] ]
25
+ response = _do_connect("https://comunicacion.hexacta.com:4443/apptoken",params)
26
+ if( response.is_a?( Net::HTTPSuccess ) )
27
+ token = JSON.parse(response.body)
28
+ @access_token = token["access_token"]
29
+ @refresh_token = token["refresh_token"]
30
+ @expire = DateTime.parse(token[".expires"])
31
+ else
32
+ NotificationSender.instance.send_error(nil,'Authorize mail failed',response.body)
33
+ end
34
+ end
35
+
36
+ def _build_map_values(mail_values)
37
+ values = []
38
+ for key in mail_values.keys
39
+ index = mail_values.keys.index(key)
40
+ values << ["MailValues[#{index}].Key", key]
41
+ values << ["MailValues[#{index}].Value", mail_values[key]]
42
+ end
43
+ values
44
+ end
45
+
46
+ def _build_to_map(to_map)
47
+ values = []
48
+ for key in to_map.keys
49
+ index = to_map.keys.index(key)
50
+ values << ["ResourcesRequest[#{index}].TypeGroup", to_map[key]]
51
+ values << ["ResourcesRequest[#{index}].Name", key]
52
+ end
53
+ values
54
+ end
55
+
56
+ def _expired?
57
+ expire.nil? || DateTime.now < expire
58
+ end
59
+
60
+ def do_send(mail)
61
+ begin
62
+ if _expired?
63
+ _authorize_mail
64
+ end
65
+
66
+ params = [ [ "ApplicationCode", 2009 ], [ "Name", mail.template ], [ "Subject", mail.subject ], [ "EmailAddress", mail.from ] ]
67
+ params = params + _build_map_values(mail.values) + _build_to_map(mail.to)
68
+
69
+ response = _do_connect("https://comunicacion.hexacta.com:4444/api/app/TemplateNotification/SendMail",params,@access_token)
70
+
71
+ if( !response.is_a?( Net::HTTPSuccess ) )
72
+ NotificationSender.instance.send_error(nil,'Send mail failed',response.body)
73
+ end
74
+ rescue StandardError => error
75
+ message = error.backtrace.join(',');
76
+ NotificationSender.instance.send_error(nil,"Mail send error",message)
77
+ end
78
+ end
79
+
80
+ def send(mail)
81
+ run(mail) #Async call
82
+ end
83
+
84
+ #For async method call
85
+ def perform(mail)
86
+ do_send(mail)
87
+ end
88
+ end
@@ -1,7 +1,10 @@
1
1
  # encoding: utf-8
2
+ require 'singleton'
3
+
2
4
  class NotificationSender
5
+ include Singleton
3
6
 
4
- def self.send_to(user,creator,title,message,label,link=nil)
7
+ def send_to(user,creator,title,message,label,link=nil)
5
8
  notification = Notification.find_or_create(:user_id => user.id,
6
9
  :creator_id => creator.id,
7
10
  :title => title,
@@ -16,13 +19,13 @@ class NotificationSender
16
19
  notification
17
20
  end
18
21
 
19
- def self.send_to_subscriptors(creator,title,message,label,link=nil)
22
+ def send_to_subscriptors(creator,title,message,label,link=nil)
20
23
  Subscription.where(:label => label).all.each do |subscription|
21
- notification = NotificationSender.send_to(subscription.user,creator,title,message,label,link)
24
+ notification = send_to(subscription.user,creator,title,message,label,link)
22
25
  end
23
26
  end
24
27
 
25
- def self.send_error(creator,title,message)
28
+ def send_error(creator,title,message)
26
29
  Subscription.where(:label => 'error').all.each do |subscription|
27
30
  creator = subscription.user if creator.nil?
28
31
  notification = Notification.find(:user_id => subscription.user_id,
@@ -32,7 +35,7 @@ class NotificationSender
32
35
  :label => 'error',
33
36
  :read_date => nil)
34
37
  if notification.nil?
35
- notification = NotificationSender.send_to(subscription.user,creator,title,message,'error')
38
+ notification = send_to(subscription.user,creator,title,message,'error')
36
39
  end
37
40
  end
38
41
 
@@ -18,7 +18,7 @@ module Sinatra
18
18
  if code == 500
19
19
  title = env['sinatra.error'].message.split(':')[0].gsub('#<','');
20
20
  message = (["in #{request.url}"] + env['sinatra.error'].backtrace).join(',');
21
- NotificationSender.send_error(authenticated(User),title,message)
21
+ NotificationSender.instance.send_error(authenticated(User),title,message)
22
22
  end
23
23
  slim "#{Hexacta::GEM_FILE_DIR}/errors/#{code}".to_sym, locals: { :params => params }
24
24
  end
@@ -11,10 +11,10 @@ module Sinatra
11
11
 
12
12
  post '/notification' do
13
13
  if params[:user_ids].blank?
14
- NotificationSender.send_to_subscriptors(authenticated(User),params[:title],params[:message],params[:label])
14
+ NotificationSender.instance.send_to_subscriptors(authenticated(User),params[:title],params[:message],params[:label])
15
15
  else
16
16
  for id in params[:user_ids]
17
- NotificationSender.send_to(User.find(:id => id),authenticated(User),params[:title],params[:message],params[:label])
17
+ NotificationSender.instance.send_to(User.find(:id => id),authenticated(User),params[:title],params[:message],params[:label])
18
18
  end
19
19
  end
20
20
  redirect back
@@ -21,7 +21,7 @@ module Sinatra
21
21
  rescue StandardError => error
22
22
  title = error.message.split(':')[0].gsub('#<','');
23
23
  message = error.backtrace.join(',');
24
- NotificationSender.send_error(nil,title,message)
24
+ NotificationSender.instance.send_error(nil,title,message)
25
25
  end
26
26
 
27
27
  end
@@ -50,7 +50,7 @@ module Sinatra
50
50
  rescue error
51
51
  title = error.message.split(':')[0].gsub('#<','');
52
52
  message = error.backtrace.join(',');
53
- NotificationSender.send_error(nil,title,message)
53
+ NotificationSender.instance.send_error(nil,title,message)
54
54
  end
55
55
 
56
56
  end
@@ -35,6 +35,11 @@ module Sinatra
35
35
  end
36
36
  end
37
37
 
38
+ unless Gem.loaded_specs["sinatra-hexacta"].nil?
39
+ FileUtils.remove_dir "/app/views/sinatra-hexacta" if Dir.exist? "/app/views/sinatra-hexacta"
40
+ FileUtils.remove_dir "/app/public/sinatra-hexacta" if Dir.exist? "/app/public/sinatra-hexacta"
41
+ end
42
+
38
43
  require_relative 'helpers/init'
39
44
  require_relative 'handlers/init'
40
45
  require_relative 'extensions/init'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sinatra-hexacta
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marco Zanger
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-06-02 00:00:00.000000000 Z
11
+ date: 2020-06-03 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A gem to support general functionality accross all apps
14
14
  email: mzanger@hexacta.com
@@ -18,7 +18,11 @@ extra_rdoc_files: []
18
18
  files:
19
19
  - lib/sinatra/extensions/antiquity.rb
20
20
  - lib/sinatra/extensions/date.rb
21
+ - lib/sinatra/extensions/generalmail.rb
21
22
  - lib/sinatra/extensions/init.rb
23
+ - lib/sinatra/extensions/mail.rb
24
+ - lib/sinatra/extensions/mailbuilder.rb
25
+ - lib/sinatra/extensions/mailsender.rb
22
26
  - lib/sinatra/extensions/notification.rb
23
27
  - lib/sinatra/handlers/errors.rb
24
28
  - lib/sinatra/handlers/init.rb
@@ -31,7 +35,6 @@ files:
31
35
  - lib/sinatra/helpers/init.rb
32
36
  - lib/sinatra/helpers/inputs.rb
33
37
  - lib/sinatra/helpers/libraries.rb
34
- - lib/sinatra/helpers/mailer.rb
35
38
  - lib/sinatra/helpers/reports.rb
36
39
  - lib/sinatra/helpers/schedule.rb
37
40
  - lib/sinatra/helpers/subscriptions.rb
@@ -1,74 +0,0 @@
1
- # encoding: utf-8
2
- require 'sucker_punch'
3
-
4
- module Sinatra
5
- module MailHelper
6
-
7
- attr_reader :access_token, :expire, :refresh_token
8
-
9
- def _do_connect(uri, form={},token=nil)
10
- url = URI.parse("#{uri}")
11
- http = Net::HTTP.new(url.host, url.port)
12
- http.read_timeout = 1000
13
- http.use_ssl = (url.scheme == 'https')
14
- http.verify_mode = OpenSSL::SSL::VERIFY_NONE
15
- header = { 'Content-Type' => 'application/x-www-form-urlencoded' }
16
- header["Authorization"] = "Bearer #{token}" unless token.nil?
17
- request = Net::HTTP::Post.new(url, header)
18
- request.form_data = form
19
- http.start {|http| http.request(request) }
20
- end
21
-
22
- def _authorize_mail
23
- params = [ [ "grant_type", "password" ], [ "client_id", "MailApp" ], [ "client_secret", "MailAppPRD2018!" ] ]
24
- response = _do_connect("https://comunicacion.hexacta.com:4443/apptoken",params)
25
- if( response.is_a?( Net::HTTPSuccess ) )
26
- token = JSON.parse(response.body)
27
- @access_token = token["access_token"]
28
- @refresh_token = token["refresh_token"]
29
- @expire = DateTime.parse(token[".expires"])
30
- else
31
- p "Didn't succeed :("
32
- end
33
- end
34
-
35
- def _build_map_values(mail_values)
36
- values = []
37
- for key in mail_values.keys
38
- index = mail_values.keys.index(key)
39
- values << ["MailValues[#{index}].Key", key]
40
- values << ["MailValues[#{index}].Value", mail_values[key]]
41
- end
42
- values
43
- end
44
-
45
- def _build_to_map(to_map)
46
- values = []
47
- for key in to_map.keys
48
- index = to_map.keys.index(key)
49
- values << ["ResourcesRequest[#{index}].TypeGroup", to_map[key]]
50
- values << ["ResourcesRequest[#{index}].Name", key]
51
- end
52
- values
53
- end
54
-
55
- def _expired?
56
- expire.nil? || DateTime.now < expire
57
- end
58
-
59
- def send_mail(template,subject,mail_values={},to_map)
60
- if _expired?
61
- _authorize_mail
62
- end
63
- params = [ [ "ApplicationCode", 1 ], [ "Name", template ], [ "Subject", subject ], [ "EmailAddress", "apps@hexacta.com" ] ]
64
- params = params + _build_map_values(mail_values) + _build_to_map(to_map)
65
- response = do_connect("https://comunicacion.hexacta.com:4444/api/app/TemplateNotification/SendMail",params,@access_token)
66
- if( !response.is_a?( Net::HTTPSuccess ) )
67
- p "Didn't succeed :("
68
- end
69
- end
70
-
71
- end
72
-
73
- helpers MailHelper
74
- end