Skip to content

Commit 9d73896

Browse files
authored
Align webhook parser with Steep suggestions (#481)
Passing the webhook parser to Steep revealed several suggestions for improvement. While some of them are strict, following them has no significant downside. This PR applies changes to eliminate those errors.
1 parent 1029e7c commit 9d73896

7 files changed

Lines changed: 50 additions & 50 deletions

File tree

Gemfile.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,15 @@ GEM
4343
json (2.10.2)
4444
language_server-protocol (3.17.0.4)
4545
lint_roller (1.1.0)
46-
logger (1.7.0)
4746
listen (3.9.0)
4847
rb-fsevent (~> 0.10, >= 0.10.3)
4948
rb-inotify (~> 0.9, >= 0.9.10)
49+
logger (1.7.0)
5050
minitest (5.25.5)
5151
multipart-post (2.4.1)
52+
mutex_m (0.3.0)
5253
net-http (0.6.0)
5354
uri
54-
mutex_m (0.3.0)
5555
parallel (1.26.3)
5656
parser (3.3.7.4)
5757
ast (~> 2.4.1)

README_v2.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ post '/callback' do
7272
signature = request.env['HTTP_X_LINE_SIGNATURE']
7373

7474
begin
75-
events = parser.parse(body, signature)
75+
events = parser.parse(body: body, signatuer: signature)
7676
rescue Line::Bot::V2::WebhookParser::InvalidSignatureError
7777
halt 400, { 'Content-Type' => 'text/plain' }, 'Bad Request'
7878
end
@@ -122,7 +122,7 @@ The `x-line-accepted-request-id` or `content-type` header can also be obtained i
122122
push_request = Line::Bot::V2::MessagingApi::PushMessageRequest.new(
123123
to: event.source.user_id,
124124
messages: [
125-
Line::Bot::V2::MessagingApi::TextMessage.new(type: 'text', text: "[^Request ID] #{headers['x-line-request-id']}")
125+
Line::Bot::V2::MessagingApi::TextMessage.new(text: "[^Request ID] #{headers['x-line-request-id']}")
126126
]
127127
)
128128
_body, _status_code, headers = client.push_message_with_http_info(push_message_request: push_request)

examples/v2/echobot/app.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def parser
1818
signature = request.env['HTTP_X_LINE_SIGNATURE']
1919

2020
begin
21-
events = parser.parse(body, signature)
21+
events = parser.parse(body: body, signature: signature)
2222
rescue Line::Bot::V2::WebhookParser::InvalidSignatureError
2323
halt 400, { 'Content-Type' => 'text/plain' }, 'Bad Request'
2424
end

examples/v2/kitchensink/app.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def parser
4444
signature = request.env['HTTP_X_LINE_SIGNATURE']
4545

4646
begin
47-
events = parser.parse(body, signature)
47+
events = parser.parse(body: body, signature: signature)
4848
rescue Line::Bot::V2::WebhookParser::InvalidSignatureError
4949
halt 400, { 'Content-Type' => 'text/plain' }, 'Bad Request'
5050
end

lib/line/bot/v2/webhook_parser.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
require 'base64'
12
require 'json'
23
require 'openssl'
34

4-
require 'line/bot/v2/reserved_words'
5-
require 'line/bot/v2/messaging_api/core'
5+
require 'line/bot/v2/utils'
66
require 'line/bot/v2/webhook/core'
77

88
module Line
@@ -39,7 +39,7 @@ def initialize(channel_secret:)
3939
# signature = request.env['HTTP_X_LINE_SIGNATURE']
4040
#
4141
# begin
42-
# events = parser.parse(body, signature)
42+
# events = parser.parse(body: body, signature: signature)
4343
# rescue Line::Bot::V2::WebhookParser::InvalidSignatureError
4444
# halt 400, { 'Content-Type' => 'text/plain' }, 'Bad Request'
4545
# end
@@ -53,7 +53,7 @@ def initialize(channel_secret:)
5353
# end
5454
# "OK"
5555
# end
56-
def parse(body, signature)
56+
def parse(body:, signature:)
5757
raise InvalidSignatureError.new("Invalid signature: #{signature}") unless verify_signature(body: body, signature: signature)
5858

5959
data = JSON.parse(body.chomp, symbolize_names: true)
@@ -62,7 +62,7 @@ def parse(body, signature)
6262
data = Line::Bot::V2::Utils.deep_symbolize(data)
6363

6464
data[:events].map do |event|
65-
Line::Bot::V2::Webhook::Event.create(**event)
65+
Line::Bot::V2::Webhook::Event.create(**event) # steep:ignore
6666
end
6767
end
6868

@@ -85,7 +85,7 @@ def secure_compare(a, b)
8585
l = a.unpack("C#{a.bytesize}")
8686

8787
res = 0
88-
b.each_byte { |byte| res |= byte ^ l.shift }
88+
b.each_byte { |byte| res |= byte ^ l.shift } # steep:ignore ArgumentTypeMismatch
8989
res == 0
9090
end
9191
end

sig/line/bot/v2/webhook_parser.rbs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ module Line
1414

1515
private
1616

17-
def variable_secure_compare: (a: String, b: String) -> bool
17+
def verify_signature: (body: String, signature: String) -> bool
1818

19-
def secure_compare: (a: String, b: String) -> bool
19+
def variable_secure_compare: (String, String) -> bool
2020

21-
def verify_signature: (body: String, signature: String) -> bool
21+
def secure_compare: (String, String) -> bool
2222
end
2323
end
2424
end

0 commit comments

Comments
 (0)