11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
# File 'lib/inkcite/cli/scope.rb', line 11
def self.invoke email, opts
email.upload
puts "Scoping your email ..."
config = email.config[:litmus]
has_litmus = !config.blank?
if has_litmus
username = config[:username]
password = config[:password]
end
uri = URI.parse('https://litmus.com/scope/api/v1/emails/')
https = Net::HTTP.new(uri.host, uri.port)
https.use_ssl = true
versions = Array(opts[:version] || email.versions)
versions.each do |version|
view = email.view(:preview, :email, version)
subject = view.subject
mail = Mail.new do
from '"Inkcite" <[email protected]>'
to '"Awesome Designer" <[email protected]>'
subject subject
html_part do
content_type 'text/html; charset=UTF-8'
body view.render!
end
end
scope_request = Net::HTTP::Post.new(uri.path)
scope_request.basic_auth(username, password) unless username.blank?
scope_request.set_form_data('email[source]' => mail.to_s)
begin
response = https.request(scope_request)
case response
when Net::HTTPSuccess
result = JSON.parse(response.body)
slug = result['email']['slug']
puts "'#{subject}' shared to https://litmus.com/scope/#{slug}"
when Net::HTTPUnauthorized
abort <<-ERROR.strip_heredoc
Oops! Inkcite wasn't able to scope your email because of an
authentication problem with Litmus. Please check the settings
in config.yml:
litmus:
username: '#{username}'
password: '#{password}'
ERROR
when Net::HTTPServerError
abort <<-ERROR.strip_heredoc
Oops! Inkcite wasn't able to scope your email because Litmus'
server returned an error. Please try again later.
#{response.message}
ERROR
else
raise response.message
end
rescue Exception => e
abort <<-ERROR.strip_heredoc
Oops! Inkcite wasn't able to scope your email because of an
unexpected error. Please try again later.
#{e.message}
ERROR
end
end
unless has_litmus
puts 'Note! Your scoped email will expire in 15 days.'
end
true
end
|