Sha256: ac057f9484c005e23e35f19adda26667af7c35f156ad29ee68f04f17be31b41d

Contents?: true

Size: 1.62 KB

Versions: 1

Compression:

Stored size: 1.62 KB

Contents

module Evertils
  module Controller
    class Log < Controller::Base
      def pre_exec
        super

        @note_helper = Evertils::Helper.load('Note')
        @search_grammar = Evertils::Helper.load('SearchGrammar')
        @api_helper = Evertils::Helper.load('ApiEnmlHandler', @config)
      end

      # Send arbitrary text to the daily log
      def message(text = nil)
        return Notify.error('A message is required') if text.nil?

        # TODO: should be an object instead of arbitrary hash
        criteria = {
          notebook: :Daily,
          tags: {
            day: Date.today.yday
          },
          created: Date.new(Date.today.year, 1, 1).strftime('%Y%m%d')
        }

        note = @note_helper.wait_for_with_grammar(criteria)

        return Notify.error('Note not found') if note.entity.nil?

        modify(note, text)
      end

      private

      # Update a note with content
      def modify(note, text)
        xml = @api_helper.from_str(note.entity.content)

        time = Time.now.strftime('%I:%M')
        target = xml.search('en-note').first

        return Notify.error('Unable to log message, triage section not found') if target.nil?

        log_message_txt = "<div>* #{time} - #{text}</div>"

        # append the log message to the target
        target.add_child(log_message_txt)

        # remove XML processing definition if it is the second element
        if xml.children[1].is_a?(Nokogiri::XML::ProcessingInstruction)
          xml.children[1].remove
        end

        note.entity.content = xml.to_s

        Notify.success("Item logged at #{time}") if note.update
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
evertils-1.0.11 lib/evertils/controllers/log.rb