Skip to content

Commit c3696c0

Browse files
author
Yoshiyuki Kinjo
committed
Accept open-uri as create_rich_menu_image argument
1 parent 7a86a44 commit c3696c0

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

lib/line/bot/client.rb

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -507,11 +507,17 @@ def get_rich_menu_image(rich_menu_id)
507507
def create_rich_menu_image(rich_menu_id, file)
508508
channel_token_required
509509

510-
content_type = case file.path
511-
when /\.jpe?g\z/i then 'image/jpeg'
512-
when /\.png\z/i then 'image/png'
510+
content_type = if file.respond_to?(:content_type)
511+
content_type = file.content_type
512+
raise ArgumentError, "invalid content type: #{content_type}" unless ['image/jpeg', 'image/png'].include?(content_type)
513+
content_type
513514
else
514-
raise ArgumentError, "invalid file extension: #{file.path}"
515+
case file.path
516+
when /\.jpe?g\z/i then 'image/jpeg'
517+
when /\.png\z/i then 'image/png'
518+
else
519+
raise ArgumentError, "invalid file extension: #{file.path}"
520+
end
515521
end
516522

517523
endpoint_path = "/bot/richmenu/#{rich_menu_id}/content"

spec/line/bot/rich_menu_spec.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
require 'webmock/rspec'
33
require 'json'
44
require 'tempfile'
5+
require 'open-uri'
56

67
RICH_MENU_CONTENT = <<"EOS"
78
{
@@ -177,6 +178,24 @@
177178
.with(body: File.open(RICH_MENU_IMAGE_FILE_PATH).read)
178179
end
179180

181+
it 'uploads and attaches am image to a rich menu from uri' do
182+
uri_template = Addressable::Template.new Line::Bot::API::DEFAULT_BLOB_ENDPOINT + '/bot/richmenu/1234567/content'
183+
184+
stub_request(:post, uri_template).to_return(body: '{}', status: 200).with do |request|
185+
expect(request.headers["Content-Type"]).to eq('image/png')
186+
end
187+
188+
image_url = 'https://line.example.org/rich_menu.png'
189+
image_content = File.open(RICH_MENU_IMAGE_FILE_PATH).read
190+
image_content.force_encoding('ASCII-8BIT')
191+
stub_request(:get, image_url).to_return(body: image_content, status: 200, headers: {'Content-type': 'image/png' })
192+
193+
client.create_rich_menu_image('1234567', open(image_url))
194+
195+
expect(WebMock).to have_requested(:post, Line::Bot::API::DEFAULT_BLOB_ENDPOINT + '/bot/richmenu/1234567/content')
196+
.with(body: image_content)
197+
end
198+
180199
it "uploads invalid extension's file" do
181200
uri_template = Addressable::Template.new Line::Bot::API::DEFAULT_ENDPOINT + '/bot/richmenu/1234567/content'
182201
stub_request(:post, uri_template).to_return(body: '{}', status: 200)
@@ -186,4 +205,17 @@
186205
end
187206
end.to raise_error(ArgumentError)
188207
end
208+
209+
it "uploads invalid content type uri" do
210+
uri_template = Addressable::Template.new Line::Bot::API::DEFAULT_ENDPOINT + '/bot/richmenu/1234567/content'
211+
stub_request(:post, uri_template).to_return(body: '{}', status: 200)
212+
213+
text_url = 'https://line.example.org/rich_menu.txt'
214+
text_content = File.open(RICH_MENU_INVALID_FILE_EXTENSION_PATH).read
215+
stub_request(:get, text_url).to_return(body: text_content, status: 200, headers: {'Content-type': 'plain/text' })
216+
217+
expect do
218+
client.create_rich_menu_image('1234567', open(text_url))
219+
end.to raise_error(ArgumentError)
220+
end
189221
end

0 commit comments

Comments
 (0)