Skip to content

Commit ba90a52

Browse files
authored
Merge pull request #177 from yskkin/open_uri
Accept open-uri as create_rich_menu_image's argument
2 parents 5cc3197 + d680579 commit ba90a52

File tree

3 files changed

+49
-9
lines changed

3 files changed

+49
-9
lines changed

lib/line/bot/client.rb

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -507,15 +507,8 @@ 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'
513-
else
514-
raise ArgumentError, "invalid file extension: #{file.path}"
515-
end
516-
517510
endpoint_path = "/bot/richmenu/#{rich_menu_id}/content"
518-
headers = credentials.merge('Content-Type' => content_type)
511+
headers = credentials.merge('Content-Type' => content_type(file))
519512
post(blob_endpoint, endpoint_path, file.rewind && file.read, headers)
520513
end
521514

@@ -691,6 +684,21 @@ def secure_compare(a, b)
691684
res == 0
692685
end
693686

687+
def content_type(file)
688+
if file.respond_to?(:content_type)
689+
content_type = file.content_type
690+
raise ArgumentError, "invalid content type: #{content_type}" unless ['image/jpeg', 'image/png'].include?(content_type)
691+
content_type
692+
else
693+
case file.path
694+
when /\.jpe?g\z/i then 'image/jpeg'
695+
when /\.png\z/i then 'image/png'
696+
else
697+
raise ArgumentError, "invalid file extension: #{file.path}"
698+
end
699+
end
700+
end
701+
694702
def channel_token_required
695703
raise ArgumentError, '`channel_token` is not configured' unless channel_token
696704
end

line-bot-api.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,5 @@ Gem::Specification.new do |spec|
2323
spec.add_development_dependency "bundler", "~> 1.11" if RUBY_VERSION < "2.3"
2424
spec.add_development_dependency 'rake', "~> 10.4"
2525
spec.add_development_dependency "rspec", "~> 3.0"
26-
spec.add_development_dependency "webmock", "~> 1.24"
26+
spec.add_development_dependency "webmock", "~> 3.8"
2727
end

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 an 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', URI.parse(image_url).open)
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', URI.parse(text_url).open)
219+
end.to raise_error(ArgumentError)
220+
end
189221
end

0 commit comments

Comments
 (0)