class ChatWorkToSlack::Filters::Quote

Attributes

chatwork_users[R]
users[R]

Public Class Methods

call(text, options) click to toggle source
# File lib/chatwork_to_slack/filters/quote.rb, line 9
def self.call(text, options)
  new(options[:users]).call(text)
end
new(users) click to toggle source
# File lib/chatwork_to_slack/filters/quote.rb, line 5
def initialize(users)
  @users = users
end

Public Instance Methods

call(text) click to toggle source
# File lib/chatwork_to_slack/filters/quote.rb, line 13
def call(text)
  qts = text.scan(/(\[qt\].+\[\/qt\])/m)
  return text if qts.empty?

  quotes =
    if text.scan(/\[qt\]/m).size > 1
      text.split(/\[\/qt\]/).map{|t| "#{t}[/qt]".scan(/(\[qt\].+\[\/qt\])/m)[0]}.compact.map(&:first)
    else
      qts.first
    end

  result = text
  quotes.each do |quote|
    result.gsub!(quote,qtmeta(quote).split("\n").map{|t| ">#{t}"}.join("\n").gsub(/\[\/?qt\]/,''))
  end
  result
end

Private Instance Methods

qtmeta(text) click to toggle source
# File lib/chatwork_to_slack/filters/quote.rb, line 33
def qtmeta(text)
  q = text.scan(/\[qtmeta.+\]/).first
  qh = q.scan(/([\w]+)=([\d]+)/).to_h
  time = Time.at(qh['time'].to_i)
  user = users.find {|cw| cw[:chatwork_account_id] == qh['aid'].to_i }
  return text unless user
  text.gsub(q, "@#{user[:slack_name]}: #{time}\n")
end