Class: Webhookdb::Tasks::Message

Inherits:
Rake::TaskLib
  • Object
show all
Defined in:
lib/webhookdb/tasks/message.rb

Instance Method Summary collapse

Constructor Details

#initializeMessage

Returns a new instance of Message.



7
8
9
10
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
# File 'lib/webhookdb/tasks/message.rb', line 7

def initialize
  super()
  namespace :message do
    desc "Render the specified message"
    task :render, [:template_class, :out] do |_t, args|
      template_class_name = args[:template_class] or
        raise "Provide the template class name (NewCustomer or new_customer) as the first argument"
      outpath = args[:out]
      outpath = nil if outpath.blank? || outpath == "-"
      if outpath
        html_io = File.open(outpath, "w")
        feedback_io = $stdout
      else
        html_io = $stdout
        feedback_io = $stderr
      end

      ENV["WEBHOOKDB_DB_SLOW_QUERY_SECONDS"] ||= "1"
      require "webhookdb"
      Webhookdb.load_app
      SemanticLogger.appenders.to_a.each { |a| SemanticLogger.remove_appender(a) }
      SemanticLogger.add_appender(io: feedback_io)

      commit = Webhookdb::RACK_ENV != "test"
      clsname = template_class_name.classify
      (clsname += "s") if template_class_name.end_with?("s") && !clsname.end_with?("s")
      delivery = Webhookdb::Message::Delivery.preview(clsname, commit:)
      feedback_io << "*** Created MessageDelivery: #{delivery.values}\n\n"
      feedback_io << delivery.body_with_mediatype!("text/plain")&.content
      feedback_io << "\n\n"
      if outpath
        feedback_io << "*** Writing HTML output to #{outpath}\n"
      elsif html_io.tty?
        feedback_io << "*** Writing HTML output to stdout.\n"
        feedback_io << "*** Redirect it to a file (> temp.html), pass OUT to write it to a file (OUT=temp.html),\n"
        feedback_io << "*** or view it at /admin_api/v1/message_deliveries/last\n\n"
      else
        feedback_io << "*** Writing HTML output to redirected stdout.\n"
      end
      html_io << delivery.body_with_mediatype!("text/html")&.content
      html_io << "\n"
    end
  end
end