class WGU::Sentence

Attributes

message[R]
recipient[R]

Public Class Methods

new(recipient, msg) click to toggle source
# File lib/pps_commons/sentence.rb, line 10
def initialize(recipient, msg)
  @recipient = recipient
  @message = (msg.kind_of?(WGU::Sentence) ? msg.log_entry : msg.to_s)
  @message.strip!
end

Public Instance Methods

add_need(need) click to toggle source
# File lib/pps_commons/sentence.rb, line 16
def add_need(need)
  with_clause =~ /#{need}/ ? @message : add_need!(need)
end
add_need!(need) click to toggle source
# File lib/pps_commons/sentence.rb, line 20
def add_need!(need)
  if with_clause.empty?
    @message << " with needs #{need}"
  else
    tmp = if with_clause =~ /needs/
      "needs #{need} and"
    else
      "needs #{need}"
    end

    new_with_clause = "#{tmp} #{with_clause}"
    @message.gsub!(/#{with_clause}/, new_with_clause).strip!
    @message
  end
end
category() click to toggle source
# File lib/pps_commons/sentence.rb, line 36
def category
  request? ? :request : :response
end
has_more_needs?() click to toggle source
# File lib/pps_commons/sentence.rb, line 40
def has_more_needs?
  !!(message.index(' needs '))
end
location_information() click to toggle source

Hostname or ipaddress from message

# File lib/pps_commons/sentence.rb, line 45
def location_information
  if message.scan(/\s([a-z0-9\-]+\.wgu\.edu)\s/).count > 0
    message.gsub(/.+\s([a-z0-9\-]+\.wgu\.edu)\s.+/, '\1')
  elsif message.scan(/(\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b)/).count > 0
    message.gsub(/.+(\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b).+/, '\1')
  elsif message.scan(/WGU_UT\\[a-z\-0-9]+\s/i).count > 0
    message.gsub(/.+WGU_UT\\([a-z\-0-9]+)\s.*/i, '\1')
  else
    message.gsub(/.+for\s\w+\son\s([\w\-]+)\swith.*/, '\1')
  end
end
log_entry() click to toggle source
# File lib/pps_commons/sentence.rb, line 57
def log_entry
  @log_entry || "#{message.to_s}\n"
end
mark_completed!(need) click to toggle source
# File lib/pps_commons/sentence.rb, line 61
def mark_completed!(need)
  matched_needs = needs.select { |nd| /\!?#{need}/ =~ nd }
  if matched_needs.count > 1
    "alert human #{message} has too many validations that match the name \"#{need}\" so it failed"
  else
    suffix = with_clause[/needs\s\!?#{Regexp.escape(matched_needs.first)}\s?/]
    new_suffix = suffix.gsub(/needs\s/, '')
    message.gsub!(/#{Regexp.escape(suffix)}/, new_suffix).strip!
    words = message.split(' ')
    prefix = words[0..words.index('for')].join(' ')
    message.gsub!(/#{Regexp.escape(prefix)}/, '').strip!
    message
  end
end
mark_incompleted!(need) click to toggle source
# File lib/pps_commons/sentence.rb, line 76
def mark_incompleted!(need)
  mark_completed!(need)
  message.gsub!(/(with)?(and)?\s!?#{need}/, '')
  add_need!("!#{need}")
end
needs() click to toggle source
# File lib/pps_commons/sentence.rb, line 82
def needs
  if has_more_needs?
    with_clause.split(' and ')
      .map { |need| need.gsub!(/(needs\s)/, '') }
  else
    []
  end
end
new_request?() click to toggle source
# File lib/pps_commons/sentence.rb, line 102
def new_request?
  !!(with_clause.empty? && needs.empty?)
end
next_tick() click to toggle source
# File lib/pps_commons/sentence.rb, line 91
def next_tick
  if has_more_needs?
    next_need =
      needs.find { |need| need !~ /^!/ } ||
      (needs.find { |need| need =~ /^!/ }.gsub('!', '') rescue '')
    "validate #{next_need}"
  else
    [verb, simple_subject].join(' ')
  end
end
prepositions() { |pat| ... } click to toggle source
# File lib/pps_commons/sentence.rb, line 106
def prepositions
  bare_patterns = [
    /\sunder\s/, /\sfor\s/, /\sover\s/, /\swithin\s/, /\saliased\s/, /\sat\s/,
    /\severy\s/
  ]
  if block_given?
    Regexp.union(bare_patterns.map { |pat| yield pat })
  else
    Regexp.union(bare_patterns)
  end
end
request?() click to toggle source
# File lib/pps_commons/sentence.rb, line 118
def request?
  !!(message =~ /^please/)
end
secondary_subject() click to toggle source
# File lib/pps_commons/sentence.rb, line 129
def secondary_subject
  subject.gsub(/.+for(.*)$/, '\1').strip
end
sentiment() click to toggle source
# File lib/pps_commons/sentence.rb, line 133
def sentiment
  @sentiment || (
    (message =~ /#{success_patterns}$/) ? :success : :fail
  )
end
simple_subject() click to toggle source
# File lib/pps_commons/sentence.rb, line 122
def simple_subject
  subject.gsub(
    /(.*?)#{prepositions { |prep| /#{prep.source + '.*'}/ }}/,
    '\1'
  )
end
subject(msg = message) click to toggle source
# File lib/pps_commons/sentence.rb, line 139
def subject(msg = message)
  words = msg.split
  if request?
    lower_bound = 2
    upper_bound = (words.reverse.index('with') || words.count)
    words[lower_bound...upper_bound].join(' ')
  else
    words[0..-2].join(' ')
  end
end
success_patterns() click to toggle source
# File lib/pps_commons/sentence.rb, line 150
def success_patterns
  Regexp.union([
    /(connected)/, /(success)/, /(succeeded)/, /(successfull)/,
    /(working)/, /(works)/, /(linked)/, /(done)/, /(skipped)/
  ])
end
ticked!(status) click to toggle source
# File lib/pps_commons/sentence.rb, line 157
def ticked!(status)
  words = message.split(' ')
  words.shift # get rid of "please"
  words.first.gsub!(/(\w+)e$/, "#{'\1'}ion") # change from imperative to declarative
  words.insert(1, 'for') # finish changing to declarative sentence
  words.push(status) # add the status of the task that ran to the end
  @message = words.join(' ')
  message
end
verb() click to toggle source
# File lib/pps_commons/sentence.rb, line 167
def verb
  words = message.split(' ')
  request? ? words[1] : words.last
end
with_clause() click to toggle source
# File lib/pps_commons/sentence.rb, line 172
def with_clause
  return '' unless message =~ /with/
  msg = message.gsub(/.+with\s(.+)$/, '\1')
  if request?
    msg
  else
    # pop the response status off, when dealing with a response
    tmp = msg.split(' ')
    tmp.pop
    tmp.join(' ').strip
  end
end