Sha256: fb1899d4f7dcbbe1ad684a29c317df643a54162111616b4c0fe3714b9e06bdd3

Contents?: true

Size: 1.59 KB

Versions: 25

Compression:

Stored size: 1.59 KB

Contents

namespace :censor_bear do
  desc 'Format words'
  task format: :environment do
    from_file = File.read(CensorBear.config.format_from_file_path)
    File.open(CensorBear.config.format_dest_file_path, 'w') do |f|
      from_file.each_line do |word|
        line = "#{word.strip}={BANNED}|{BANNED}|{BANNED}|{BANNED}\r\n"
        f.write(line)
        f.flush
      end
    end
  end

  desc 'Import words'
  task import: :environment do
    is_overwrite = true
    file = File.read(CensorBear.config.import_file_path)
    # lines = doc.split(/\n/)
    file.each_line do |line|
      line.strip!
      key, rule_str = line.split('=')
      rules = rule_str.split('|')
      ugc = parse(rules[0]).blank? ? 'replace' : parse(rules[0])
      username = parse(rules[1]) || 'ignore'
      signature = parse(rules[2]) || 'ignore'
      dialog = parse(rules[3]) || 'ignore'
      nickname = parse(rules[1]) || 'ignore'
      replacement = ugc == 'replace' ? rules[0] : '**'

      payload = {
        key: key,
        ugc: ugc,
        username: username,
        signature: signature,
        dialog: dialog,
        nickname: nickname,
        replacement: replacement,
        user_id: CensorBear.config.user_class.constantize.last&.id,
        created_at: Time.now,
        updated_at: Time.now
      }

      word = CensorBear::StopWord.find_by(key: key)
      if word
        word.update_columns(payload) if is_overwrite
      else
        CensorBear::StopWord.create(payload)
      end
    end
  end

  def parse(rule)
    return nil if rule.blank?

    r = rule.match(/^{(\w+)}$/)
    r.blank? ? nil : r[1].downcase
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
censor_bear-0.1.29 lib/tasks/censor_bear_tasks.rake
censor_bear-0.1.28 lib/tasks/censor_bear_tasks.rake
censor_bear-0.1.27 lib/tasks/censor_bear_tasks.rake
censor_bear-0.1.26 lib/tasks/censor_bear_tasks.rake
censor_bear-0.1.25 lib/tasks/censor_bear_tasks.rake
censor_bear-0.1.24 lib/tasks/censor_bear_tasks.rake
censor_bear-0.1.23 lib/tasks/censor_bear_tasks.rake
censor_bear-0.1.22 lib/tasks/censor_bear_tasks.rake
censor_bear-0.1.21 lib/tasks/censor_bear_tasks.rake
censor_bear-0.1.20 lib/tasks/censor_bear_tasks.rake
censor_bear-0.1.19 lib/tasks/censor_bear_tasks.rake
censor_bear-0.1.18 lib/tasks/censor_bear_tasks.rake
censor_bear-0.1.17 lib/tasks/censor_bear_tasks.rake
censor_bear-0.1.15 lib/tasks/censor_bear_tasks.rake
censor_bear-0.1.14 lib/tasks/censor_bear_tasks.rake
censor_bear-0.1.13 lib/tasks/censor_bear_tasks.rake
censor_bear-0.1.12 lib/tasks/censor_bear_tasks.rake
censor_bear-0.1.11 lib/tasks/censor_bear_tasks.rake
censor_bear-0.1.10 lib/tasks/censor_bear_tasks.rake
censor_bear-0.1.9 lib/tasks/censor_bear_tasks.rake