Skip to content

Commit e09b366

Browse files
authored
Merge pull request #93 from a2ikm/feature/account_link
Support account link event
2 parents 313d540 + bb18fd4 commit e09b366

File tree

4 files changed

+53
-1
lines changed

4 files changed

+53
-1
lines changed

lib/line/bot/client.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ def parse_events_from(request_body)
466466

467467
json['events'].map { |item|
468468
begin
469-
klass = Line::Bot::Event.const_get(item['type'].capitalize)
469+
klass = Line::Bot::Event.const_get(camelize(item['type']))
470470
klass.new(item)
471471
rescue NameError => e
472472
Line::Bot::Event::Base.new(item)
@@ -510,6 +510,11 @@ def secure_compare(a, b)
510510
b.each_byte { |byte| res |= byte ^ l.shift }
511511
res == 0
512512
end
513+
514+
# @return [String]
515+
def camelize(string)
516+
string.split(/_|(?=[A-Z])/).map(&:capitalize).join
517+
end
513518
end
514519
end
515520
end

lib/line/bot/event.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# under the License.
1414

1515
require 'line/bot/event/base'
16+
require 'line/bot/event/account_link'
1617
require 'line/bot/event/beacon'
1718
require 'line/bot/event/follow'
1819
require 'line/bot/event/join'

lib/line/bot/event/account_link.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Copyright 2016 LINE
2+
#
3+
# LINE Corporation licenses this file to you under the Apache License,
4+
# version 2.0 (the "License"); you may not use this file except in compliance
5+
# with the License. You may obtain a copy of the License at:
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12+
# License for the specific language governing permissions and limitations
13+
# under the License.
14+
15+
module Line
16+
module Bot
17+
module Event
18+
class AccountLink < Base
19+
def result
20+
@src['link']['result']
21+
end
22+
23+
def nonce
24+
@src['link']['nonce']
25+
end
26+
end
27+
end
28+
end
29+
end

spec/line/bot/client_parse_spec.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,19 @@
145145
"type": "enter",
146146
"dm": "1234567890abcdef"
147147
}
148+
},
149+
{
150+
"type": "accountLink",
151+
"replyToken": "replyToken",
152+
"source": {
153+
"type": "user",
154+
"userId": "userid"
155+
},
156+
"timestamp": 12345678901234,
157+
"link": {
158+
"result": "ok",
159+
"nonce": "nonce"
160+
}
148161
}
149162
]
150163
}
@@ -251,6 +264,10 @@ def generate_client
251264
expect(events[11].type).to eq("enter")
252265
expect(events[11]['beacon']['dm']).to eq("1234567890abcdef")
253266
expect(events[11].deviceMessage).to eq("\x12\x34\x56\x78\x90\xab\xcd\xef".b)
267+
268+
expect(events[12]).to be_a(Line::Bot::Event::AccountLink)
269+
expect(events[12].result).to eq("ok")
270+
expect(events[12].nonce).to eq("nonce")
254271
end
255272

256273
it 'parses unknown event' do

0 commit comments

Comments
 (0)