Skip to content
This repository has been archived by the owner on Nov 27, 2022. It is now read-only.

Commit

Permalink
Merge pull request #159 from banjun/id/156
Browse files Browse the repository at this point in the history
fix ASIPhoneNotifier.strip parse error at double-quote refs #156
  • Loading branch information
shimomura1004 committed Dec 22, 2013
2 parents 44e4629 + 45f109a commit 1106176
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
12 changes: 7 additions & 5 deletions plugins/as_iphone_notifier/lib/as_iphone_notifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
class AsakusaSatellite::Hook::ASIPhoneNotifier < AsakusaSatellite::Hook::Listener

def strip(str, n)
s = str.to_json.scan(/((\\u[0-9a-f]{4})|(.))/).map{|m| m[0]}.reduce(""){|x,y|
z = x + y
z.size <= n ? z : x
}
(JSON.parse "[#{s}]")[0]
escaped = str.to_json.match(/^"(.*)"$/)[1]
len = 0
s = escaped.scan(/((\\u[0-9a-f]{4})|(.))/).map(&:first).take_while{|escaped_char|
len += escaped_char.length
len <= n
}.join
(JSON.parse "[\"#{s}\"]")[0]
end

def after_create_message(context)
Expand Down
20 changes: 20 additions & 0 deletions plugins/as_iphone_notifier/spec/lib/as_iphone_notifier.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- encoding: utf-8 -*-
require File.dirname(__FILE__) + '/../../../../spec/spec_helper'
require 'as_iphone_notifier'

describe AsakusaSatellite::Hook::ASIPhoneNotifier do
before do
@hook = AsakusaSatellite::Hook::ASIPhoneNotifier.new({})
end

it 'JSONエンコード済み文字列長制限で元の文字列を切る' do
@hook.strip('test', 3).should == 'tes'
@hook.strip('test', 4).should == 'test'
@hook.strip('test', 5).should == 'test'

@hook.strip('日本語', 3*6-1).should == '日本'
@hook.strip('日本語', 3*6).should == '日本語'
@hook.strip('日本語', 3*6+1).should == '日本語'
end
end

0 comments on commit 1106176

Please sign in to comment.