send_grid_mailer 1.1.0 → 1.2.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: 2d726da9f986023abcc14c683d799ed6cacf43ffeb934cf0ec12ea6cd25e707f
4
- data.tar.gz: 6f09f6d43206e7a9fbe22ef59970d579a94b2f7cd140d79ff534b7090e741dcd
3
+ metadata.gz: e109a7af505603294c6798f83ba1d83649cd326e603a81f708e275b033da4c67
4
+ data.tar.gz: db5967fe28921e318223b7bc5e2cb353079552e681e95665b2458681bdff0392
5
5
  SHA512:
6
- metadata.gz: 9e68184451599dc1579f3891cbfad2613bacc3dbc9741a489cd2ed6169468a0b6e8029bc1f3812b54bb99ad2ccb3b6e9bd8a64100a2461d4eeddcba703ed8308
7
- data.tar.gz: 817b1843f2c2a2a8727ae9693bdc1186fe947b0b8ee93b52de72e07003cfe450d783ae79757df8e5f022714e24ca6d3b68754ff106efd580d78b3419b93d45b2
6
+ metadata.gz: a189f88ee3e30656ac6e325f8775c154b92c378aa24a307d82b631024ceb36cdddbf973164d9992f35967b5745cac990924fb1fa428b90bec4598c932890480c
7
+ data.tar.gz: 9450313750e71ffe6edee43b1e26e7ef380b5677ca9db758916d451fd30cb28bbf6f01850f467bb91928a02b87eb3062e761ea113024f9dfb04cf2d69f1cf4d4
data/.rubocop.yml CHANGED
@@ -10,7 +10,7 @@ AllCops:
10
10
  DisplayCopNames: false
11
11
  StyleGuideCopsOnly: false
12
12
  TargetRubyVersion: 2.3
13
- Style/AccessModifierIndentation:
13
+ Layout/AccessModifierIndentation:
14
14
  Description: Check indentation of private/protected visibility modifiers.
15
15
  StyleGuide: https://github.com/bbatsov/ruby-style-guide#indent-public-private-protected
16
16
  Enabled: true
@@ -18,7 +18,7 @@ Style/AccessModifierIndentation:
18
18
  SupportedStyles:
19
19
  - outdent
20
20
  - indent
21
- Style/AlignHash:
21
+ Layout/AlignHash:
22
22
  Description: Align the elements of a hash literal if they span more than one line.
23
23
  Enabled: true
24
24
  EnforcedHashRocketStyle: key
@@ -29,7 +29,7 @@ Style/AlignHash:
29
29
  - always_ignore
30
30
  - ignore_implicit
31
31
  - ignore_explicit
32
- Style/AlignParameters:
32
+ Layout/AlignParameters:
33
33
  Description: Align the parameters of a method call if they span more than one line.
34
34
  StyleGuide: https://github.com/bbatsov/ruby-style-guide#no-double-indent
35
35
  Enabled: true
@@ -63,11 +63,11 @@ Style/BracesAroundHashParameters:
63
63
  - braces
64
64
  - no_braces
65
65
  - context_dependent
66
- Style/CaseIndentation:
66
+ Layout/CaseIndentation:
67
67
  Description: Indentation of when in a case/when/[else/]end.
68
68
  StyleGuide: https://github.com/bbatsov/ruby-style-guide#indent-when-to-case
69
69
  Enabled: true
70
- IndentWhenRelativeTo: case
70
+ EnforcedStyle: case
71
71
  SupportedStyles:
72
72
  - case
73
73
  - end
@@ -108,7 +108,7 @@ Style/CommentAnnotation:
108
108
  - OPTIMIZE
109
109
  - HACK
110
110
  - REVIEW
111
- Style/DotPosition:
111
+ Layout/DotPosition:
112
112
  Description: Checks the position of the dot in multi-line method calls.
113
113
  StyleGuide: https://github.com/bbatsov/ruby-style-guide#consistent-multi-line-chains
114
114
  Enabled: true
@@ -116,26 +116,26 @@ Style/DotPosition:
116
116
  SupportedStyles:
117
117
  - leading
118
118
  - trailing
119
- Style/EmptyLineBetweenDefs:
119
+ Layout/EmptyLineBetweenDefs:
120
120
  Description: Use empty lines between defs.
121
121
  StyleGuide: https://github.com/bbatsov/ruby-style-guide#empty-lines-between-methods
122
122
  Enabled: true
123
123
  AllowAdjacentOneLineDefs: false
124
- Style/EmptyLinesAroundBlockBody:
124
+ Layout/EmptyLinesAroundBlockBody:
125
125
  Description: Keeps track of empty lines around block bodies.
126
126
  Enabled: true
127
127
  EnforcedStyle: no_empty_lines
128
128
  SupportedStyles:
129
129
  - empty_lines
130
130
  - no_empty_lines
131
- Style/EmptyLinesAroundClassBody:
131
+ Layout/EmptyLinesAroundClassBody:
132
132
  Description: Keeps track of empty lines around class bodies.
133
133
  Enabled: true
134
134
  EnforcedStyle: no_empty_lines
135
135
  SupportedStyles:
136
136
  - empty_lines
137
137
  - no_empty_lines
138
- Style/EmptyLinesAroundModuleBody:
138
+ Layout/EmptyLinesAroundModuleBody:
139
139
  Description: Keeps track of empty lines around module bodies.
140
140
  Enabled: true
141
141
  EnforcedStyle: no_empty_lines
@@ -155,7 +155,7 @@ Style/FileName:
155
155
  StyleGuide: https://github.com/bbatsov/ruby-style-guide#snake-case-files
156
156
  Enabled: false
157
157
  Exclude: []
158
- Style/FirstParameterIndentation:
158
+ Layout/FirstParameterIndentation:
159
159
  Description: Checks the indentation of the first parameter in a method call.
160
160
  Enabled: true
161
161
  EnforcedStyle: special_for_inner_method_call_in_parentheses
@@ -206,12 +206,12 @@ Style/IfUnlessModifier:
206
206
  StyleGuide: https://github.com/bbatsov/ruby-style-guide#if-as-a-modifier
207
207
  Enabled: false
208
208
  MaxLineLength: 80
209
- Style/IndentationWidth:
209
+ Layout/IndentationWidth:
210
210
  Description: Use 2 spaces for indentation.
211
211
  StyleGuide: https://github.com/bbatsov/ruby-style-guide#spaces-indentation
212
212
  Enabled: true
213
213
  Width: 2
214
- Style/IndentHash:
214
+ Layout/IndentHash:
215
215
  Description: Checks the indentation of the first key in a hash literal.
216
216
  Enabled: true
217
217
  EnforcedStyle: special_inside_parentheses
@@ -256,7 +256,7 @@ Style/MethodName:
256
256
  SupportedStyles:
257
257
  - snake_case
258
258
  - camelCase
259
- Style/MultilineOperationIndentation:
259
+ Layout/MultilineOperationIndentation:
260
260
  Description: Checks indentation of binary operations that span more than one line.
261
261
  Enabled: true
262
262
  EnforcedStyle: indented
@@ -366,14 +366,14 @@ Style/StringLiteralsInInterpolation:
366
366
  SupportedStyles:
367
367
  - single_quotes
368
368
  - double_quotes
369
- Style/SpaceAroundBlockParameters:
369
+ Layout/SpaceAroundBlockParameters:
370
370
  Description: Checks the spacing inside and after block parameters pipes.
371
371
  Enabled: true
372
372
  EnforcedStyleInsidePipes: no_space
373
- SupportedStyles:
373
+ SupportedStylesInsidePipes:
374
374
  - space
375
375
  - no_space
376
- Style/SpaceAroundEqualsInParameterDefault:
376
+ Layout/SpaceAroundEqualsInParameterDefault:
377
377
  Description: Checks that the equals signs in parameter default assignments have
378
378
  or don't have surrounding space depending on configuration.
379
379
  StyleGuide: https://github.com/bbatsov/ruby-style-guide#spaces-around-equals
@@ -382,14 +382,14 @@ Style/SpaceAroundEqualsInParameterDefault:
382
382
  SupportedStyles:
383
383
  - space
384
384
  - no_space
385
- Style/SpaceBeforeBlockBraces:
385
+ Layout/SpaceBeforeBlockBraces:
386
386
  Description: Checks that the left block brace has or doesn't have space before it.
387
387
  Enabled: true
388
388
  EnforcedStyle: space
389
389
  SupportedStyles:
390
390
  - space
391
391
  - no_space
392
- Style/SpaceInsideBlockBraces:
392
+ Layout/SpaceInsideBlockBraces:
393
393
  Description: Checks that block braces have or don't have surrounding space. For
394
394
  blocks taking parameters, checks that the left brace has or doesn't have trailing
395
395
  space.
@@ -400,7 +400,7 @@ Style/SpaceInsideBlockBraces:
400
400
  - no_space
401
401
  EnforcedStyleForEmptyBraces: no_space
402
402
  SpaceBeforeBlockParameters: true
403
- Style/SpaceInsideHashLiteralBraces:
403
+ Layout/SpaceInsideHashLiteralBraces:
404
404
  Description: Use spaces inside hash literal braces - or don't.
405
405
  StyleGuide: https://github.com/bbatsov/ruby-style-guide#spaces-operators
406
406
  Enabled: true
@@ -414,7 +414,7 @@ Style/SymbolProc:
414
414
  Enabled: true
415
415
  IgnoredMethods:
416
416
  - respond_to
417
- Style/TrailingBlankLines:
417
+ Layout/TrailingBlankLines:
418
418
  Description: Checks trailing blank lines and final newline.
419
419
  StyleGuide: https://github.com/bbatsov/ruby-style-guide#newline-eof
420
420
  Enabled: true
@@ -530,15 +530,15 @@ Lint/AssignmentInCondition:
530
530
  Lint/EndAlignment:
531
531
  Description: Align ends correctly.
532
532
  Enabled: true
533
- AlignWith: keyword
534
- SupportedStyles:
533
+ EnforcedStyleAlignWith: keyword
534
+ SupportedStylesAlignWith:
535
535
  - keyword
536
536
  - variable
537
537
  Lint/DefEndAlignment:
538
538
  Description: Align ends corresponding to defs correctly.
539
539
  Enabled: true
540
- AlignWith: start_of_line
541
- SupportedStyles:
540
+ EnforcedStyleAlignWith: start_of_line
541
+ SupportedStylesAlignWith:
542
542
  - start_of_line
543
543
  - def
544
544
  Rails/ActionFilter:
@@ -589,7 +589,7 @@ Style/SymbolArray:
589
589
  Description: Use %i or %I for arrays of symbols.
590
590
  StyleGuide: https://github.com/bbatsov/ruby-style-guide#percent-i
591
591
  Enabled: false
592
- Style/ExtraSpacing:
592
+ Layout/ExtraSpacing:
593
593
  Description: Do not use unnecessary spacing.
594
594
  Enabled: false
595
595
  Style/AccessorMethodName:
@@ -599,7 +599,7 @@ Style/Alias:
599
599
  Description: Use alias_method instead of alias.
600
600
  StyleGuide: https://github.com/bbatsov/ruby-style-guide#alias-method
601
601
  Enabled: false
602
- Style/AlignArray:
602
+ Layout/AlignArray:
603
603
  Description: Align the elements of an array literal if they span more than one line.
604
604
  StyleGuide: https://github.com/bbatsov/ruby-style-guide#align-multiline-arrays
605
605
  Enabled: true
@@ -627,7 +627,7 @@ Style/BlockComments:
627
627
  Description: Do not use block comments.
628
628
  StyleGuide: https://github.com/bbatsov/ruby-style-guide#no-block-comments
629
629
  Enabled: false
630
- Style/BlockEndNewline:
630
+ Layout/BlockEndNewline:
631
631
  Description: Put end statement of multiline block on its own line.
632
632
  Enabled: true
633
633
  Style/CaseEquality:
@@ -654,7 +654,7 @@ Style/ColonMethodCall:
654
654
  Description: 'Do not use :: for method call.'
655
655
  StyleGuide: https://github.com/bbatsov/ruby-style-guide#double-colons
656
656
  Enabled: false
657
- Style/CommentIndentation:
657
+ Layout/CommentIndentation:
658
658
  Description: Indentation of comments.
659
659
  Enabled: true
660
660
  Style/ConstantName:
@@ -679,19 +679,19 @@ Style/DoubleNegation:
679
679
  Style/EachWithObject:
680
680
  Description: Prefer `each_with_object` over `inject` or `reduce`.
681
681
  Enabled: false
682
- Style/ElseAlignment:
682
+ Layout/ElseAlignment:
683
683
  Description: Align elses and elsifs correctly.
684
684
  Enabled: true
685
685
  Style/EmptyElse:
686
686
  Description: Avoid empty else-clauses.
687
687
  Enabled: true
688
- Style/EmptyLines:
688
+ Layout/EmptyLines:
689
689
  Description: Don't use several empty lines in a row.
690
690
  Enabled: true
691
- Style/EmptyLinesAroundAccessModifier:
691
+ Layout/EmptyLinesAroundAccessModifier:
692
692
  Description: Keep blank lines around access modifiers.
693
693
  Enabled: true
694
- Style/EmptyLinesAroundMethodBody:
694
+ Layout/EmptyLinesAroundMethodBody:
695
695
  Description: Keeps track of empty lines around method bodies.
696
696
  Enabled: true
697
697
  Style/EmptyLiteral:
@@ -702,7 +702,7 @@ Style/EndBlock:
702
702
  Description: Avoid the use of END blocks.
703
703
  StyleGuide: https://github.com/bbatsov/ruby-style-guide#no-END-blocks
704
704
  Enabled: true
705
- Style/EndOfLine:
705
+ Layout/EndOfLine:
706
706
  Description: Use Unix-style line endings.
707
707
  StyleGuide: https://github.com/bbatsov/ruby-style-guide#crlf
708
708
  Enabled: true
@@ -718,10 +718,10 @@ Style/IfWithSemicolon:
718
718
  Description: Do not use if x; .... Use the ternary operator instead.
719
719
  StyleGuide: https://github.com/bbatsov/ruby-style-guide#no-semicolon-ifs
720
720
  Enabled: false
721
- Style/IndentationConsistency:
721
+ Layout/IndentationConsistency:
722
722
  Description: Keep indentation straight.
723
723
  Enabled: true
724
- Style/IndentArray:
724
+ Layout/IndentArray:
725
725
  Description: Checks the indentation of the first element in an array literal.
726
726
  Enabled: true
727
727
  Style/InfiniteLoop:
@@ -732,7 +732,7 @@ Style/Lambda:
732
732
  Description: Use the new lambda literal syntax for single-line blocks.
733
733
  StyleGuide: https://github.com/bbatsov/ruby-style-guide#lambda-multi-line
734
734
  Enabled: false
735
- Style/LeadingCommentSpace:
735
+ Layout/LeadingCommentSpace:
736
736
  Description: Comments should start with a space.
737
737
  StyleGuide: https://github.com/bbatsov/ruby-style-guide#hash-space
738
738
  Enabled: true
@@ -740,7 +740,7 @@ Style/LineEndConcatenation:
740
740
  Description: Use \ instead of + or << to concatenate two string literals at line
741
741
  end.
742
742
  Enabled: false
743
- Style/MethodCallParentheses:
743
+ Style/MethodCallWithoutArgsParentheses:
744
744
  Description: Do not use parentheses for method calls with no arguments.
745
745
  StyleGuide: https://github.com/bbatsov/ruby-style-guide#no-args-no-parens
746
746
  Enabled: true
@@ -752,7 +752,7 @@ Style/MultilineBlockChain:
752
752
  Description: Avoid multi-line chains of blocks.
753
753
  StyleGuide: https://github.com/bbatsov/ruby-style-guide#single-line-blocks
754
754
  Enabled: false
755
- Style/MultilineBlockLayout:
755
+ Layout/MultilineBlockLayout:
756
756
  Description: Ensures newlines after multiline block do statements.
757
757
  Enabled: false
758
758
  Style/MultilineIfThen:
@@ -820,56 +820,56 @@ Style/SelfAssignment:
820
820
  used.
821
821
  StyleGuide: https://github.com/bbatsov/ruby-style-guide#self-assignment
822
822
  Enabled: false
823
- Style/SpaceAfterColon:
823
+ Layout/SpaceAfterColon:
824
824
  Description: Use spaces after colons.
825
825
  StyleGuide: https://github.com/bbatsov/ruby-style-guide#spaces-operators
826
826
  Enabled: true
827
- Style/SpaceAfterComma:
827
+ Layout/SpaceAfterComma:
828
828
  Description: Use spaces after commas.
829
829
  StyleGuide: https://github.com/bbatsov/ruby-style-guide#spaces-operators
830
830
  Enabled: true
831
- Style/SpaceAroundKeyword:
831
+ Layout/SpaceAroundKeyword:
832
832
  Description: Use a space around keywords if appropriate.
833
833
  Enabled: true
834
- Style/SpaceAfterMethodName:
834
+ Layout/SpaceAfterMethodName:
835
835
  Description: Do not put a space between a method name and the opening parenthesis
836
836
  in a method definition.
837
837
  StyleGuide: https://github.com/bbatsov/ruby-style-guide#parens-no-spaces
838
838
  Enabled: true
839
- Style/SpaceAfterNot:
839
+ Layout/SpaceAfterNot:
840
840
  Description: Tracks redundant space after the ! operator.
841
841
  StyleGuide: https://github.com/bbatsov/ruby-style-guide#no-space-bang
842
842
  Enabled: true
843
- Style/SpaceAfterSemicolon:
843
+ Layout/SpaceAfterSemicolon:
844
844
  Description: Use spaces after semicolons.
845
845
  StyleGuide: https://github.com/bbatsov/ruby-style-guide#spaces-operators
846
846
  Enabled: true
847
- Style/SpaceBeforeComma:
847
+ Layout/SpaceBeforeComma:
848
848
  Description: No spaces before commas.
849
849
  Enabled: true
850
- Style/SpaceBeforeComment:
850
+ Layout/SpaceBeforeComment:
851
851
  Description: Checks for missing space between code and a comment on the same line.
852
852
  Enabled: true
853
- Style/SpaceBeforeFirstArg:
853
+ Layout/SpaceBeforeFirstArg:
854
854
  Description: Put a space between a method name and the first argument in a method
855
855
  call without parentheses.
856
856
  Enabled: true
857
- Style/SpaceBeforeSemicolon:
857
+ Layout/SpaceBeforeSemicolon:
858
858
  Description: No spaces before semicolons.
859
859
  Enabled: true
860
- Style/SpaceAroundOperators:
860
+ Layout/SpaceAroundOperators:
861
861
  Description: Use spaces around operators.
862
862
  StyleGuide: https://github.com/bbatsov/ruby-style-guide#spaces-operators
863
863
  Enabled: true
864
- Style/SpaceInsideBrackets:
864
+ Layout/SpaceInsideBrackets:
865
865
  Description: No spaces after [ or before ].
866
866
  StyleGuide: https://github.com/bbatsov/ruby-style-guide#no-spaces-braces
867
867
  Enabled: true
868
- Style/SpaceInsideParens:
868
+ Layout/SpaceInsideParens:
869
869
  Description: No spaces after ( or before ).
870
870
  StyleGuide: https://github.com/bbatsov/ruby-style-guide#no-spaces-braces
871
871
  Enabled: true
872
- Style/SpaceInsideRangeLiteral:
872
+ Layout/SpaceInsideRangeLiteral:
873
873
  Description: No spaces inside range literals.
874
874
  StyleGuide: https://github.com/bbatsov/ruby-style-guide#no-space-inside-range-literals
875
875
  Enabled: true
@@ -881,11 +881,11 @@ Style/StructInheritance:
881
881
  Description: Checks for inheritance from Struct.new.
882
882
  StyleGuide: https://github.com/bbatsov/ruby-style-guide#no-extend-struct-new
883
883
  Enabled: true
884
- Style/Tab:
884
+ Layout/Tab:
885
885
  Description: No hard tabs.
886
886
  StyleGuide: https://github.com/bbatsov/ruby-style-guide#spaces-indentation
887
887
  Enabled: true
888
- Style/TrailingWhitespace:
888
+ Layout/TrailingWhitespace:
889
889
  Description: Avoid trailing whitespace.
890
890
  StyleGuide: https://github.com/bbatsov/ruby-style-guide#no-trailing-whitespace
891
891
  Enabled: true
@@ -955,7 +955,7 @@ Lint/EnsureReturn:
955
955
  Description: Do not use return in an ensure block.
956
956
  StyleGuide: https://github.com/bbatsov/ruby-style-guide#no-return-ensure
957
957
  Enabled: true
958
- Lint/Eval:
958
+ Security/Eval:
959
959
  Description: The use of eval represents a serious security risk.
960
960
  Enabled: true
961
961
  Lint/HandleExceptions:
data/CHANGELOG.md CHANGED
@@ -2,6 +2,12 @@
2
2
  All notable changes to this project will be documented in this file.
3
3
  This project adheres to [Semantic Versioning](http://semver.org/).
4
4
 
5
+ ### v1.2.0
6
+
7
+ ##### Added
8
+
9
+ * Add :sendgrid_dev delivery method
10
+
5
11
  ### v1.1.0
6
12
 
7
13
  ##### Added
data/README.md CHANGED
@@ -18,7 +18,16 @@ gem "send_grid_mailer"
18
18
  bundle install
19
19
  ```
20
20
 
21
- In your environment file you need to add:
21
+ We provide two delivery methods. For development environments, where sending the email is not required, you can use `:sendgrid_dev` to open it in the browser:
22
+
23
+ ```ruby
24
+ config.action_mailer.delivery_method = :sendgrid_dev
25
+ config.action_mailer.sendgrid_settings = {
26
+ api_key: "YOUR-SENDGRID-API-KEY"
27
+ }
28
+ ```
29
+
30
+ Otherwise, you can use `:sendgrid` to actually send the email:
22
31
 
23
32
  ```ruby
24
33
  config.action_mailer.delivery_method = :sendgrid
@@ -27,6 +36,7 @@ config.action_mailer.sendgrid_settings = {
27
36
  }
28
37
  ```
29
38
 
39
+
30
40
  ## Usage
31
41
 
32
42
  With this adapter you will be able to:
@@ -2,32 +2,37 @@ module SendGridMailer
2
2
  class Api
3
3
  include Logger
4
4
 
5
- SUCCESS_CODE = 202
6
-
7
5
  def initialize(api_key)
8
6
  @api_key = api_key || raise(SendGridMailer::InvalidApiKey)
9
7
  end
10
8
 
11
9
  def send_mail(sg_definition)
12
10
  response = sg_api.client.mail._('send').post(request_body: sg_definition.to_json)
13
- handle_response(response)
11
+ handle_response(response, :mail)
12
+ end
13
+
14
+ def get_template(sg_definition)
15
+ response = sg_api.client.templates._(sg_definition.mail.template_id).get()
16
+ handle_response(response, :template)
14
17
  end
15
18
 
16
19
  private
17
20
 
18
- def handle_response(response)
19
- if response.status_code.to_i != SUCCESS_CODE
21
+ def handle_response(response, api_call_type)
22
+ status_code = response.status_code.to_i
23
+ if status_code.between?(400, 600)
20
24
  errors = response_errors(response)
21
- log_api_error_response(response.status_code, errors)
22
- raise SendGridMailer::ApiError.new(response.status_code, errors)
25
+ log_api_error_response(status_code, errors, api_call_type)
26
+ raise SendGridMailer::ApiError.new(status_code, errors)
23
27
  end
24
28
 
25
- log_api_success_response(response)
29
+ log_api_success_response(status_code, api_call_type)
26
30
  response
27
31
  end
28
32
 
29
33
  def response_errors(response)
30
- JSON.parse(response.body)["errors"]
34
+ body = JSON.parse(response.body)
35
+ body["errors"] || [{ "message" => body["error"] }]
31
36
  end
32
37
 
33
38
  def sg_api
@@ -0,0 +1,60 @@
1
+ module SendGridMailer
2
+ class DevDeliverer
3
+ include InterceptorsHandler
4
+ include Logger
5
+ require "letter_opener"
6
+
7
+ def deliver!(sg_definition)
8
+ @sg_definition = sg_definition
9
+ execute_interceptors(@sg_definition)
10
+ log_definition(@sg_definition)
11
+ letter_opener_delivery_method.deliver!(mail)
12
+ end
13
+
14
+ private
15
+
16
+ def sg_api
17
+ @sg_api ||= Api.new(api_key)
18
+ end
19
+
20
+ def api_key
21
+ Rails.application.config.action_mailer.sendgrid_dev_settings[:api_key]
22
+ rescue
23
+ nil
24
+ end
25
+
26
+ def letter_opener_delivery_method
27
+ @letter_opener_delivery_method ||= LetterOpener::DeliveryMethod.new(location: '/tmp/mails')
28
+ end
29
+
30
+ def parsed_template
31
+ template_response = sg_api.get_template(@sg_definition)
32
+ template_active_version = JSON.parse(template_response.body)["versions"].find do |version|
33
+ version["active"] == 1
34
+ end
35
+ template_content = template_active_version["html_content"]
36
+ @sg_definition.personalization.substitutions.each { |k, v| template_content.gsub!(k, v) }
37
+ template_content
38
+ end
39
+
40
+ def emails(origin)
41
+ @emails ||= {}
42
+ return @emails[origin] if @emails.has_key?(origin)
43
+
44
+ @emails[origin] = @sg_definition.personalization.send(origin)&.map {|em| em["email"]}
45
+ end
46
+
47
+ def mail
48
+ template = parsed_template.html_safe
49
+ m = Mail.new
50
+ m.html_part = template
51
+ m.subject = @sg_definition.personalization.subject
52
+ m.from = @sg_definition.mail.from["email"] if @sg_definition.mail.from.present?
53
+ m.to = emails(:tos) if emails(:tos).present?
54
+ m.cc = emails(:ccs) if emails(:ccs).present?
55
+ m.bcc = emails(:bccs) if emails(:bccs).present?
56
+
57
+ m
58
+ end
59
+ end
60
+ end
@@ -4,7 +4,7 @@ module SendGridMailer
4
4
 
5
5
  config.generators do |g|
6
6
  g.test_framework :rspec, fixture: false
7
- g.fixture_replacement :factory_girl, dir: "spec/factories"
7
+ g.fixture_replacement :factory_bot, dir: "spec/factories"
8
8
  end
9
9
 
10
10
  initializer "initialize" do
@@ -17,9 +17,11 @@ module SendGridMailer
17
17
  require_relative "./mailer_base_ext"
18
18
  end
19
19
 
20
- initializer "add_sendgrid_deliverer", before: "action_mailer.set_configs" do
20
+ initializer "add_sendgrid_deliverers", before: "action_mailer.set_configs" do
21
+ require_relative "./dev_deliverer"
21
22
  require_relative "./deliverer"
22
23
  ActionMailer::Base.add_delivery_method(:sendgrid, SendGridMailer::Deliverer)
24
+ ActionMailer::Base.add_delivery_method(:sendgrid_dev, SendGridMailer::DevDeliverer)
23
25
  end
24
26
  end
25
27
  end
@@ -20,12 +20,12 @@ module SendGridMailer
20
20
  log(build_definition_message(data))
21
21
  end
22
22
 
23
- def log_api_success_response(response)
24
- log("The E-mail was successfully sent :)\nStatus Code: #{response.status_code}")
23
+ def log_api_success_response(status_code, api_call_type)
24
+ log("Succesfully called the SendGrid API :)\nStatus Code: #{status_code}")
25
25
  end
26
26
 
27
- def log_api_error_response(status_code, errors)
28
- msg = "The E-mail was not sent :(\nStatus Code: #{status_code}\nErrors:"
27
+ def log_api_error_response(status_code, errors, api_call_type)
28
+ msg = "There was a problem calling the SendGrid API :(\nStatus Code: #{status_code}\nErrors:"
29
29
  msg += log_errors(errors)
30
30
  log(msg)
31
31
  end
@@ -23,7 +23,7 @@ module ActionMailer
23
23
 
24
24
  define_sg_mail(headers)
25
25
 
26
- SendGridMailer::Deliverer.new.deliver!(sg_definition)
26
+ deliverer&.new&.deliver!(sg_definition)
27
27
  end
28
28
 
29
29
  private
@@ -82,8 +82,17 @@ module ActionMailer
82
82
  @sg_definition ||= SendGridMailer::Definition.new
83
83
  end
84
84
 
85
+ def deliverer
86
+ case self.class.delivery_method
87
+ when :sendgrid_dev
88
+ SendGridMailer::DevDeliverer
89
+ when :sendgrid
90
+ SendGridMailer::Deliverer
91
+ end
92
+ end
93
+
85
94
  def enabled_sendgrid?
86
- self.class.delivery_method == :sendgrid
95
+ [:sendgrid, :sendgrid_dev].include?(self.class.delivery_method)
87
96
  end
88
97
  end
89
98
  end
@@ -1,3 +1,3 @@
1
1
  module SendGridMailer
2
- VERSION = "1.1.0"
2
+ VERSION = "1.2.0"
3
3
  end
@@ -21,11 +21,12 @@ Gem::Specification.new do |s|
21
21
 
22
22
  s.add_dependency "rails", ">= 4.2.0"
23
23
  s.add_dependency "sendgrid-ruby", "~> 4.0", ">= 4.0.4"
24
+ s.add_dependency "letter_opener", "~> 1.7.0"
24
25
  s.add_development_dependency "pry"
25
26
  s.add_development_dependency "pry-rails"
26
- s.add_development_dependency "sqlite3"
27
+ s.add_development_dependency "sqlite3", "~> 1.3.13"
27
28
  s.add_development_dependency "rspec-rails", "~> 3.4.0"
28
29
  s.add_development_dependency "guard-rspec", "~> 4.7"
29
- s.add_development_dependency "factory_girl_rails", "~> 4.6.0"
30
+ s.add_development_dependency "factory_bot_rails"
30
31
  s.add_development_dependency "coveralls"
31
32
  end
@@ -78,4 +78,10 @@ class TestMailer < ApplicationMailer
78
78
  substitute "%key2%", "value2"
79
79
  mail(body: "X")
80
80
  end
81
+
82
+ def template_with_substitutions_email(value)
83
+ set_template_id("XXX")
84
+ substitute "%key%", value
85
+ mail(to: "r1@platan.us", body: "X")
86
+ end
81
87
  end