Skip to content

Commit 12c5127

Browse files
authored
Merge pull request #246 from zenizh/get-follower-with-limit
Get the follower ids with a limit
2 parents d530254 + adca724 commit 12c5127

File tree

2 files changed

+71
-5
lines changed

2 files changed

+71
-5
lines changed

lib/line/bot/client.rb

+11-4
Original file line numberDiff line numberDiff line change
@@ -333,15 +333,22 @@ def get_room_member_profile(room_id, user_id)
333333

334334
# Get user IDs of who added your LINE Official Account as a friend
335335
#
336-
# @param continuation_token [String] Identifier to return next page
337-
# (next property to be included in the response)
336+
# @param start [String] Identifier to return next page (next property to be included in the response)
337+
# @param limit [Integer] The maximum number of user IDs to retrieve in a single request
338338
#
339339
# @return [Net::HTTPResponse]
340-
def get_follower_ids(continuation_token = nil)
340+
def get_follower_ids(deprecated_continuation_token = nil, start: nil, limit: nil)
341341
channel_token_required
342342

343+
if deprecated_continuation_token
344+
warn "continuation_token as the first argument is deprecated. Please use :start instead."
345+
start = deprecated_continuation_token
346+
end
347+
348+
params = { start: start, limit: limit }.compact
349+
343350
endpoint_path = "/bot/followers/ids"
344-
endpoint_path += "?start=#{continuation_token}" if continuation_token
351+
endpoint_path += "?" + URI.encode_www_form(params) unless params.empty?
345352
get(endpoint, endpoint_path, credentials)
346353
end
347354

spec/line/bot/client_get_follower_spec.rb

+60-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,15 @@
2323
}
2424
EOS
2525

26+
LIMITED_USER_ID_CONTENT = <<"EOS"
27+
{
28+
"userIds": [
29+
"Uxxxxxxxxxxxxxx1",
30+
"Uxxxxxxxxxxxxxx2"
31+
]
32+
}
33+
EOS
34+
2635
describe Line::Bot::Client do
2736
def dummy_config
2837
{
@@ -42,7 +51,57 @@ def generate_client
4251
uri_template = Addressable::Template.new Line::Bot::API::DEFAULT_ENDPOINT + '/bot/followers/ids'
4352
stub_request(:get, uri_template).to_return { |request| {body: USER_ID_CONTENT, status: 200} }
4453

45-
uri_template = Addressable::Template.new Line::Bot::API::DEFAULT_ENDPOINT + '/bot/followers/ids?start={continuationToken}'
54+
uri_template = Addressable::Template.new Line::Bot::API::DEFAULT_ENDPOINT + '/bot/followers/ids?start={start}'
55+
stub_request(:get, uri_template).to_return { |request| {body: NEXT_USER_ID_CONTENT, status: 200} }
56+
57+
client = generate_client
58+
59+
# first page
60+
response = client.get_follower_ids
61+
62+
expect(response).to be_a(Net::HTTPOK)
63+
result = JSON.parse(response.body)
64+
expect(result['userIds']).to eq ["Uxxxxxxxxxxxxxx1", "Uxxxxxxxxxxxxxx2", "Uxxxxxxxxxxxxxx3"]
65+
expect(result['next']).to eq "jxEWCEEP"
66+
67+
# second page
68+
response = client.get_follower_ids(start: result['next'])
69+
70+
expect(response).to be_a(Net::HTTPOK)
71+
result = JSON.parse(response.body)
72+
expect(result['userIds']).to eq ["Uxxxxxxxxxxxxxx4", "Uxxxxxxxxxxxxxx5", "Uxxxxxxxxxxxxxx6"]
73+
expect(result['next']).to be nil
74+
end
75+
76+
it 'gets limited number of follower ids' do
77+
# without any other conditions
78+
uri_template = Addressable::Template.new Line::Bot::API::DEFAULT_ENDPOINT + '/bot/followers/ids?limit={limit}'
79+
stub_request(:get, uri_template).to_return { |request| {body: LIMITED_USER_ID_CONTENT, status: 200} }
80+
81+
# with other conditions
82+
uri_template = Addressable::Template.new Line::Bot::API::DEFAULT_ENDPOINT + '/bot/followers/ids?limit={limit}&start={start}'
83+
stub_request(:get, uri_template).to_return { |request| {body: LIMITED_USER_ID_CONTENT, status: 200} }
84+
85+
client = generate_client
86+
87+
# without any other conditions
88+
response = client.get_follower_ids(limit: 2)
89+
result = JSON.parse(response.body)
90+
expect(response).to be_a(Net::HTTPOK)
91+
expect(result['userIds']).to eq ["Uxxxxxxxxxxxxxx1", "Uxxxxxxxxxxxxxx2"]
92+
93+
# with other conditions
94+
response = client.get_follower_ids(start: 'foo', limit: 2)
95+
result = JSON.parse(response.body)
96+
expect(response).to be_a(Net::HTTPOK)
97+
expect(result['userIds']).to eq ["Uxxxxxxxxxxxxxx1", "Uxxxxxxxxxxxxxx2"]
98+
end
99+
100+
it 'gets follower ids using deprecated_continuation_token argument' do
101+
uri_template = Addressable::Template.new Line::Bot::API::DEFAULT_ENDPOINT + '/bot/followers/ids'
102+
stub_request(:get, uri_template).to_return { |request| {body: USER_ID_CONTENT, status: 200} }
103+
104+
uri_template = Addressable::Template.new Line::Bot::API::DEFAULT_ENDPOINT + '/bot/followers/ids?start={start}'
46105
stub_request(:get, uri_template).to_return { |request| {body: NEXT_USER_ID_CONTENT, status: 200} }
47106

48107
client = generate_client

0 commit comments

Comments
 (0)