Skip to content

Commit ec0d151

Browse files
Merge pull request #212 from line/set-webhook-endpoint
support set webhook endpoint
2 parents 8c54202 + 1898e9a commit ec0d151

File tree

3 files changed

+63
-21
lines changed

3 files changed

+63
-21
lines changed

lib/line/bot/client.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,19 @@ def get_webhook_endpoint
655655
get(endpoint, endpoint_path, credentials)
656656
end
657657

658+
# Sets the webhook endpoint URL.
659+
#
660+
# @param webhook_endpoint [String]
661+
#
662+
# @return [Net::HTTPResponse]
663+
def set_webhook_endpoint(webhook_endpoint)
664+
channel_token_required
665+
666+
endpoint_path = '/bot/channel/webhook/endpoint'
667+
body = {endpoint: webhook_endpoint}
668+
put(endpoint, endpoint_path, body.to_json, credentials)
669+
end
670+
658671
def get_liff_apps
659672
channel_token_required
660673

spec/line/bot/client_get_spec.rb

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -200,13 +200,6 @@
200200
}
201201
EOS
202202

203-
WEBHOOK_ENDPOINT_CONTENT = <<"EOS"
204-
{
205-
"endpoint": "https://example.com/test",
206-
"active": true
207-
}
208-
EOS
209-
210203
describe Line::Bot::Client do
211204
before do
212205
end
@@ -487,18 +480,4 @@ def generate_client
487480
markAsReadMode: 'manual'
488481
)
489482
end
490-
491-
it 'get webhook endpoint' do
492-
uri_template = Addressable::Template.new Line::Bot::API::DEFAULT_ENDPOINT + '/bot/channel/webhook/endpoint'
493-
stub_request(:get, uri_template).to_return(body: WEBHOOK_ENDPOINT_CONTENT, status: 200)
494-
495-
client = generate_client
496-
response = client.get_webhook_endpoint
497-
498-
json = JSON.parse(response.body, symbolize_names: true)
499-
expect(json).to eq(
500-
endpoint: 'https://example.com/test',
501-
active: true
502-
)
503-
end
504483
end

spec/line/bot/webhook_spec.rb

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
require 'spec_helper'
2+
require 'webmock/rspec'
3+
require 'json'
4+
5+
WEBHOOK_ENDPOINT_CONTENT = <<"EOS"
6+
{
7+
"endpoint": "https://example.com/test",
8+
"active": true
9+
}
10+
EOS
11+
12+
UPDATE_WEBHOOK_ENDPOINT = 'https://example.com/hoge'
13+
UPDATE_WEBHOOK_ENDPOINT_CONTENT = <<"EOS"
14+
{
15+
"endpoint": "#{UPDATE_WEBHOOK_ENDPOINT}"
16+
}
17+
EOS
18+
19+
describe Line::Bot::Client do
20+
let(:client) do
21+
dummy_config = {
22+
channel_token: 'access token',
23+
}
24+
Line::Bot::Client.new do |config|
25+
config.channel_token = dummy_config[:channel_token]
26+
end
27+
end
28+
29+
it 'get webhook endpoint' do
30+
uri_template = Addressable::Template.new Line::Bot::API::DEFAULT_ENDPOINT + '/bot/channel/webhook/endpoint'
31+
stub_request(:get, uri_template).to_return(body: WEBHOOK_ENDPOINT_CONTENT, status: 200)
32+
33+
response = client.get_webhook_endpoint
34+
35+
json = JSON.parse(response.body, symbolize_names: true)
36+
expect(json).to eq(
37+
endpoint: 'https://example.com/test',
38+
active: true
39+
)
40+
end
41+
42+
it 'set webhook endpoint' do
43+
uri_template = Addressable::Template.new Line::Bot::API::DEFAULT_ENDPOINT + '/bot/channel/webhook/endpoint'
44+
stub_request(:put, uri_template).to_return(body: '{}', status: 200)
45+
46+
client.set_webhook_endpoint(UPDATE_WEBHOOK_ENDPOINT)
47+
expect(WebMock).to have_requested(:put, Line::Bot::API::DEFAULT_ENDPOINT + '/bot/channel/webhook/endpoint')
48+
.with(body: JSON.parse(UPDATE_WEBHOOK_ENDPOINT_CONTENT).to_json)
49+
end
50+
end

0 commit comments

Comments
 (0)