Class: Diane::Recorder

Inherits:
Object
  • Object
show all
Defined in:
lib/diane/recorder.rb

Overview

Records messages and metadata

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message, opts) ⇒ Recorder

Returns a new instance of Recorder.



8
9
10
11
12
13
# File 'lib/diane/recorder.rb', line 8

def initialize(message, opts)
  abort 'Message is Nil. Fuck off.'.magenta if message.nil?
  @message  = message
  @user     = slug(opts.fetch(:user, USER))
  @time     = Time.now.asctime
end

Instance Attribute Details

#userObject (readonly)

Returns the value of attribute user.



6
7
8
# File 'lib/diane/recorder.rb', line 6

def user
  @user
end

Instance Method Details

#recordObject

generates new recording as csv row to new or existing DIANE file



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/diane/recorder.rb', line 17

def record
  if File.exist? DIFILE
    CSV.open(DIFILE, 'a') { |csv| csv << [@user, @message, @time] }
  else
    CSV.open(DIFILE, 'a') do |csv|
      csv << %w[user message time]
      csv << [@user, @message, @time]
    end
  end
  puts ''.green
rescue StandardError => e
  abort 'Broken'.magenta + e
end

#slug(user) ⇒ Object

normalizes and slugifies recording user handle



33
34
35
36
# File 'lib/diane/recorder.rb', line 33

def slug(user)
  abort 'User is nil. Fuck off.'.magenta if user.nil?
  user.downcase.strip.tr(' ', '_').gsub(/[^\w-]/, '')
end