lib/roda/plugins/mailer.rb in roda-2.27.0 vs lib/roda/plugins/mailer.rb in roda-2.28.0

- old
+ new

@@ -108,21 +108,32 @@ # web requests is shared. However, it's recommended that you create a # separate Roda application for emails. This can be a subclass of your main # Roda application if you want your helper methods to automatically be # available in your email views. module Mailer + OPTS = {}.freeze + REQUEST_METHOD = "REQUEST_METHOD".freeze + RodaPlugins.deprecate_constant(self, :REQUEST_METHOD) PATH_INFO = "PATH_INFO".freeze + RodaPlugins.deprecate_constant(self, :PATH_INFO) SCRIPT_NAME = 'SCRIPT_NAME'.freeze + RodaPlugins.deprecate_constant(self, :SCRIPT_NAME) EMPTY_STRING = ''.freeze + RodaPlugins.deprecate_constant(self, :EMPTY_STRING) RACK_INPUT = 'rack.input'.freeze + RodaPlugins.deprecate_constant(self, :RACK_INPUT) RODA_MAIL = 'roda.mail'.freeze + RodaPlugins.deprecate_constant(self, :RODA_MAIL) RODA_MAIL_ARGS = 'roda.mail_args'.freeze + RodaPlugins.deprecate_constant(self, :RODA_MAIL_ARGS) MAIL = "MAIL".freeze + RodaPlugins.deprecate_constant(self, :MAIL) CONTENT_TYPE = 'Content-Type'.freeze + RodaPlugins.deprecate_constant(self, :CONTENT_TYPE) TEXT_PLAIN = "text/plain".freeze - OPTS = {}.freeze + RodaPlugins.deprecate_constant(self, :TEXT_PLAIN) # Error raised when the using the mail class method, but the routing # tree doesn't return the mail object. class Error < ::Roda::RodaError; end @@ -137,11 +148,11 @@ # and arguments. You can further manipulate the returned mail object before # calling +deliver+ to send the mail. def mail(path, *args) mail = ::Mail.new catch(:no_mail) do - unless mail.equal?(new(PATH_INFO=>path, SCRIPT_NAME=>EMPTY_STRING, REQUEST_METHOD=>MAIL, RACK_INPUT=>StringIO.new, RODA_MAIL=>mail, RODA_MAIL_ARGS=>args).call(&route_block)) + unless mail.equal?(new("PATH_INFO"=>path, 'SCRIPT_NAME'=>'', "REQUEST_METHOD"=>"MAIL", 'rack.input'=>StringIO.new, 'roda.mail'=>mail, 'roda.mail_args'=>args).call(&route_block)) raise Error, "route did not return mail instance for #{path.inspect}, #{args.inspect}" end mail end end @@ -159,13 +170,13 @@ # only if the request method is MAIL (only set when using the Roda class # +mail+ or +sendmail+ methods) and the rest of the arguments match # the request. This yields any of the captures to the block, as well as # any arguments passed to the +mail+ or +sendmail+ Roda class methods. def mail(*args) - if @env[REQUEST_METHOD] == MAIL + if @env["REQUEST_METHOD"] == "MAIL" if_match(args) do |*vs| - yield(*(vs + @env[RODA_MAIL_ARGS])) + yield(*(vs + @env['roda.mail_args'])) end end end end @@ -177,11 +188,11 @@ # to the email, as well as adding the response body to the email. # Return the email unless no body was set for it, which would indicate # that the routing tree did not handle the request. def finish if m = mail - header_content_type = @headers.delete(CONTENT_TYPE) + header_content_type = @headers.delete('Content-Type') m.headers(@headers) m.body(@body.join) unless @body.empty? mail_attachments.each do |a, block| m.add_file(*a) block.call if block @@ -189,11 +200,11 @@ if content_type = header_content_type || roda_class.opts[:mailer][:content_type] if mail.multipart? if mail.content_type =~ /multipart\/mixed/ && mail.parts.length >= 2 && - (part = mail.parts.find{|p| !p.attachment && p.content_type == TEXT_PLAIN}) + (part = mail.parts.find{|p| !p.attachment && p.content_type == "text/plain"}) part.content_type = content_type end else mail.content_type = content_type end @@ -215,11 +226,11 @@ module InstanceMethods # Add delegates for common email methods. [:from, :to, :cc, :bcc, :subject].each do |meth| define_method(meth) do |*args| - env[RODA_MAIL].send(meth, *args) + env['roda.mail'].send(meth, *args) nil end end [:text_part, :html_part].each do |meth| define_method(meth) do |*args| @@ -229,14 +240,14 @@ # If this is an email request, set the mail object in the response, as well # as the default content_type for the email. def initialize(env) super - if mail = env[RODA_MAIL] + if mail = env['roda.mail'] res = @_response res.mail = mail - res.headers.delete(CONTENT_TYPE) + res.headers.delete('Content-Type') end end # Delay adding a file to the message until after the message body has been set. # If a block is given, the block is called after the file has been added, and you @@ -254,10 +265,10 @@ private # Set the text_part or html_part (depending on the method) in the related email, # using the given body and optional headers. def _mail_part(meth, body, headers=nil) - env[RODA_MAIL].send(meth) do + env['roda.mail'].send(meth) do body(body) headers(headers) if headers end nil end