Class: WGU::Sentence
- Inherits:
-
Object
- Object
- WGU::Sentence
- Defined in:
- lib/pps_commons/sentence.rb
Instance Attribute Summary collapse
-
#message ⇒ Object
readonly
Returns the value of attribute message.
-
#recipient ⇒ Object
readonly
Returns the value of attribute recipient.
Instance Method Summary collapse
- #add_need(need) ⇒ Object
- #add_need!(need) ⇒ Object
- #category ⇒ Object
- #has_more_needs? ⇒ Boolean
-
#initialize(recipient, msg) ⇒ Sentence
constructor
A new instance of Sentence.
-
#location_information ⇒ Object
Hostname or ipaddress from message.
- #log_entry ⇒ Object
- #mark_completed!(need) ⇒ Object
- #mark_incompleted!(need) ⇒ Object
- #needs ⇒ Object
- #new_request? ⇒ Boolean
- #next_tick ⇒ Object
- #prepositions ⇒ Object
- #request? ⇒ Boolean
- #secondary_subject ⇒ Object
- #sentiment ⇒ Object
- #simple_subject ⇒ Object
- #subject(msg = message) ⇒ Object
- #success_patterns ⇒ Object
- #ticked!(status) ⇒ Object
- #verb ⇒ Object
- #with_clause ⇒ Object
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 = (msg.kind_of?(WGU::Sentence) ? msg.log_entry : msg.to_s) .strip! end |
Instance Attribute Details
#message ⇒ Object (readonly)
Returns the value of attribute message.
6 7 8 |
# File 'lib/pps_commons/sentence.rb', line 6 def end |
#recipient ⇒ Object (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}/ ? : 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? << " with needs #{need}" else tmp = if with_clause =~ /needs/ "needs #{need} and" else "needs #{need}" end new_with_clause = "#{tmp} #{with_clause}" .gsub!(/#{with_clause}/, new_with_clause).strip! end end |
#category ⇒ Object
34 35 36 |
# File 'lib/pps_commons/sentence.rb', line 34 def category request? ? :request : :response end |
#has_more_needs? ⇒ Boolean
38 39 40 |
# File 'lib/pps_commons/sentence.rb', line 38 def has_more_needs? !!(.index(' needs ')) end |
#location_information ⇒ Object
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 .scan(/\s([a-z0-9\-]+\.wgu\.edu)\s/).count > 0 .gsub(/.+\s([a-z0-9\-]+\.wgu\.edu)\s.+/, '\1') elsif .scan(/(\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b)/).count > 0 .gsub(/.+(\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b).+/, '\1') elsif .scan(/WGU_UT\\[a-z\-0-9]+\s/i).count > 0 .gsub(/.+WGU_UT\\([a-z\-0-9]+)\s.*/i, '\1') else .gsub(/.+for\s\w+\son\s([\w\-]+)\swith.*/, '\1') end end |
#log_entry ⇒ Object
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/, '') .gsub!(/#{Regexp.escape(suffix)}/, new_suffix).strip! words = .split(' ') prefix = words[0..words.index('for')].join(' ') .gsub!(/#{Regexp.escape(prefix)}/, '').strip! 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) .gsub!(/(with)?(and)?\s!?#{need}/, '') add_need!("!#{need}") end |
#needs ⇒ Object
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
100 101 102 |
# File 'lib/pps_commons/sentence.rb', line 100 def new_request? !!(with_clause.empty? && needs.empty?) end |
#next_tick ⇒ Object
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 |
#prepositions ⇒ Object
104 105 106 107 108 109 110 111 112 113 |
# File 'lib/pps_commons/sentence.rb', line 104 def prepositions = [ /\sunder\s/, /\sfor\s/, /\sover\s/, /\swithin\s/, /\saliased\s/ ] if block_given? Regexp.union(.map { |pat| yield pat }) else Regexp.union() end end |
#request? ⇒ Boolean
115 116 117 |
# File 'lib/pps_commons/sentence.rb', line 115 def request? !!( =~ /^please/) end |
#secondary_subject ⇒ Object
126 127 128 |
# File 'lib/pps_commons/sentence.rb', line 126 def secondary_subject subject.gsub(/.+for(.*)$/, '\1').strip end |
#sentiment ⇒ Object
130 131 132 133 134 |
# File 'lib/pps_commons/sentence.rb', line 130 def sentiment @sentiment || ( ( =~ /#{success_patterns}$/) ? :success : :fail ) end |
#simple_subject ⇒ Object
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 = ) 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 ⇒ Object
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 = .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 = words.join(' ') end |
#verb ⇒ Object
164 165 166 167 |
# File 'lib/pps_commons/sentence.rb', line 164 def verb words = .split(' ') request? ? words[1] : words.last end |
#with_clause ⇒ Object
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 =~ /with/ msg = .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 |