Class: Atig::Command::Autofix
- Defined in:
- lib/atig/command/autofix.rb
Instance Attribute Summary
Attributes inherited from Command
Instance Method Summary collapse
- #action(target, mesg, command, args) ⇒ Object
- #command_name ⇒ Object
- #distance(s1, s2) ⇒ Object
- #fix?(command, text, prev) ⇒ Boolean
-
#initialize(*args) ⇒ Autofix
constructor
A new instance of Autofix.
Methods inherited from Command
Constructor Details
#initialize(*args) ⇒ Autofix
Returns a new instance of Autofix.
13 |
# File 'lib/atig/command/autofix.rb', line 13 def initialize(*args); super end |
Instance Method Details
#action(target, mesg, command, args) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/atig/command/autofix.rb', line 27 def action(target, mesg, command, args) if args.empty? yield "/me #{command} blah blah" return end text = mesg.split(" ", 2)[1] q = gateway.(:status => text) prev,*_ = db.statuses.find_by_user( db.me, :limit => 1) unless fix?(command, q[:status], prev) then api.delay(0, :retry=>3) do|t| ret = t.post("statuses/update", q) gateway.update_status ret, target end else api.delay(0, :retry=>3) do|t| yield "Similar update in previous. Conclude that it has error." yield "And overwrite previous as new status: #{q[:status]}" ret = t.post("statuses/update", q) gateway.update_status ret, target t.post("statuses/destroy/#{prev.status.id}") db.statuses.transaction{|d| d.remove_by_id prev.id } end end end |
#command_name ⇒ Object
14 |
# File 'lib/atig/command/autofix.rb', line 14 def command_name; /(?:autofix|topic|overwwrite)!?/ end |
#distance(s1, s2) ⇒ Object
16 17 18 19 20 21 |
# File 'lib/atig/command/autofix.rb', line 16 def distance(s1, s2) c1 = s1.split(//) c2 = s2.split(//) distance = Atig::Levenshtein.levenshtein c1, c2 distance.to_f / [ c1.size, c2.size ].max end |
#fix?(command, text, prev) ⇒ Boolean
23 24 25 |
# File 'lib/atig/command/autofix.rb', line 23 def fix?(command, text, prev) command[-1,1] == '!' or distance(text, prev.status.text) < 0.5 end |