Class: WGU::Sentence

Inherits:
Object
  • Object
show all
Defined in:
lib/pps_commons/sentence.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(recipient, msg) ⇒ Sentence

Returns a new instance of Sentence.



8
9
10
11
12
# File 'lib/pps_commons/sentence.rb', line 8

def initialize(recipient, msg)
  @recipient = recipient
  @message = (msg.kind_of?(WGU::Sentence) ? msg.log_entry : msg.to_s)
  @message.strip!
end

Instance Attribute Details

#messageObject (readonly)

Returns the value of attribute message.



6
7
8
# File 'lib/pps_commons/sentence.rb', line 6

def message
  @message
end

#recipientObject (readonly)

Returns the value of attribute recipient.



5
6
7
# File 'lib/pps_commons/sentence.rb', line 5

def recipient
  @recipient
end

Instance Method Details

#add_need(need) ⇒ Object



14
15
16
# File 'lib/pps_commons/sentence.rb', line 14

def add_need(need)
  with_clause =~ /#{need}/ ? @message : add_need!(need)
end

#add_need!(need) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/pps_commons/sentence.rb', line 18

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

#categoryObject



34
35
36
# File 'lib/pps_commons/sentence.rb', line 34

def category
  request? ? :request : :response
end

#has_more_needs?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/pps_commons/sentence.rb', line 38

def has_more_needs?
  !!(message.index(' needs '))
end

#location_informationObject

Hostname or ipaddress from message



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/pps_commons/sentence.rb', line 43

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_entryObject



55
56
57
# File 'lib/pps_commons/sentence.rb', line 55

def log_entry
  @log_entry || "#{message.to_s}\n"
end

#mark_completed!(need) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/pps_commons/sentence.rb', line 59

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) ⇒ Object



74
75
76
77
78
# File 'lib/pps_commons/sentence.rb', line 74

def mark_incompleted!(need)
  mark_completed!(need)
  message.gsub!(/(with)?(and)?\s!?#{need}/, '')
  add_need!("!#{need}")
end

#needsObject



80
81
82
83
84
85
86
87
# File 'lib/pps_commons/sentence.rb', line 80

def needs
  if has_more_needs?
    with_clause.split(' and ')
      .map { |need| need.gsub!(/(needs\s)/, '') }
  else
    []
  end
end

#new_request?Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/pps_commons/sentence.rb', line 100

def new_request?
  !!(with_clause.empty? && needs.empty?)
end

#next_tickObject



89
90
91
92
93
94
95
96
97
98
# File 'lib/pps_commons/sentence.rb', line 89

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

#prepositionsObject



104
105
106
107
108
109
110
111
112
113
# File 'lib/pps_commons/sentence.rb', line 104

def prepositions
  bare_patterns = [
    /\sunder\s/, /\sfor\s/, /\sover\s/, /\swithin\s/, /\saliased\s/
  ]
  if block_given?
    Regexp.union(bare_patterns.map { |pat| yield pat })
  else
    Regexp.union(bare_patterns)
  end
end

#request?Boolean

Returns:

  • (Boolean)


115
116
117
# File 'lib/pps_commons/sentence.rb', line 115

def request?
  !!(message =~ /^please/)
end

#secondary_subjectObject



126
127
128
# File 'lib/pps_commons/sentence.rb', line 126

def secondary_subject
  subject.gsub(/.+for(.*)$/, '\1').strip
end

#sentimentObject



130
131
132
133
134
# File 'lib/pps_commons/sentence.rb', line 130

def sentiment
  @sentiment || (
    (message =~ /#{success_patterns}$/) ? :success : :fail
  )
end

#simple_subjectObject



119
120
121
122
123
124
# File 'lib/pps_commons/sentence.rb', line 119

def simple_subject
  subject.gsub(
    /(.*?)#{prepositions { |prep| /#{prep.source + '.*'}/ }}/,
    '\1'
  )
end

#subject(msg = message) ⇒ Object



136
137
138
139
140
141
142
143
144
145
# File 'lib/pps_commons/sentence.rb', line 136

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_patternsObject



147
148
149
150
151
152
# File 'lib/pps_commons/sentence.rb', line 147

def success_patterns
  Regexp.union([
    /(connected)/, /(success)/, /(succeeded)/, /(successfull)/,
    /(working)/, /(works)/, /(linked)/, /(done)/, /(skipped)/
  ])
end

#ticked!(status) ⇒ Object



154
155
156
157
158
159
160
161
162
# File 'lib/pps_commons/sentence.rb', line 154

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

#verbObject



164
165
166
167
# File 'lib/pps_commons/sentence.rb', line 164

def verb
  words = message.split(' ')
  request? ? words[1] : words.last
end

#with_clauseObject



169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/pps_commons/sentence.rb', line 169

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