Class: Atig::Command::Autofix

Inherits:
Command
  • Object
show all
Defined in:
lib/atig/command/autofix.rb

Instance Attribute Summary

Attributes inherited from Command

#api, #db, #gateway, #opts

Instance Method Summary collapse

Methods inherited from Command

#find_by_tid

Constructor Details

#initialize(*args) ⇒ Autofix

Returns a new instance of Autofix.



14
# File 'lib/atig/command/autofix.rb', line 14

def initialize(*args); super end

Instance Method Details

#action(target, mesg, command, args) ⇒ Object



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
56
# File 'lib/atig/command/autofix.rb', line 28

def action(target, mesg, command, args)
  if args.empty?
    yield "/me #{command} blah blah"
    return
  end
  text = mesg.split(" ", 2)[1]
  q = gateway.output_message(: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_nameObject



15
# File 'lib/atig/command/autofix.rb', line 15

def command_name; /(?:autofix|topic|overwwrite)!?/ end

#distance(s1, s2) ⇒ Object



17
18
19
20
21
22
# File 'lib/atig/command/autofix.rb', line 17

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

Returns:

  • (Boolean)


24
25
26
# File 'lib/atig/command/autofix.rb', line 24

def fix?(command, text, prev)
  command[-1,1] == '!' or distance(text, prev.status.text) < 0.5
end