Skip to content

Commit 592b3b9

Browse files
committed
Support insights api
1 parent 107b8a9 commit 592b3b9

File tree

2 files changed

+215
-0
lines changed

2 files changed

+215
-0
lines changed

lib/line/bot/client.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,40 @@ def get_quota_consumption
496496
get(endpoint_path, credentials)
497497
end
498498

499+
# Returns the number of messages sent on a specified day
500+
#
501+
# @param [String] date (Format:yyyyMMdd, Example:20191231)
502+
#
503+
# @return [Net::HTTPResponse]
504+
def get_number_of_message_deliveries(date)
505+
channel_token_required
506+
507+
endpoint_path = "/bot/insight/message/delivery?date=#{date}"
508+
get(endpoint_path, credentials)
509+
end
510+
511+
# Returns the number of followers
512+
#
513+
# @param [String] date (Format:yyyyMMdd, Example:20191231)
514+
#
515+
# @return [Net::HTTPResponse]
516+
def get_number_of_followers(date)
517+
channel_token_required
518+
519+
endpoint_path = "/bot/insight/followers?date=#{date}"
520+
get(endpoint_path, credentials)
521+
end
522+
523+
# Retrieves the demographic attributes for a bot's friends.
524+
#
525+
# @return [Net::HTTPResponse]
526+
def get_friend_demographics
527+
channel_token_required
528+
529+
endpoint_path = '/bot/insight/demographic'
530+
get(endpoint_path, credentials)
531+
end
532+
499533
# Fetch data, get content of specified URL.
500534
#
501535
# @param endpoint_path [String]

spec/line/bot/client_get_spec.rb

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,110 @@
3939
}
4040
EOS
4141

42+
NUMBER_OF_MESSAGE_DELIVERY_CONTENT = <<"EOS"
43+
{
44+
"status": "ready",
45+
"broadcast": 5385,
46+
"targeting": 522,
47+
"autoResponse": 123,
48+
"welcomeResponse": 456,
49+
"chat": 789,
50+
"apiBroadcast": 1123,
51+
"apiPush": 1234,
52+
"apiMulticast": 1567,
53+
"apiReply": 1890
54+
}
55+
EOS
56+
57+
NUMBER_OF_FOLLOWERS_CONTENT = <<"EOS"
58+
{
59+
"status": "ready",
60+
"followers": 5385,
61+
"targetedReaches": 522,
62+
"blocks": 123
63+
}
64+
EOS
65+
66+
FRIEND_DEMOGRAPHIC_CONTENT = <<"EOS"
67+
{
68+
"available": true,
69+
"genders": [
70+
{
71+
"gender": "unknown",
72+
"percentage": 37.6
73+
},
74+
{
75+
"gender": "male",
76+
"percentage": 31.8
77+
},
78+
{
79+
"gender": "female",
80+
"percentage": 30.6
81+
}
82+
],
83+
"ages": [
84+
{
85+
"age": "unknown",
86+
"percentage": 37.6
87+
},
88+
{
89+
"age": "from50",
90+
"percentage": 17.3
91+
}
92+
],
93+
"areas": [
94+
{
95+
"area": "unknown",
96+
"percentage": 42.9
97+
},
98+
{
99+
"area": "徳島",
100+
"percentage": 2.9
101+
}
102+
],
103+
"appTypes": [
104+
{
105+
"appType": "ios",
106+
"percentage": 62.4
107+
},
108+
{
109+
"appType": "android",
110+
"percentage": 27.7
111+
},
112+
{
113+
"appType": "others",
114+
"percentage": 9.9
115+
}
116+
],
117+
"subscriptionPeriods": [
118+
{
119+
"subscriptionPeriod": "over365days",
120+
"percentage": 96.4
121+
},
122+
{
123+
"subscriptionPeriod": "within365days",
124+
"percentage": 1.9
125+
},
126+
{
127+
"subscriptionPeriod": "within180days",
128+
"percentage": 1.2
129+
},
130+
{
131+
"subscriptionPeriod": "within90days",
132+
"percentage": 0.5
133+
},
134+
{
135+
"subscriptionPeriod": "within30days",
136+
"percentage": 0.1
137+
},
138+
{
139+
"subscriptionPeriod": "within7days",
140+
"percentage": 0
141+
}
142+
]
143+
}
144+
EOS
145+
42146
WebMock.allow_net_connect!
43147

44148
describe Line::Bot::Client do
@@ -173,4 +277,81 @@ def generate_client
173277
quota = JSON.parse(response.body)
174278
expect(quota['totalUsage']).to eq 1
175279
end
280+
281+
it 'get number of message deliveries' do
282+
uri_template = Addressable::Template.new Line::Bot::API::DEFAULT_ENDPOINT + '/bot/insight/message/delivery?date=20190701'
283+
stub_request(:get, uri_template).to_return(body: NUMBER_OF_MESSAGE_DELIVERY_CONTENT, status: 200)
284+
285+
client = generate_client
286+
response = client.get_number_of_message_deliveries("20190701")
287+
288+
json = JSON.parse(response.body, symbolize_names: true)
289+
expect(json).to eq({
290+
status: "ready",
291+
broadcast: 5385,
292+
targeting: 522,
293+
autoResponse: 123,
294+
welcomeResponse: 456,
295+
chat: 789,
296+
apiBroadcast: 1123,
297+
apiPush: 1234,
298+
apiMulticast: 1567,
299+
apiReply: 1890,
300+
})
301+
end
302+
303+
it 'get number of followers' do
304+
uri_template = Addressable::Template.new Line::Bot::API::DEFAULT_ENDPOINT + '/bot/insight/followers?date=20190701'
305+
stub_request(:get, uri_template).to_return(body: NUMBER_OF_FOLLOWERS_CONTENT, status: 200)
306+
307+
client = generate_client
308+
response = client.get_number_of_followers("20190701")
309+
310+
json = JSON.parse(response.body, symbolize_names: true)
311+
expect(json).to eq({
312+
status: "ready",
313+
followers: 5385,
314+
targetedReaches: 522,
315+
blocks: 123,
316+
})
317+
end
318+
319+
it 'get friend demographics' do
320+
uri_template = Addressable::Template.new Line::Bot::API::DEFAULT_ENDPOINT + '/bot/insight/demographic'
321+
stub_request(:get, uri_template).to_return(body: FRIEND_DEMOGRAPHIC_CONTENT, status: 200)
322+
323+
client = generate_client
324+
response = client.get_friend_demographics
325+
326+
json = JSON.parse(response.body, symbolize_names: true)
327+
expect(json).to eq({
328+
available: true,
329+
genders: [
330+
{ gender: "unknown", percentage: 37.6 },
331+
{ gender: "male", percentage: 31.8 },
332+
{ gender: "female", percentage: 30.6 }
333+
],
334+
ages: [
335+
{ age: "unknown", percentage: 37.6 },
336+
{ age: "from50", percentage: 17.3 },
337+
],
338+
areas: [
339+
{ area: "unknown", percentage: 42.9 },
340+
{ area: "徳島", percentage: 2.9 },
341+
],
342+
appTypes: [
343+
{ appType: "ios", percentage: 62.4 },
344+
{ appType: "android", percentage: 27.7 },
345+
{ appType: "others", percentage: 9.9 }
346+
],
347+
subscriptionPeriods: [
348+
{ subscriptionPeriod: "over365days", percentage: 96.4 },
349+
{ subscriptionPeriod: "within365days", percentage: 1.9 },
350+
{ subscriptionPeriod: "within180days", percentage: 1.2 },
351+
{ subscriptionPeriod: "within90days", percentage: 0.5 },
352+
{ subscriptionPeriod: "within30days", percentage: 0.1 },
353+
{ subscriptionPeriod: "within7days", percentage: 0 }
354+
]
355+
})
356+
end
176357
end

0 commit comments

Comments
 (0)