Skip to content

Commit 107f361

Browse files
authored
Merge pull request #91 from kotaro-dr/add_default_rich_menu_function
Add functions for default rich menu
2 parents 67ffb80 + 9e969c8 commit 107f361

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

lib/line/bot/client.rb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,24 @@ def get_user_rich_menu(user_id)
280280
get(endpoint_path)
281281
end
282282

283+
# Set default rich menu (Link a rich menu to all user)
284+
#
285+
# @param rich_menu_id [String] ID of an uploaded rich menu
286+
#
287+
# @return [Net::HTTPResponse]
288+
def set_default_rich_menu(rich_menu_id)
289+
endpoint_path = "/bot/user/all/richmenu/#{rich_menu_id}"
290+
post(endpoint_path)
291+
end
292+
293+
# Unset default rich menu (Unlink a rich menu from all user)
294+
#
295+
# @return [Net::HTTPResponse]
296+
def unset_default_rich_menu
297+
endpoint_path = "/bot/user/all/richmenu"
298+
delete(endpoint_path)
299+
end
300+
283301
# Link a rich menu to a user
284302
#
285303
# @param user_id [String] ID of the user
@@ -353,6 +371,25 @@ def get(endpoint_path)
353371
request.get
354372
end
355373

374+
# Post data, get content of specified URL.
375+
#
376+
# @param endpoint_path [String]
377+
#
378+
# @return [Net::HTTPResponse]
379+
def post(endpoint_path, payload = nil)
380+
raise Line::Bot::API::InvalidCredentialsError, 'Invalidates credentials' unless credentials?
381+
382+
request = Request.new do |config|
383+
config.httpclient = httpclient
384+
config.endpoint = endpoint
385+
config.endpoint_path = endpoint_path
386+
config.credentials = credentials
387+
config.payload = payload if payload
388+
end
389+
390+
request.post
391+
end
392+
356393
# Delete content of specified URL.
357394
#
358395
# @param endpoint_path [String]

spec/line/bot/rich_menu_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,22 @@
9595
expect(response.body).to eq '{"richMenuId":"7654321"}'
9696
end
9797

98+
it 'set a default rich menu' do
99+
uri_template = Addressable::Template.new Line::Bot::API::DEFAULT_ENDPOINT + '/bot/user/all/richmenu/7654321'
100+
stub_request(:post, uri_template).to_return(body: '{}', status: 200)
101+
102+
client.set_default_rich_menu('7654321')
103+
expect(WebMock).to have_requested(:post, Line::Bot::API::DEFAULT_ENDPOINT + '/bot/user/all/richmenu/7654321')
104+
end
105+
106+
it 'unset a default rich menu' do
107+
uri_template = Addressable::Template.new Line::Bot::API::DEFAULT_ENDPOINT + '/bot/user/all/richmenu'
108+
stub_request(:delete, uri_template).to_return(body: '{}', status: 200)
109+
110+
client.unset_default_rich_menu
111+
expect(WebMock).to have_requested(:delete, Line::Bot::API::DEFAULT_ENDPOINT + '/bot/user/all/richmenu')
112+
end
113+
98114
it 'links a rich menu to a user' do
99115
uri_template = Addressable::Template.new Line::Bot::API::DEFAULT_ENDPOINT + '/bot/user/1234567/richmenu/7654321'
100116
stub_request(:post, uri_template).to_return(body: '{}', status: 200)

0 commit comments

Comments
 (0)