emailfuse 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 10f6474788d0e55fd6b5acf21634b0400093ae064a2edea2bad4e24bf5492f23
4
+ data.tar.gz: '0359d5d174aa60a50a152fa99d1846b314a61614ff54d016ed734536417f93d4'
5
+ SHA512:
6
+ metadata.gz: 4143f719a80cb4639c4be3f0936cd7d4c518c3f25811c775fcb5f590ccf4beb97a109ae63f8432cea562c70b25fa78ce19ceaca2f429402123a9d194930f2850
7
+ data.tar.gz: 836eb683894f0575addb17ade8fca333f91bd747df92f3a6dc89fb871554b06677d8aabf7726d81dfa287392fef6405d6d4a0837fc48fb151e2e1572125e2c9d
data/README.md ADDED
@@ -0,0 +1,26 @@
1
+ # Voupe Emails
2
+
3
+ *email_fuse* is an Action Mailer adapter for using [Emails](https://github.com/voupe/emails) in Rails apps.
4
+
5
+ It is a heavily modified version of the [mailgun_rails](https://github.com/jorgemanrubia/mailgun_rails) gem
6
+
7
+ ## Installing
8
+
9
+ In your `Gemfile`
10
+
11
+ ```ruby
12
+ gem "email_fuse"
13
+ ```
14
+
15
+ ## Usage
16
+
17
+ To configure your VoupeEmails credentials place the following code in the corresponding environment file (`development.rb`, `production.rb`...)
18
+
19
+ ```ruby
20
+ config.action_mailer.delivery_method = :email_fuse
21
+ config.action_mailer.email_fuse_settings = {
22
+ api_token: "abc",
23
+ host: "your.host.com",
24
+ ssl: true
25
+ }
26
+ ```
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task default: :test
@@ -0,0 +1,69 @@
1
+ module Emailfuse
2
+ class Client
3
+ class << self
4
+ def url
5
+ Emailfuse.config.host || "https://api.emailfuse.net"
6
+ end
7
+
8
+ def connection
9
+ @connection ||= Faraday.new("#{url}/v1") do |conn|
10
+ conn.request :authorization, :Bearer, Emailfuse.config.token
11
+
12
+ conn.headers = {
13
+ "User-Agent" => "emailfuse/v#{VERSION} (github.com/voupe/emailfuse-gem)"
14
+ }
15
+
16
+ conn.request :multipart
17
+ conn.request :json
18
+
19
+ conn.response :json
20
+ end
21
+ end
22
+
23
+ def get_request(url, params: {}, headers: {})
24
+ handle_response connection.get(url, params, headers)
25
+ end
26
+
27
+ def post_request(url, body: {}, headers: {})
28
+ handle_response connection.post(url, body, headers)
29
+ end
30
+
31
+ def patch_request(url, body:, headers: {})
32
+ handle_response connection.patch(url, body, headers)
33
+ end
34
+
35
+ def delete_request(url, headers: {})
36
+ handle_response connection.delete(url, headers)
37
+ end
38
+
39
+ def handle_response(response)
40
+ case response.status
41
+ when 400
42
+ raise Error, "Error 400: Your request was malformed."
43
+ when 401
44
+ raise Error, "Error 401: You did not supply valid authentication credentials."
45
+ when 403
46
+ raise Error, "Error 403: You are not allowed to perform that action."
47
+ when 404
48
+ raise Error, "Error 404: No results were found for your request."
49
+ when 409
50
+ raise Error, "Error 409: Your request was a conflict."
51
+ when 429
52
+ raise Error, "Error 429: Your request exceeded the API rate limit."
53
+ when 422
54
+ raise Error, "Error 422: Unprocessable Entity."
55
+ when 500
56
+ raise Error, "Error 500: We were unable to perform the request due to server-side problems."
57
+ when 503
58
+ raise Error, "Error 503: You have been rate limited for sending more than 20 requests per second."
59
+ when 501
60
+ raise Error, "Error 501: This resource has not been implemented."
61
+ when 204
62
+ return true
63
+ end
64
+
65
+ response
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,27 @@
1
+ module Emailfuse
2
+ class Collection
3
+ attr_reader :data, :total
4
+
5
+ def self.from_response(response, type:, key: nil)
6
+ body = response.body
7
+
8
+ if key.is_a?(String)
9
+ data = body["data"][key].map { |attrs| type.new(attrs) }
10
+ total = body["data"]["total"]
11
+ else
12
+ data = body["data"].map { |attrs| type.new(attrs) }
13
+ total = body["data"].count
14
+ end
15
+
16
+ new(
17
+ data: data,
18
+ total: total
19
+ )
20
+ end
21
+
22
+ def initialize(data:, total:)
23
+ @data = data
24
+ @total = total
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Emailfuse
4
+ class Configuration
5
+ attr_accessor :host, :token
6
+
7
+ def initialize
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,68 @@
1
+ module Emailfuse
2
+ class Deliverer
3
+ class Attachment < StringIO
4
+ attr_reader :original_filename, :content_type, :path
5
+
6
+ def initialize (attachment, *rest)
7
+ @path = ""
8
+ @original_filename = attachment.filename
9
+ @content_type = attachment.content_type.split(";")[0]
10
+ super attachment.body.decoded
11
+ end
12
+ end
13
+
14
+ attr_accessor :settings
15
+
16
+ def initialize(settings)
17
+ self.settings = settings
18
+ end
19
+
20
+ def api_token
21
+ self.settings[:token] || Emailfuse.config.token
22
+ end
23
+
24
+ def deliver!(rails_message)
25
+ attributes = {
26
+ from: rails_message[:from],
27
+ to: rails_message[:to],
28
+ subject: rails_message.subject,
29
+ html: extract_html(rails_message),
30
+ text: extract_text(rails_message)
31
+ }
32
+
33
+ unless rails_message.attachments.empty?
34
+ attributes[:attachments] = []
35
+ rails_message.attachments.each do |attachment|
36
+ attributes[:attachments] << Attachment.new(attachment, encoding: "ascii-8bit")
37
+ end
38
+ end
39
+
40
+ Email.create(**attributes)
41
+ end
42
+
43
+ private
44
+
45
+ # @see http://stackoverflow.com/questions/4868205/rails-mail-getting-the-body-as-plain-text
46
+ def extract_html(rails_message)
47
+ if rails_message.html_part
48
+ rails_message.html_part.body.decoded
49
+ else
50
+ rails_message.content_type =~ /text\/html/ ? rails_message.body.decoded : nil
51
+ end
52
+ end
53
+
54
+ def extract_text(rails_message)
55
+ if rails_message.multipart?
56
+ rails_message.text_part ? rails_message.text_part.body.decoded : nil
57
+ else
58
+ rails_message.content_type =~ /text\/plain/ ? rails_message.body.decoded : nil
59
+ end
60
+ end
61
+
62
+ def email_fuse_client
63
+ @email_fuse_client ||= Client.new(api_token, host)
64
+ end
65
+ end
66
+ end
67
+
68
+ ActionMailer::Base.add_delivery_method :email_fuse, Emailfuse::Deliverer
@@ -0,0 +1,4 @@
1
+ module Emailfuse
2
+ class Error < StandardError
3
+ end
4
+ end
@@ -0,0 +1,21 @@
1
+ module Emailfuse
2
+ class Email < Object
3
+ class << self
4
+ def create(to:, from:, subject:, html: nil, text: nil, **attributes)
5
+ raise ArgumentError, "You must provide either html or text" if html.nil? && text.nil?
6
+
7
+ attributes.merge!(
8
+ to: to,
9
+ from: from,
10
+ subject: subject,
11
+ html: html,
12
+ text: text
13
+ )
14
+
15
+ response = Client.post_request("emails", body: attributes)
16
+
17
+ Email.new(response.body) if response.success?
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,19 @@
1
+ require "ostruct"
2
+
3
+ module Emailfuse
4
+ class Object < OpenStruct
5
+ def initialize(attributes)
6
+ super to_ostruct(attributes)
7
+ end
8
+
9
+ def to_ostruct(obj)
10
+ if obj.is_a?(Hash)
11
+ OpenStruct.new(obj.map { |key, val| [ key, to_ostruct(val) ] }.to_h)
12
+ elsif obj.is_a?(Array)
13
+ obj.map { |o| to_ostruct(o) }
14
+ else # Assumed to be a primitive value
15
+ obj
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,12 @@
1
+ module Emailfuse
2
+ class Railtie < Rails::Railtie
3
+ initializer "emailfuse.add_delivery_method" do
4
+ ActiveSupport.on_load :action_mailer do
5
+ ActionMailer::Base.add_delivery_method(
6
+ :emailfuse,
7
+ Deliverer
8
+ )
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Emailfuse
4
+ VERSION = "0.1.1"
5
+ end
data/lib/emailfuse.rb ADDED
@@ -0,0 +1,30 @@
1
+ require "action_mailer"
2
+
3
+ require "faraday"
4
+ require "faraday/multipart"
5
+
6
+ module Emailfuse
7
+ autoload :Configuration, "emailfuse/configuration"
8
+ autoload :Client, "emailfuse/client"
9
+ autoload :Collection, "emailfuse/collection"
10
+ autoload :Error, "emailfuse/error"
11
+ autoload :Object, "emailfuse/object"
12
+
13
+ autoload :Deliverer, "emailfuse/deliverer"
14
+
15
+ class << self
16
+ attr_writer :config
17
+ end
18
+
19
+ def self.configure
20
+ yield(config) if block_given?
21
+ end
22
+
23
+ def self.config
24
+ @config ||= Configuration.new
25
+ end
26
+
27
+ autoload :Email, "emailfuse/models/email"
28
+ end
29
+
30
+ require "emailfuse/railtie" if defined?(Rails::Railtie)
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: emailfuse
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Dean Perry
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-06-14 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: '7.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '7.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: faraday
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.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.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: faraday-multipart
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.0'
55
+ description:
56
+ email:
57
+ - dean@voupe.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - README.md
63
+ - Rakefile
64
+ - lib/emailfuse.rb
65
+ - lib/emailfuse/client.rb
66
+ - lib/emailfuse/collection.rb
67
+ - lib/emailfuse/configuration.rb
68
+ - lib/emailfuse/deliverer.rb
69
+ - lib/emailfuse/error.rb
70
+ - lib/emailfuse/models/email.rb
71
+ - lib/emailfuse/object.rb
72
+ - lib/emailfuse/railtie.rb
73
+ - lib/emailfuse/version.rb
74
+ homepage:
75
+ licenses:
76
+ - MIT
77
+ metadata: {}
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubygems_version: 3.5.9
94
+ signing_key:
95
+ specification_version: 4
96
+ summary: Ruby library for the Emailfuse API including an Rails Action Mailer adapter
97
+ test_files: []