Skip to content

Commit 16bedef

Browse files
authored
Merge pull request #205 from yskkin/feature/liff_server_api
Support LIFF v1 Server API
2 parents 30a3bc8 + 3bce6d7 commit 16bedef

File tree

4 files changed

+122
-0
lines changed

4 files changed

+122
-0
lines changed

lib/line/bot/api.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ module Bot
1919
module API
2020
DEFAULT_ENDPOINT = "https://api.line.me/v2"
2121
DEFAULT_BLOB_ENDPOINT = "https://api-data.line.me/v2"
22+
DEFAULT_LIFF_ENDPOINT = "https://api.line.me/liff/v1"
2223

2324
DEFAULT_HEADERS = {
2425
'Content-Type' => 'application/json; charset=UTF-8',

lib/line/bot/client.rb

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ def blob_endpoint
6666
end
6767
end
6868

69+
def liff_endpoint
70+
@liff_endpoint ||= API::DEFAULT_LIFF_ENDPOINT
71+
end
72+
6973
# @return [Hash]
7074
def credentials
7175
{
@@ -641,6 +645,34 @@ def get_bot_info
641645
get(endpoint, endpoint_path, credentials)
642646
end
643647

648+
def get_liff_apps
649+
channel_token_required
650+
651+
endpoint_path = '/apps'
652+
get(liff_endpoint, endpoint_path, credentials)
653+
end
654+
655+
def create_liff_app(app)
656+
channel_token_required
657+
658+
endpoint_path = '/apps'
659+
post(liff_endpoint, endpoint_path, app.to_json, credentials)
660+
end
661+
662+
def update_liff_app(liff_id, app)
663+
channel_token_required
664+
665+
endpoint_path = "/apps/#{liff_id}"
666+
put(liff_endpoint, endpoint_path, app.to_json, credentials)
667+
end
668+
669+
def delete_liff_app(liff_id)
670+
channel_token_required
671+
672+
endpoint_path = "/apps/#{liff_id}"
673+
delete(liff_endpoint, endpoint_path, credentials)
674+
end
675+
644676
# Fetch data, get content of specified URL.
645677
#
646678
# @param endpoint_base [String]
@@ -666,6 +698,19 @@ def post(endpoint_base, endpoint_path, payload = nil, headers = {})
666698
httpclient.post(endpoint_base + endpoint_path, payload, headers)
667699
end
668700

701+
# Put data, get content of specified URL.
702+
#
703+
# @param endpoint_base [String]
704+
# @param endpoint_path [String]
705+
# @param payload [String or NilClass]
706+
# @param headers [Hash]
707+
#
708+
# @return [Net::HTTPResponse]
709+
def put(endpoint_base, endpoint_path, payload = nil, headers = {})
710+
headers = API::DEFAULT_HEADERS.merge(headers)
711+
httpclient.put(endpoint_base + endpoint_path, payload, headers)
712+
end
713+
669714
# Delete content of specified URL.
670715
#
671716
# @param endpoint_base [String]

lib/line/bot/httpclient.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ def post(url, payload, header = {})
5757
http(uri).post(uri.request_uri, payload, header)
5858
end
5959

60+
def put(url, payload, header = {})
61+
uri = URI(url)
62+
http(uri).put(uri.request_uri, payload, header)
63+
end
64+
6065
def delete(url, header = {})
6166
uri = URI(url)
6267
http(uri).delete(uri.request_uri, header)

spec/line/bot/liff_spec.rb

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
require 'spec_helper'
2+
require 'webmock/rspec'
3+
require 'json'
4+
5+
LIFF_APP_CONTENT = <<LIFF
6+
{
7+
"liffId": "1234567-abcdefgh",
8+
"view": {
9+
"type": "full",
10+
"url": "https://example.com/myservice"
11+
},
12+
"description": "Happy New York",
13+
"permanentLinkPattern": "concat"
14+
}
15+
LIFF
16+
LIFF_APP_LIST_CONTENT = <<LIFF
17+
{
18+
"apps": [
19+
#{LIFF_APP_CONTENT}
20+
]
21+
}
22+
LIFF
23+
24+
LIFF_ENDPOINT = 'https://api.line.me'
25+
26+
describe Line::Bot::Client do
27+
let(:client) do
28+
dummy_config = {
29+
channel_token: 'access token',
30+
}
31+
Line::Bot::Client.new do |config|
32+
config.channel_token = dummy_config[:channel_token]
33+
end
34+
end
35+
36+
it 'gets a list of liff apps' do
37+
uri_template = Addressable::Template.new LIFF_ENDPOINT + '/liff/v1/apps'
38+
stub_request(:get, uri_template).to_return(body: LIFF_APP_LIST_CONTENT, status: 200)
39+
40+
response = client.get_liff_apps
41+
expect(WebMock).to have_requested(:get, LIFF_ENDPOINT + '/liff/v1/apps')
42+
expect(response.body).to eq LIFF_APP_LIST_CONTENT
43+
end
44+
45+
it 'updates a liff app' do
46+
uri_template = Addressable::Template.new LIFF_ENDPOINT + '/liff/v1/apps/1234567-abcdefgh'
47+
stub_request(:put, uri_template).to_return(body: LIFF_APP_CONTENT, status: 200)
48+
49+
response = client.update_liff_app('1234567-abcdefgh', JSON.parse(LIFF_APP_CONTENT))
50+
expect(WebMock).to have_requested(:put, LIFF_ENDPOINT + '/liff/v1/apps/1234567-abcdefgh')
51+
.with(body: JSON.parse(LIFF_APP_CONTENT).to_json)
52+
expect(response.body).to eq LIFF_APP_CONTENT
53+
end
54+
55+
it 'creates a liff app' do
56+
uri_template = Addressable::Template.new LIFF_ENDPOINT + '/liff/v1/apps'
57+
stub_request(:post, uri_template).to_return(body: LIFF_APP_CONTENT, status: 200)
58+
59+
client.create_liff_app(JSON.parse(LIFF_APP_CONTENT))
60+
expect(WebMock).to have_requested(:post, LIFF_ENDPOINT + '/liff/v1/apps')
61+
.with(body: JSON.parse(LIFF_APP_CONTENT).to_json)
62+
end
63+
64+
it 'deletes a liff app' do
65+
uri_template = Addressable::Template.new LIFF_ENDPOINT + '/liff/v1/apps/1234567-abcdefgh'
66+
stub_request(:delete, uri_template).to_return(body: '{}', status: 200)
67+
68+
client.delete_liff_app('1234567-abcdefgh')
69+
expect(WebMock).to have_requested(:delete, LIFF_ENDPOINT + '/liff/v1/apps/1234567-abcdefgh')
70+
end
71+
end

0 commit comments

Comments
 (0)