Skip to content

Commit 81e683a

Browse files
committed
Merge branch 'master' of github.com:line/line-bot-sdk-ruby
2 parents 67122a9 + a5beb1e commit 81e683a

File tree

7 files changed

+119
-7
lines changed

7 files changed

+119
-7
lines changed

examples/kitchensink/app.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ def reply_content(event, messages)
6666
when Line::Bot::Event::Beacon
6767
reply_text(event, "[BEACON]\n#{JSON.generate(event['beacon'])}")
6868

69+
when Line::Bot::Event::Things
70+
reply_text(event, "[THINGS]\n#{JSON.generate(event['things'])}")
71+
6972
else
7073
reply_text(event, "Unknown event type: #{event}")
7174
end

lib/line/bot.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# License for the specific language governing permissions and limitations
1313
# under the License.
1414

15+
require 'line/bot/util'
1516
require 'line/bot/client'
1617
require 'line/bot/event'
1718
require 'line/bot/api/errors'

lib/line/bot/client.rb

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -466,9 +466,9 @@ def parse_events_from(request_body)
466466

467467
json['events'].map { |item|
468468
begin
469-
klass = Line::Bot::Event.const_get(camelize(item['type']))
469+
klass = Line::Bot::Event.const_get(Line::Bot::Util.camelize(item['type']))
470470
klass.new(item)
471-
rescue NameError => e
471+
rescue NameError
472472
Line::Bot::Event::Base.new(item)
473473
end
474474
}
@@ -510,11 +510,6 @@ 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
518513
end
519514
end
520515
end

lib/line/bot/event.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,4 @@
2323
require 'line/bot/event/unfollow'
2424
require 'line/bot/event/member_joined'
2525
require 'line/bot/event/member_left'
26+
require 'line/bot/event/things'

lib/line/bot/event/things.rb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
module ThingsType
19+
Link = 'link'
20+
Unlink = 'unlink'
21+
Unsupport = 'unsupport'
22+
end
23+
24+
class Things < Base
25+
def type
26+
Line::Bot::Event::ThingsType.const_get(Line::Bot::Util.camelize(@src['things']['type']))
27+
rescue NameError
28+
Line::Bot::Event::ThingsType::Unsupport
29+
end
30+
31+
def device_id
32+
@src['things']['deviceId']
33+
end
34+
end
35+
end
36+
end
37+
end

lib/line/bot/util.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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 Util
18+
# @return [String]
19+
def self.camelize(string)
20+
string.split(/_|(?=[A-Z])/).map(&:capitalize).join
21+
end
22+
end
23+
end
24+
end

spec/line/bot/client_parse_spec.rb

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,45 @@
200200
}
201201
]
202202
}
203+
},
204+
{
205+
"type": "things",
206+
"replyToken": "nHuyWi...",
207+
"source": {
208+
"type": "user",
209+
"groupId": "U991eeaf62d..."
210+
},
211+
"timestamp": 12345678901234,
212+
"things": {
213+
"deviceId": "deviceid1",
214+
"type": "link"
215+
}
216+
},
217+
{
218+
"type": "things",
219+
"replyToken": "nHuyWi...",
220+
"source": {
221+
"type": "user",
222+
"groupId": "U991eeaf62d..."
223+
},
224+
"timestamp": 12345678901234,
225+
"things": {
226+
"deviceId": "deviceid2",
227+
"type": "unlink"
228+
}
229+
},
230+
{
231+
"type": "things",
232+
"replyToken": "nHuyWi...",
233+
"source": {
234+
"type": "user",
235+
"groupId": "U991eeaf62d..."
236+
},
237+
"timestamp": 12345678901234,
238+
"things": {
239+
"deviceId": "deviceid3",
240+
"type": "unsupport"
241+
}
203242
}
204243
]
205244
}
@@ -313,6 +352,18 @@ def generate_client
313352

314353
expect(events[13]).to be_a(Line::Bot::Event::MemberJoined)
315354
expect(events[14]).to be_a(Line::Bot::Event::MemberLeft)
355+
356+
expect(events[15]).to be_a(Line::Bot::Event::Things)
357+
expect(events[15].type).to eq(Line::Bot::Event::ThingsType::Link)
358+
expect(events[15].device_id).to eq('deviceid1')
359+
360+
expect(events[16]).to be_a(Line::Bot::Event::Things)
361+
expect(events[16].type).to eq(Line::Bot::Event::ThingsType::Unlink)
362+
expect(events[16].device_id).to eq('deviceid2')
363+
364+
expect(events[17]).to be_a(Line::Bot::Event::Things)
365+
expect(events[17].type).to eq(Line::Bot::Event::ThingsType::Unsupport)
366+
expect(events[17].device_id).to eq('deviceid3')
316367
end
317368

318369
it 'parses unknown event' do

0 commit comments

Comments
 (0)