Class: Detroit::Email

Inherits:
Tool
  • Object
show all
Includes:
EmailUtils, Standard
Defined in:
lib/detroit-email.rb

Overview

The Email tool is used to send out project announcements to a set of email addresses.

By default it generates a *release announcement* based on a projects metadata. Release announcements can be customized via parts, including a project’s README file.

Fully custom messages can also be sent by setting the message field, and or by using a message file.

Email account settings default to environment variables so they need not be set independently for every project.

@server    ENV['EMAIL_SERVER']
@from      ENV['EMAIL_FROM']
@account   ENV['EMAIL_ACCOUNT'] || ENV['EMAIL_FROM']
@password  ENV['EMAIL_PASSWORD']
@port      ENV['EMAIL_PORT']
@domain    ENV['EMAIL_DOMAIN']
@login     ENV['EMAIL_LOGIN']
@secure    ENV['EMAIL_SECURE']

This tool ties into the ‘prepare` and `promote` stations of the standard toolchain.

Constant Summary collapse

MANPAGE =

Location of manpage for this tool.

File.dirname(__FILE__) + '/../man/detroit-email.5'

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#accountObject

Email account name (defaults to from).



91
92
93
# File 'lib/detroit-email.rb', line 91

def 
  @account
end

#domainObject

User domain (not sure why SMTP requires this?).



94
95
96
# File 'lib/detroit-email.rb', line 94

def domain
  @domain
end

#fileObject

Message file to send.



70
71
72
# File 'lib/detroit-email.rb', line 70

def file
  @file
end

#fromObject

Email address from whom.



79
80
81
# File 'lib/detroit-email.rb', line 79

def from
  @from
end

#loginObject

Login type (plain, login).



97
98
99
# File 'lib/detroit-email.rb', line 97

def 
  @login
end

#mailtoObject Also known as: to

List of email addresses to whom to email.



73
74
75
# File 'lib/detroit-email.rb', line 73

def mailto
  @mailto
end

#noenvObject

Do not use environment variables for email defaults.



106
107
108
# File 'lib/detroit-email.rb', line 106

def noenv
  @noenv
end

#partsObject

Components of announcment.



103
104
105
# File 'lib/detroit-email.rb', line 103

def parts
  @parts
end

#portObject

Emails server port (default is usually correct).



88
89
90
# File 'lib/detroit-email.rb', line 88

def port
  @port
end

#secureObject

Use TLS/SSL true or false?



100
101
102
# File 'lib/detroit-email.rb', line 100

def secure
  @secure
end

#serverObject

Email server.



85
86
87
# File 'lib/detroit-email.rb', line 85

def server
  @server
end

#subjectObject

Subject line (default is “ANN: title version”).



82
83
84
# File 'lib/detroit-email.rb', line 82

def subject
  @subject
end

Instance Method Details

#announceObject

Send announcement message.



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/detroit-email.rb', line 125

def announce
  apply_environment unless @approved

  mailopts = self.mailopts

  if mailto.empty?
    report "No recipents given."
  else
    if trial?
      subject = mailopts['subject']
      mailto  = mailopts['to'].flatten.join(", ")
      report "email '#{subject}' to #{mailto}"
    else
      #emailer = Emailer.new(mailopts)
      #emailer.email
      if @approved
        email(mailopts)
      else
        exit -1
      end
    end
  end
end

#apply_environmentObject

Apply environment settings.



208
209
210
211
212
213
214
215
216
217
218
# File 'lib/detroit-email.rb', line 208

def apply_environment
  return if noenv
  @server   ||= ENV['EMAIL_SERVER']
  @from     ||= ENV['EMAIL_FROM']    || ENV['EMAIL_ACCOUNT']
  @account  ||= ENV['EMAIL_ACCOUNT'] || ENV['EMAIL_FROM']
  @password ||= ENV['EMAIL_PASSWORD']
  @port     ||= ENV['EMAIL_PORT']
  @domain   ||= ENV['EMAIL_DOMAIN']
  @login    ||= ENV['EMAIL_LOGIN']
  @secure   ||= ENV['EMAIL_SECURE']
end

#approveObject

Ask developer if the mail should be sent.



114
115
116
117
118
119
# File 'lib/detroit-email.rb', line 114

def approve
  apply_environment
  @approved = mail_confirm?
  # TODO: better way to terminate?
  exit -1 unless @approved
end

#assemble?(station, options = {}) ⇒ Boolean

Returns:

  • (Boolean)


237
238
239
240
241
# File 'lib/detroit-email.rb', line 237

def assemble?(station, options={})
  return true if station == :prepare && approve?(options)
  return true if station == :promote
  return false
end

#mail_confirm?Boolean

Confirm announcement

Returns:

  • (Boolean)


168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/detroit-email.rb', line 168

def mail_confirm?
  if mailto
    return true if force?
    to  = [mailto].flatten.join(", ")
    ans = ask("Announce to #{to} [(v)iew (y)es (N)o]? ")
    case ans.downcase
    when 'y', 'yes'
      true
    when 'v', 'view'
      puts "From: #{from}"
      puts "To: #{to}"
      puts "Subject: #{subject}"
      puts
      puts message
      mail_confirm?
    else
      false
    end
  end
end

#mailoptsObject



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/detroit-email.rb', line 190

def mailopts
  options = {
    'message' => self.message,
    'to'      => self.to,
    'from'    => self.from,
    'subject' => self.subject,
    'server'  => self.server,
    'port'    => self.port,
    'account' => self.,
    'domain'  => self.domain,
    'login'   => self.,
    'secure'  => self.secure
  }
  options.delete_if{|k,v| v.nil?}
  options
end

#messageObject

Message to send. Defaults to a generated release announcement.



155
156
157
158
159
160
161
162
163
164
165
# File 'lib/detroit-email.rb', line 155

def message
  @message ||= (
    path = Dir[file].first if file
    if path
      project.announcement(File.new(file))
    else
      parts.map{ |part| /^file:\/\// =~ part.to_s ? $' : part }
      project.announcement(*parts)
    end
  )
end

#prepareObject

Alias for #approve.



122
# File 'lib/detroit-email.rb', line 122

def prepare; approve; end

#prerequisitevoid

This method returns an undefined value.

Load any special requirements and set attribute defaults.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/detroit-email.rb', line 51

def prerequisite
  require 'facets/boolean'  # TODO: should this universal?

  @mailto   = ['[email protected]']
  @subject  = "[ANN] %s v%s released" % [.title, .version]
  @file     = nil #'doc/ANN{,OUNCE}{,.txt,.rdoc}' TODO: default announcment file?
  @parts    = [:message, :description, :resources, :notes, :changes]

  #mailopts = Ratch::Emailer.environment_options.rekey(&:to_s)  # FIXME
  #@port    = mailopts['port']
  #@server  = mailopts['server']
  #@account = mailopts['account']  #|| metadata.email
  #@domain  = mailopts['domain']   #|| metadata.domain
  #@login   = mailopts['login']
  #@secure  = mailopts['secure']
  #@from    = mailopts['from']     #|| metadata.email
end

#promoteObject

Alias for #announce.



150
151
152
# File 'lib/detroit-email.rb', line 150

def promote
  announce
end