Skip to content

Commit 9429154

Browse files
authored
Merge pull request #47 from sarco3t/templates-api
Add EmailTemplate API functionality
2 parents fee2a44 + b1e9c9b commit 9429154

File tree

46 files changed

+8339
-34
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+8339
-34
lines changed

.github/workflows/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: Ruby
22

3-
on: push
3+
on: [push, pull_request]
44

55
jobs:
66
build:

.rubocop.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@ AllCops:
1515
Layout/LineLength:
1616
Max: 120
1717

18+
Metrics/AbcSize:
19+
Max: 25
20+
21+
Metrics/ClassLength:
22+
Max: 200
23+
24+
Metrics/MethodLength:
25+
Max: 20
26+
1827
Naming/MethodParameterName:
1928
MinNameLength: 2
2029

@@ -27,6 +36,9 @@ RSpec/MultipleExpectations:
2736
RSpec/NestedGroups:
2837
Max: 4
2938

39+
RSpec/ExampleLength:
40+
Enabled: false
41+
3042
Style/Documentation:
3143
Enabled: false
3244

Gemfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@ source 'https://rubygems.org'
55
gemspec
66

77
gem 'appraisal'
8+
gem 'irb'
89
gem 'mail'
910
gem 'net-smtp'
1011
gem 'rake', '~> 13.0'
12+
gem 'rdoc', '~> 6.13.0'
1113
gem 'rspec', '~> 3.0'
1214
gem 'rspec-its'
1315
gem 'rubocop', '~> 1.21'
1416
gem 'rubocop-rake', require: false
1517
gem 'rubocop-rspec', require: false
1618
gem 'vcr'
1719
gem 'webmock'
20+
gem 'yard', '~> 0.9'

Gemfile.lock

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ PATH
22
remote: .
33
specs:
44
mailtrap (2.3.0)
5+
base64
56

67
GEM
78
remote: https://rubygems.org/
@@ -13,13 +14,19 @@ GEM
1314
rake
1415
thor (>= 0.14.0)
1516
ast (2.4.2)
17+
base64 (0.3.0)
1618
bigdecimal (3.1.8)
1719
crack (1.0.0)
1820
bigdecimal
1921
rexml
2022
date (3.4.1)
2123
diff-lcs (1.5.1)
2224
hashdiff (1.1.0)
25+
io-console (0.8.0)
26+
irb (1.15.2)
27+
pp (>= 0.6.0)
28+
rdoc (>= 4.0.0)
29+
reline (>= 0.4.2)
2330
json (2.7.2)
2431
language_server-protocol (3.17.0.3)
2532
mail (2.8.1)
@@ -41,11 +48,21 @@ GEM
4148
parser (3.3.3.0)
4249
ast (~> 2.4.1)
4350
racc
51+
pp (0.6.2)
52+
prettyprint
53+
prettyprint (0.2.0)
54+
psych (5.2.6)
55+
date
56+
stringio
4457
public_suffix (5.1.1)
4558
racc (1.8.0)
4659
rainbow (3.1.1)
4760
rake (13.2.1)
61+
rdoc (6.13.1)
62+
psych (>= 4.0.0)
4863
regexp_parser (2.9.2)
64+
reline (0.6.1)
65+
io-console (~> 0.5)
4966
rexml (3.3.9)
5067
rspec (3.13.0)
5168
rspec-core (~> 3.13.0)
@@ -81,6 +98,7 @@ GEM
8198
rubocop-rspec (3.0.2)
8299
rubocop (~> 1.61)
83100
ruby-progressbar (1.13.0)
101+
stringio (3.1.7)
84102
thor (1.3.1)
85103
timeout (0.4.3)
86104
unicode-display_width (2.5.0)
@@ -89,23 +107,27 @@ GEM
89107
addressable (>= 2.8.0)
90108
crack (>= 0.3.2)
91109
hashdiff (>= 0.4.0, < 2.0.0)
110+
yard (0.9.37)
92111

93112
PLATFORMS
94113
ruby
95114

96115
DEPENDENCIES
97116
appraisal
117+
irb
98118
mail
99119
mailtrap!
100120
net-smtp
101121
rake (~> 13.0)
122+
rdoc (~> 6.13.0)
102123
rspec (~> 3.0)
103124
rspec-its
104125
rubocop (~> 1.21)
105126
rubocop-rake
106127
rubocop-rspec
107128
vcr
108129
webmock
130+
yard (~> 0.9)
109131

110132
BUNDLED WITH
111133
2.6.5

README.md

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,29 @@ client = Mailtrap::Client.new(api_key: 'your-api-key')
6262
client.send(mail)
6363
```
6464

65-
Refer to the [`examples`](examples) folder for more examples.
65+
### Email Templates API
66+
67+
```ruby
68+
require 'mailtrap'
69+
70+
client = Mailtrap::Client.new(api_key: 'your-api-key')
71+
templates = Mailtrap::EmailTemplatesAPI.new 3229, client
72+
73+
templates.create(
74+
name: 'Welcome Email',
75+
subject: 'Welcome to Mailtrap!',
76+
body_html: '<h1>Hello</h1>',
77+
body_text: 'Hello',
78+
category: 'welcome'
79+
)
80+
```
81+
82+
Refer to the [`examples`](examples) folder for more examples:
6683

6784
- [Full](examples/full.rb)
6885
- [Email template](examples/email_template.rb)
6986
- [ActionMailer](examples/action_mailer.rb)
87+
- [Email Templates API](examples/email_templates_api.rb)
7088

7189
### Content-Transfer-Encoding
7290

@@ -77,7 +95,7 @@ sending. For example, `/api/send` endpoint ignores `Content-Transfer-Encoding`
7795
Meaning your recipients will receive emails only in the default encoding which
7896
is `quoted-printable`, if you send with Mailtrap API.
7997

80-
For those who does need to use `7bit` or any other encoding, SMTP provides
98+
For those who need to use `7bit` or any other encoding, SMTP provides
8199
better flexibility in that regard. Go to your _Mailtrap account__Email Sending_
82100
_Sending Domains__Your domain__SMTP/API Settings_ to find the SMTP
83101
configuration example.

examples/email_templates_api.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
require 'mailtrap'
2+
3+
client = Mailtrap::Client.new(api_key: 'your-api-key')
4+
templates = Mailtrap::EmailTemplatesAPI.new 3229, client
5+
6+
# Set your API credentials as environment variables
7+
# export MAILTRAP_API_KEY='your-api-key'
8+
# export MAILTRAP_ACCOUNT_ID=your-account-id
9+
#
10+
# templates = Mailtrap::EmailTemplatesAPI.new
11+
12+
# Get all email templates
13+
templates.list
14+
15+
# Create a new email template
16+
email_template = templates.create(
17+
name: 'Welcome Email',
18+
subject: 'Welcome to Mailtrap!',
19+
body_html: '<h1>Hello</h1>',
20+
body_text: 'Hello',
21+
category: 'welcome'
22+
)
23+
24+
# Get an email template
25+
email_template = templates.get(email_template.id)
26+
27+
# Update an email template
28+
email_template = templates.update(email_template.id, name: 'Welcome Updated')
29+
30+
# Delete an email template
31+
templates.delete(email_template.id)

examples/full.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
category: 'Integration Test',
1919
attachments: [
2020
{
21-
content: Base64.encode64('Attachment content'), # base64 encoded content or IO string
21+
content: Base64.strict_encode64('Attachment content'), # base64 encoded content or IO string
2222
filename: 'attachment.txt'
2323
}
2424
],
@@ -37,6 +37,11 @@
3737

3838
client = Mailtrap::Client.new(api_key: 'your-api-key')
3939

40+
# Set your API credentials as environment variables
41+
# export MAILTRAP_API_KEY='your-api-key'
42+
#
43+
# client = Mailtrap::Client.new
44+
4045
# Custom host / port
4146
# client = Mailtrap::Client.new(api_key: 'your-api-key', api_host: 'alternative.host.mailtrap.io', api_port: 8080)
4247

lib/mailtrap.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,12 @@
44
require_relative 'mailtrap/mail'
55
require_relative 'mailtrap/errors'
66
require_relative 'mailtrap/version'
7+
require_relative 'mailtrap/email_templates_api'
78

8-
module Mailtrap; end
9+
module Mailtrap
10+
# @!macro api_errors
11+
# @raise [Mailtrap::Error] If the API request fails with a client or server error
12+
# @raise [Mailtrap::AuthorizationError] If the API key is invalid
13+
# @raise [Mailtrap::RejectionError] If the server refuses to process the request
14+
# @raise [Mailtrap::RateLimitError] If too many requests are made
15+
end

0 commit comments

Comments
 (0)