Skip to content

Commit ec7eb27

Browse files
author
Yoshiyuki Kinjo
committed
fix rubocop
1 parent c3696c0 commit ec7eb27

File tree

2 files changed

+20
-18
lines changed

2 files changed

+20
-18
lines changed

lib/line/bot/client.rb

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -507,21 +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 = 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
514-
else
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
521-
end
522-
523510
endpoint_path = "/bot/richmenu/#{rich_menu_id}/content"
524-
headers = credentials.merge('Content-Type' => content_type)
511+
headers = credentials.merge('Content-Type' => content_type(file))
525512
post(blob_endpoint, endpoint_path, file.rewind && file.read, headers)
526513
end
527514

@@ -697,6 +684,21 @@ def secure_compare(a, b)
697684
res == 0
698685
end
699686

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+
700702
def channel_token_required
701703
raise ArgumentError, '`channel_token` is not configured' unless channel_token
702704
end

spec/line/bot/rich_menu_spec.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,9 +188,9 @@
188188
image_url = 'https://line.example.org/rich_menu.png'
189189
image_content = File.open(RICH_MENU_IMAGE_FILE_PATH).read
190190
image_content.force_encoding('ASCII-8BIT')
191-
stub_request(:get, image_url).to_return(body: image_content, status: 200, headers: {'Content-type': 'image/png' })
191+
stub_request(:get, image_url).to_return(body: image_content, status: 200, headers: { 'Content-type' => 'image/png' })
192192

193-
client.create_rich_menu_image('1234567', open(image_url))
193+
client.create_rich_menu_image('1234567', URI.parse(image_url).open)
194194

195195
expect(WebMock).to have_requested(:post, Line::Bot::API::DEFAULT_BLOB_ENDPOINT + '/bot/richmenu/1234567/content')
196196
.with(body: image_content)
@@ -212,10 +212,10 @@
212212

213213
text_url = 'https://line.example.org/rich_menu.txt'
214214
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' })
215+
stub_request(:get, text_url).to_return(body: text_content, status: 200, headers: { 'Content-type' => 'plain/text' })
216216

217217
expect do
218-
client.create_rich_menu_image('1234567', open(text_url))
218+
client.create_rich_menu_image('1234567', URI.parse(text_url).open)
219219
end.to raise_error(ArgumentError)
220220
end
221221
end

0 commit comments

Comments
 (0)