surenotify_rails 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: e2ff790368b45481416ff8b497480f097a3698ccc3c04272c2e70eaf2599a043
4
+ data.tar.gz: d286745c8c606c2ba902584663586b3b8e40b17b7ee5145d36b46f5657f38c03
5
+ SHA512:
6
+ metadata.gz: b11f4579e8c6235c0084cc4739fd406b95f07129c67c246764d8e65b079c1d1b487d59c1da2e100abf734762de67d0e33ab96117c03324ad051e5fbf585f678e
7
+ data.tar.gz: a5c327ba693a6a5e6cb36f388939f38cea51ce58be9b5499833f19b54200296d38106f1c56022f222241e1f8d270cc41281f1b7f7dba52d4ac06a3c05924f7ce
@@ -0,0 +1,20 @@
1
+ Copyright 2013 Jorge Manrubia
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,3 @@
1
+ = SurenotifyRails
2
+
3
+ This project rocks and uses MIT-LICENSE.
@@ -0,0 +1,23 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'SurenotifyRails'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ Bundler::GemHelper.install_tasks
18
+
19
+ require 'rspec/core/rake_task'
20
+
21
+ RSpec::Core::RakeTask.new(:spec)
22
+
23
+ task default: :spec
@@ -0,0 +1,8 @@
1
+ require 'action_mailer'
2
+ require 'json'
3
+
4
+
5
+ Dir[File.dirname(__FILE__) + '/surenotify_rails/*.rb'].each {|file| require file }
6
+
7
+ module SurenotifyRails
8
+ end
@@ -0,0 +1,16 @@
1
+ module SurenotifyRails
2
+ class Attachment < StringIO
3
+ attr_reader :original_filename, :content_type, :path
4
+
5
+ def initialize (attachment, *rest)
6
+ @path = ''
7
+ if rest.detect {|opt| opt[:inline] }
8
+ basename = @original_filename = attachment.cid
9
+ else
10
+ basename = @original_filename = attachment.filename
11
+ end
12
+ @content_type = attachment.content_type.split(';')[0]
13
+ super attachment.body.decoded
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,35 @@
1
+ require 'rest_client'
2
+
3
+
4
+ module SurenotifyRails
5
+ class Client
6
+ attr_reader :api_key, :verify_ssl
7
+
8
+ def initialize(api_key, verify_ssl = true)
9
+ @api_key = api_key
10
+ @verify_ssl = verify_ssl
11
+ end
12
+
13
+ def send_message(options)
14
+ RestClient::Request.execute(
15
+ method: :post,
16
+ url: surenotify_url,
17
+ payload: JSON::dump(options),
18
+ verify_ssl: verify_ssl,
19
+ headers: {
20
+ content_type: 'application/json',
21
+ accept: 'application/json',
22
+ x_api_key: @api_key
23
+ }
24
+ )
25
+ end
26
+
27
+ def surenotify_url
28
+ api_url + "/messages"
29
+ end
30
+
31
+ def api_url
32
+ 'https://mail.surenotifyapi.com/v1'
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,134 @@
1
+ module SurenotifyRails
2
+ class Deliverer
3
+
4
+ attr_accessor :settings
5
+
6
+ def initialize(settings)
7
+ self.settings = settings
8
+ end
9
+
10
+ def api_key
11
+ self.settings[:api_key]
12
+ end
13
+
14
+ def verify_ssl
15
+ #default value = true
16
+ self.settings[:verify_ssl] != false
17
+ end
18
+
19
+ def deliver!(rails_message)
20
+ response = surenotify_client.send_message build_surenotify_message_for(rails_message)
21
+ if response.code == 200
22
+ surenotify_message_id = JSON.parse(response.to_str)["id"]
23
+ rails_message.message_id = surenotify_message_id
24
+ end
25
+ response
26
+ end
27
+
28
+ private
29
+
30
+ def build_surenotify_message_for(rails_message)
31
+ surenotify_message = build_basic_surenotify_message_for rails_message
32
+ # transform_surenotify_attributes_from_rails rails_message, surenotify_message
33
+ remove_empty_values surenotify_message
34
+
35
+ surenotify_message
36
+ end
37
+
38
+ def transform_surenotify_attributes_from_rails(rails_message, surenotify_message)
39
+ transform_reply_to rails_message, surenotify_message if rails_message.reply_to
40
+ transform_surenotify_variables rails_message, surenotify_message
41
+ transform_surenotify_options rails_message, surenotify_message
42
+ transform_surenotify_recipient_variables rails_message, surenotify_message
43
+ transform_custom_headers rails_message, surenotify_message
44
+ end
45
+
46
+ def build_basic_surenotify_message_for(rails_message)
47
+ surenotify_message = {
48
+ fromName: rails_message[:from].addrs.first.name,
49
+ fromAddress: rails_message[:from].addrs.first.address,
50
+ recipients: formatted_receivers(rails_message[:to]),
51
+ subject: rails_message.subject,
52
+ content: extract_html(rails_message)
53
+ }
54
+
55
+ [:cc, :bcc].each do |key|
56
+ surenotify_message[key] = rails_message[key].formatted if rails_message[key]
57
+ end
58
+
59
+ return surenotify_message if rails_message.attachments.empty?
60
+
61
+ # RestClient requires attachments to be in file format, use a temp directory and the decoded attachment
62
+ surenotify_message[:attachment] = []
63
+ surenotify_message[:inline] = []
64
+ rails_message.attachments.each do |attachment|
65
+ # then add as a file object
66
+ if attachment.inline?
67
+ surenotify_message[:inline] << surenotifyRails::Attachment.new(attachment, encoding: 'ascii-8bit', inline: true)
68
+ else
69
+ surenotify_message[:attachment] << surenotifyRails::Attachment.new(attachment, encoding: 'ascii-8bit')
70
+ end
71
+ end
72
+
73
+ return surenotify_message
74
+ end
75
+
76
+ def transform_reply_to(rails_message, surenotify_message)
77
+ surenotify_message['h:Reply-To'] = rails_message[:reply_to].formatted.first
78
+ end
79
+
80
+ # @see http://stackoverflow.com/questions/4868205/rails-mail-getting-the-body-as-plain-text
81
+ def extract_html(rails_message)
82
+ if rails_message.html_part
83
+ rails_message.html_part.body.decoded
84
+ else
85
+ rails_message.content_type =~ /text\/html/ ? rails_message.body.decoded : nil
86
+ end
87
+ end
88
+
89
+ def extract_text(rails_message)
90
+ if rails_message.multipart?
91
+ rails_message.text_part ? rails_message.text_part.body.decoded : nil
92
+ else
93
+ rails_message.content_type =~ /text\/plain/ ? rails_message.body.decoded : nil
94
+ end
95
+ end
96
+
97
+ def transform_surenotify_variables(rails_message, surenotify_message)
98
+ rails_message.surenotify_variables.try(:each) do |name, value|
99
+ surenotify_message["v:#{name}"] = value
100
+ end
101
+ end
102
+
103
+ def transform_surenotify_options(rails_message, surenotify_message)
104
+ rails_message.surenotify_options.try(:each) do |name, value|
105
+ surenotify_message["o:#{name}"] = value
106
+ end
107
+ end
108
+
109
+ def transform_custom_headers(rails_message, surenotify_message)
110
+ rails_message.surenotify_headers.try(:each) do |name, value|
111
+ surenotify_message["h:#{name}"] = value
112
+ end
113
+ end
114
+
115
+ def transform_surenotify_recipient_variables(rails_message, surenotify_message)
116
+ surenotify_message['recipient-variables'] = rails_message.surenotify_recipient_variables.to_json if rails_message.surenotify_recipient_variables
117
+ end
118
+
119
+ def remove_empty_values(surenotify_message)
120
+ surenotify_message.delete_if { |key, value| value.nil? or
121
+ value.respond_to?(:empty?) && value.empty? }
122
+ end
123
+
124
+ def surenotify_client
125
+ @surenotify_client ||= Client.new(api_key, verify_ssl)
126
+ end
127
+
128
+ def formatted_receivers(receivers)
129
+ receivers.addrs.map { |r| { address: r.address } }
130
+ end
131
+ end
132
+ end
133
+
134
+ ActionMailer::Base.add_delivery_method :surenotify, SurenotifyRails::Deliverer
@@ -0,0 +1,8 @@
1
+ module Mail
2
+ class Message
3
+ attr_accessor :surenotify_variables
4
+ attr_accessor :surenotify_options
5
+ attr_accessor :surenotify_recipient_variables
6
+ attr_accessor :surenotify_headers
7
+ end
8
+ end
@@ -0,0 +1,3 @@
1
+ module SurenotifyRails
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,122 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: surenotify_rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Leo Chen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-01-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: actionmailer
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 4.2.11
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 4.2.11
27
+ - !ruby/object:Gem::Dependency
28
+ name: json
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 2.1.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 2.1.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: rest-client
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 2.0.2
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 2.0.2
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 2.14.1
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 2.14.1
69
+ - !ruby/object:Gem::Dependency
70
+ name: rails
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: 4.2.11
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: 4.2.11
83
+ description: An adapter for using Surenotify with Rails and Action Mailer
84
+ email:
85
+ - pominx@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - MIT-LICENSE
91
+ - README.rdoc
92
+ - Rakefile
93
+ - lib/surenotify_rails.rb
94
+ - lib/surenotify_rails/attachment.rb
95
+ - lib/surenotify_rails/client.rb
96
+ - lib/surenotify_rails/deliverer.rb
97
+ - lib/surenotify_rails/mail_ext.rb
98
+ - lib/surenotify_rails/version.rb
99
+ homepage: https://github.com/pominx/surenotify_rails
100
+ licenses:
101
+ - MIT
102
+ metadata: {}
103
+ post_install_message:
104
+ rdoc_options: []
105
+ require_paths:
106
+ - lib
107
+ required_ruby_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ required_rubygems_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ requirements: []
118
+ rubygems_version: 3.1.4
119
+ signing_key:
120
+ specification_version: 4
121
+ summary: Rails Action Mailer adapter for Surenotify (NewsLeopard)
122
+ test_files: []