Skip to content

Commit f3e209c

Browse files
author
Ashu Goel
committed
[template] use SparkPost.Content.Inline instead of custom ContentResponse
1 parent 08720cf commit f3e209c

File tree

4 files changed

+16
-40
lines changed

4 files changed

+16
-40
lines changed

lib/content/inline.ex

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,12 @@ defmodule SparkPost.Content.Inline do
2525
html: nil,
2626
attachments: nil,
2727
inline_images: nil
28+
29+
@doc """
30+
Convert a raw "from" field into a %SparkPost.Address{} object.
31+
"""
32+
def convert_from_field(%SparkPost.Endpoint.Error{} = content), do: content
33+
def convert_from_field(%__MODULE__{} = content) do
34+
%{content | from: SparkPost.Address.to_address(content.from)}
35+
end
2836
end

lib/template.ex

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ defmodule SparkPost.Template do
1717
-headers
1818
"""
1919

20-
alias SparkPost.{Endpoint, Template}
20+
alias SparkPost.Endpoint
2121

2222
@doc """
2323
Generate a preview of an existing template.
@@ -41,7 +41,7 @@ defmodule SparkPost.Template do
4141
body = %{substitution_data: substitution_data}
4242
:post
4343
|> Endpoint.request("/templates/#{template.template_id}/preview#{qs}", body)
44-
|> Endpoint.marshal_response(Template.ContentResponse)
45-
|> Template.ContentResponse.convert_from_field
44+
|> Endpoint.marshal_response(SparkPost.Content.Inline)
45+
|> SparkPost.Content.Inline.convert_from_field
4646
end
4747
end

lib/template/content_response.ex

Lines changed: 0 additions & 31 deletions
This file was deleted.

test/template_test.exs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
defmodule SparkPost.TemplateTest do
22
use ExUnit.Case, async: false
33

4-
alias SparkPost.Content.TemplateRef
4+
alias SparkPost.Content.{TemplateRef, Inline}
55
alias SparkPost.{Endpoint, MockServer, Template}
6-
alias SparkPost.Template.ContentResponse
76

87
import Mock
98

@@ -24,7 +23,7 @@ defmodule SparkPost.TemplateTest do
2423
end
2524
end
2625

27-
test_with_mock "Template.preview succeeds with Template.ContentResponse",
26+
test_with_mock "Template.preview succeeds with Content.Inline",
2827
HTTPoison, [request: fn (method, url, body, headers, opts) ->
2928
assert method == :post
3029
# draft not set
@@ -33,18 +32,18 @@ defmodule SparkPost.TemplateTest do
3332
fun.(method, url, body, headers, opts)
3433
end] do
3534
resp = Template.preview(TestStruct.basic_template(), TestStruct.substitution_data())
36-
assert %ContentResponse{} = resp
35+
assert %Inline{} = resp
3736
end
3837

39-
test_with_mock "Template.preview succeeds with Template.ContentResponse and draft set",
38+
test_with_mock "Template.preview succeeds with Content.Inline and draft set",
4039
HTTPoison, [request: fn (method, url, body, headers, opts) ->
4140
assert method == :post
4241
assert String.ends_with?(url, "preview?draft=true")
4342
fun = MockServer.mk_http_resp(200, MockServer.get_json("previewtemplate"))
4443
fun.(method, url, body, headers, opts)
4544
end] do
4645
resp = Template.preview(TestStruct.template_with_draft(), TestStruct.substitution_data())
47-
assert %ContentResponse{} = resp
46+
assert %Inline{} = resp
4847
end
4948

5049
test_with_mock "Template.preview fails with Endpoint.Error", HTTPoison,

0 commit comments

Comments
 (0)