Skip to content

Commit

Permalink
Merge pull request #379 from pooza/2_1_18
Browse files Browse the repository at this point in the history
2.1.18
  • Loading branch information
pooza committed Mar 24, 2019
2 parents e4450f7 + 8cdc982 commit 3e119d9
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Expand Up @@ -134,7 +134,7 @@ GEM
parser (2.6.2.0)
ast (~> 2.4.0)
pg (1.1.4)
power_assert (1.1.3)
power_assert (1.1.4)
psych (3.1.0)
public_suffix (3.0.3)
raabro (1.1.6)
Expand Down
2 changes: 1 addition & 1 deletion config/application.yaml
@@ -1,5 +1,5 @@
package:
version: 2.1.17
version: 2.1.18
url: https://github.com/pooza/mulukhiya-toot-proxy
authors:
- Tatsuya Koishi
Expand Down
2 changes: 1 addition & 1 deletion lib/mulukhiya_toot_proxy/handler/tagging.rb
Expand Up @@ -3,7 +3,7 @@ class TaggingHandler < Handler
def exec(body, headers = {})
container = TagContainer.new
container.body = body['status']
TaggingDictionary.instance.each do |k, v|
TaggingDictionary.new.each do |k, v|
next if k.length < @config['/tagging/word/minimum_length']
next unless body['status'] =~ v[:pattern]
container.push(k)
Expand Down
9 changes: 6 additions & 3 deletions lib/mulukhiya_toot_proxy/tagging_dictionary.rb
@@ -1,12 +1,10 @@
module MulukhiyaTootProxy
class TaggingDictionary < Hash
include Singleton

def initialize
super
@logger = Logger.new
@http = HTTP.new
update(Marshal.load(File.read(TaggingDictionary.path))) if exist?
load
end

def concat(values)
Expand All @@ -21,8 +19,13 @@ def exist?
return File.exist?(TaggingDictionary.path)
end

def load
update(Marshal.load(File.read(TaggingDictionary.path))) if exist?
end

def refresh
File.write(TaggingDictionary.path, Marshal.dump(fetch))
load
rescue => e
@logger.error(Ginseng::Error.create(e).to_h)
end
Expand Down
Expand Up @@ -4,7 +4,7 @@ class TaggingDictionaryWorker
sidekiq_options retry: false

def perform
TaggingDictionary.instance.refresh
TaggingDictionary.new.refresh
end
end
end
2 changes: 1 addition & 1 deletion lib/task/tagging.rb
Expand Up @@ -3,7 +3,7 @@
namespace :dictionary do
desc 'update tagging dictionary'
task :update do
dic = MulukhiyaTootProxy::TaggingDictionary.instance
dic = MulukhiyaTootProxy::TaggingDictionary.new
dic.delete if dic.exist?
dic.create
puts "path: #{MulukhiyaTootProxy::TaggingDictionary.path}"
Expand Down
2 changes: 1 addition & 1 deletion test/tagging_dictionary.rb
@@ -1,7 +1,7 @@
module MulukhiyaTootProxy
class TaggingDictionaryTest < Test::Unit::TestCase
def setup
@dic = TaggingDictionary.instance
@dic = TaggingDictionary.new
end

def test_exist?
Expand Down
65 changes: 52 additions & 13 deletions test/tagging_handler.rb
Expand Up @@ -18,52 +18,91 @@ def setup
]
end

def pattern
@pattern ||= Regexp.new(@config['/mastodon/hashtag/pattern'], Regexp::IGNORECASE)
return @pattern
end

def test_exec_without_default_tags
@config['/tagging/default_tags'] = []
handler = Handler.create('tagging')

assert_equal(handler.exec({'status' => 'hoge'})['status'], 'hoge')
tags = handler.exec({'status' => 'hoge'})['status'].scan(pattern).map(&:first)
assert_equal(tags.count, 0)
assert_equal(handler.result, 'TaggingHandler,0')

assert_equal(handler.exec({'status' => '宮本佳那子'})['status'], "宮本佳那子\n#宮本佳那子 #キュアソード #剣崎真琴")
tags = handler.exec({'status' => '宮本佳那子'})['status'].scan(pattern).map(&:first)
assert_equal(tags.count, 3)
assert(tags.member?('宮本佳那子'))
assert(tags.member?('剣崎真琴'))
assert(tags.member?('キュアソード'))
assert_equal(handler.result, 'TaggingHandler,3')

assert_equal(handler.exec({'status' => 'キュアソードの中の人は宮本佳那子。'})['status'], "キュアソードの中の人は宮本佳那子。\n#キュアソード #剣崎真琴 #宮本佳那子")
tags = handler.exec({'status' => 'キュアソードの中の人は宮本佳那子。'})['status'].scan(pattern).map(&:first)
assert_equal(tags.count, 3)
assert_equal(handler.result, 'TaggingHandler,6')

assert_equal(handler.exec({'status' => 'キュアソードの中の人は宮本 佳那子。'})['status'], "キュアソードの中の人は宮本 佳那子。\n#キュアソード #剣崎真琴 #宮本佳那子")
tags = handler.exec({'status' => 'キュアソードの中の人は宮本 佳那子。'})['status'].scan(pattern).map(&:first)
assert_equal(tags.count, 3)
assert_equal(handler.result, 'TaggingHandler,9')

assert_equal(handler.exec({'status' => 'キュアソードの中の人は宮本 佳那子。'})['status'], "キュアソードの中の人は宮本 佳那子。\n#キュアソード #剣崎真琴 #宮本佳那子")
tags = handler.exec({'status' => 'キュアソードの中の人は宮本 佳那子。'})['status'].scan(pattern).map(&:first)
assert_equal(tags.count, 3)
assert_equal(handler.result, 'TaggingHandler,12')

assert_equal(handler.exec({'status' => '#キュアソード の中の人は宮本佳那子。'})['status'], "#キュアソード の中の人は宮本佳那子。\n#剣崎真琴 #宮本佳那子")
tags = handler.exec({'status' => '#キュアソード の中の人は宮本佳那子。'})['status'].scan(pattern).map(&:first)
assert_equal(tags.count, 3)
assert_equal(handler.result, 'TaggingHandler,14')

assert_equal(handler.exec({'status' => 'Yes!プリキュア5'})['status'], "Yes!プリキュア5\n#Yes_プリキュア5")
tags = handler.exec({'status' => 'Yes!プリキュア5'})['status'].scan(pattern).map(&:first)
assert_equal(tags.count, 1)
assert(tags.member?('Yes_プリキュア5'))
assert_equal(handler.result, 'TaggingHandler,15')

assert_equal(handler.exec({'status' => 'Yes!プリキュア5 GoGo!'})['status'], "Yes!プリキュア5 GoGo!\n#Yes_プリキュア5GoGo")
tags = handler.exec({'status' => 'Yes!プリキュア5 GoGo!'})['status'].scan(pattern).map(&:first)
assert_equal(tags.count, 1)
assert(tags.member?('Yes_プリキュア5GoGo'))
assert_equal(handler.result, 'TaggingHandler,16')

assert_equal(handler.exec({'status' => "つよく、やさしく、美しく。\n#キュアフローラ_キュアマーメイド"})['status'], "つよく、やさしく、美しく。\n#キュアフローラ_キュアマーメイド\n#キュアフローラ #春野はるか #嶋村侑 #キュアマーメイド #海藤みなみ #浅野真澄")
tags = handler.exec({'status' => "つよく、やさしく、美しく。\n#キュアフローラ_キュアマーメイド"})['status'].scan(pattern).map(&:first)
assert_equal(tags.count, 7)
assert(tags.member?('キュアフローラ_キュアマーメイド'))
assert(tags.member?('キュアフローラ'))
assert(tags.member?('春野はるか'))
assert(tags.member?('嶋村侑'))
assert(tags.member?('キュアマーメイド'))
assert(tags.member?('海藤みなみ'))
assert(tags.member?('浅野真澄'))
assert_equal(handler.result, 'TaggingHandler,22')

assert_equal(handler.exec({'status' => '#キュアビューティ'})['status'], "#キュアビューティ\n#青木れいか #西村ちなみ")
tags = handler.exec({'status' => '#キュアビューティ'})['status'].scan(pattern).map(&:first)
assert_equal(tags.count, 3)
assert(tags.member?('キュアビューティ'))
assert(tags.member?('青木れいか'))
assert(tags.member?('西村ちなみ'))
assert_equal(handler.result, 'TaggingHandler,24')
end

def test_exec_with_default_tag
@config['/tagging/default_tags'] = ['美食丼']
handler = Handler.create('tagging')

assert_equal(handler.exec({'status' => 'hoge'})['status'], "hoge\n#美食丼")
tags = handler.exec({'status' => 'hoge'})['status'].scan(pattern).map(&:first)
assert_equal(tags.count, 1)
assert(tags.member?('美食丼'))
assert_equal(handler.result, 'TaggingHandler,1')

assert_equal(handler.exec({'status' => '宮本佳那子'})['status'], "宮本佳那子\n#宮本佳那子 #キュアソード #剣崎真琴 #美食丼")
tags = handler.exec({'status' => '宮本佳那子'})['status'].scan(pattern).map(&:first)
assert_equal(tags.count, 4)
assert(tags.member?('美食丼'))
assert(tags.member?('宮本佳那子'))
assert(tags.member?('剣崎真琴'))
assert(tags.member?('キュアソード'))
assert_equal(handler.result, 'TaggingHandler,5')

assert_equal(handler.exec({'status' => '#美食丼'})['status'], '#美食丼')
tags = handler.exec({'status' => '#美食丼'})['status'].scan(pattern).map(&:first)
assert_equal(tags.count, 1)
assert(tags.member?('美食丼'))
assert_equal(handler.result, 'TaggingHandler,5')
end
end
Expand Down

0 comments on commit 3e119d9

Please sign in to comment.