Skip to content

Commit

Permalink
Merge pull request #205 from yskkin/feature/liff_server_api
Browse files Browse the repository at this point in the history
Support LIFF v1 Server API
  • Loading branch information
kimoto authored Dec 17, 2020
2 parents 30a3bc8 + 3bce6d7 commit 16bedef
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/line/bot/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ module Bot
module API
DEFAULT_ENDPOINT = "https://api.line.me/v2"
DEFAULT_BLOB_ENDPOINT = "https://api-data.line.me/v2"
DEFAULT_LIFF_ENDPOINT = "https://api.line.me/liff/v1"

DEFAULT_HEADERS = {
'Content-Type' => 'application/json; charset=UTF-8',
Expand Down
45 changes: 45 additions & 0 deletions lib/line/bot/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ def blob_endpoint
end
end

def liff_endpoint
@liff_endpoint ||= API::DEFAULT_LIFF_ENDPOINT
end

# @return [Hash]
def credentials
{
Expand Down Expand Up @@ -641,6 +645,34 @@ def get_bot_info
get(endpoint, endpoint_path, credentials)
end

def get_liff_apps
channel_token_required

endpoint_path = '/apps'
get(liff_endpoint, endpoint_path, credentials)
end

def create_liff_app(app)
channel_token_required

endpoint_path = '/apps'
post(liff_endpoint, endpoint_path, app.to_json, credentials)
end

def update_liff_app(liff_id, app)
channel_token_required

endpoint_path = "/apps/#{liff_id}"
put(liff_endpoint, endpoint_path, app.to_json, credentials)
end

def delete_liff_app(liff_id)
channel_token_required

endpoint_path = "/apps/#{liff_id}"
delete(liff_endpoint, endpoint_path, credentials)
end

# Fetch data, get content of specified URL.
#
# @param endpoint_base [String]
Expand All @@ -666,6 +698,19 @@ def post(endpoint_base, endpoint_path, payload = nil, headers = {})
httpclient.post(endpoint_base + endpoint_path, payload, headers)
end

# Put data, get content of specified URL.
#
# @param endpoint_base [String]
# @param endpoint_path [String]
# @param payload [String or NilClass]
# @param headers [Hash]
#
# @return [Net::HTTPResponse]
def put(endpoint_base, endpoint_path, payload = nil, headers = {})
headers = API::DEFAULT_HEADERS.merge(headers)
httpclient.put(endpoint_base + endpoint_path, payload, headers)
end

# Delete content of specified URL.
#
# @param endpoint_base [String]
Expand Down
5 changes: 5 additions & 0 deletions lib/line/bot/httpclient.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ def post(url, payload, header = {})
http(uri).post(uri.request_uri, payload, header)
end

def put(url, payload, header = {})
uri = URI(url)
http(uri).put(uri.request_uri, payload, header)
end

def delete(url, header = {})
uri = URI(url)
http(uri).delete(uri.request_uri, header)
Expand Down
71 changes: 71 additions & 0 deletions spec/line/bot/liff_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
require 'spec_helper'
require 'webmock/rspec'
require 'json'

LIFF_APP_CONTENT = <<LIFF
{
"liffId": "1234567-abcdefgh",
"view": {
"type": "full",
"url": "https://example.com/myservice"
},
"description": "Happy New York",
"permanentLinkPattern": "concat"
}
LIFF
LIFF_APP_LIST_CONTENT = <<LIFF
{
"apps": [
#{LIFF_APP_CONTENT}
]
}
LIFF

LIFF_ENDPOINT = 'https://api.line.me'

describe Line::Bot::Client do
let(:client) do
dummy_config = {
channel_token: 'access token',
}
Line::Bot::Client.new do |config|
config.channel_token = dummy_config[:channel_token]
end
end

it 'gets a list of liff apps' do
uri_template = Addressable::Template.new LIFF_ENDPOINT + '/liff/v1/apps'
stub_request(:get, uri_template).to_return(body: LIFF_APP_LIST_CONTENT, status: 200)

response = client.get_liff_apps
expect(WebMock).to have_requested(:get, LIFF_ENDPOINT + '/liff/v1/apps')
expect(response.body).to eq LIFF_APP_LIST_CONTENT
end

it 'updates a liff app' do
uri_template = Addressable::Template.new LIFF_ENDPOINT + '/liff/v1/apps/1234567-abcdefgh'
stub_request(:put, uri_template).to_return(body: LIFF_APP_CONTENT, status: 200)

response = client.update_liff_app('1234567-abcdefgh', JSON.parse(LIFF_APP_CONTENT))
expect(WebMock).to have_requested(:put, LIFF_ENDPOINT + '/liff/v1/apps/1234567-abcdefgh')
.with(body: JSON.parse(LIFF_APP_CONTENT).to_json)
expect(response.body).to eq LIFF_APP_CONTENT
end

it 'creates a liff app' do
uri_template = Addressable::Template.new LIFF_ENDPOINT + '/liff/v1/apps'
stub_request(:post, uri_template).to_return(body: LIFF_APP_CONTENT, status: 200)

client.create_liff_app(JSON.parse(LIFF_APP_CONTENT))
expect(WebMock).to have_requested(:post, LIFF_ENDPOINT + '/liff/v1/apps')
.with(body: JSON.parse(LIFF_APP_CONTENT).to_json)
end

it 'deletes a liff app' do
uri_template = Addressable::Template.new LIFF_ENDPOINT + '/liff/v1/apps/1234567-abcdefgh'
stub_request(:delete, uri_template).to_return(body: '{}', status: 200)

client.delete_liff_app('1234567-abcdefgh')
expect(WebMock).to have_requested(:delete, LIFF_ENDPOINT + '/liff/v1/apps/1234567-abcdefgh')
end
end

0 comments on commit 16bedef

Please sign in to comment.