Class: WGU::Sentence

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from PPSCommons

deep_find, included, unleash_the_fury_level

Constructor Details

#initialize(recipient, msg) ⇒ Sentence

Returns a new instance of Sentence.



10
11
12
13
14
# 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

Instance Attribute Details

#messageObject (readonly)

Returns the value of attribute message.



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

def message
  @message
end

#recipientObject (readonly)

Returns the value of attribute recipient.



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

def recipient
  @recipient
end

Instance Method Details

#add_need(need) ⇒ Object



16
17
18
# File 'lib/pps_commons/sentence.rb', line 16

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

#add_need!(need) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# 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

#categoryObject



36
37
38
# File 'lib/pps_commons/sentence.rb', line 36

def category
  request? ? :request : :response
end

#has_more_needs?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/pps_commons/sentence.rb', line 40

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

#location_informationObject

Hostname or ipaddress from message



45
46
47
48
49
50
51
52
53
54
55
# 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_entryObject



57
58
59
# File 'lib/pps_commons/sentence.rb', line 57

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

#mark_completed!(need) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# 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) ⇒ Object



76
77
78
79
80
# 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

#needsObject



82
83
84
85
86
87
88
89
# 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?Boolean

Returns:

  • (Boolean)


102
103
104
# File 'lib/pps_commons/sentence.rb', line 102

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

#next_tickObject



91
92
93
94
95
96
97
98
99
100
# 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

#prepositionsObject



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

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

#request?Boolean

Returns:

  • (Boolean)


117
118
119
# File 'lib/pps_commons/sentence.rb', line 117

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

#secondary_subjectObject



128
129
130
# File 'lib/pps_commons/sentence.rb', line 128

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

#sentimentObject



132
133
134
135
136
# File 'lib/pps_commons/sentence.rb', line 132

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

#simple_subjectObject



121
122
123
124
125
126
# File 'lib/pps_commons/sentence.rb', line 121

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

#subject(msg = message) ⇒ Object



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

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



149
150
151
152
153
154
# File 'lib/pps_commons/sentence.rb', line 149

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

#ticked!(status) ⇒ Object



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

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



166
167
168
169
# File 'lib/pps_commons/sentence.rb', line 166

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

#with_clauseObject



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

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