File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -16,6 +16,35 @@ def initialize(channel_secret:)
1616 @channel_secret = channel_secret
1717 end
1818
19+ # Parse events from the raw request body and validate the signature.
20+ #
21+ # @param body [String]
22+ # The unmodified request body (exactly as received).
23+ # @param signature [String]
24+ # The value of the 'X-LINE-Signature' header.
25+ # @return [Array<Line::Bot::V2::Webhook::Event, OpenStruct>]
26+ # An array of event objects. Recognized events become instances of classes
27+ # under `Line::Bot::V2::Webhook::*Event`; otherwise, they're returned as `OpenStruct`.
28+ # @raise [InvalidSignatureError]
29+ # If the signature fails verification.
30+ #
31+ # @example Sinatra usage
32+ # def parser
33+ # @parser ||= Line::Bot::V2::WebhookParser.new(channel_secret: ENV.fetch("LINE_CHANNEL_SECRET"))
34+ # end
35+ #
36+ # post '/callback' do
37+ # body = request.body.read
38+ # signature = request.env['HTTP_X_LINE_SIGNATURE']
39+ #
40+ # begin
41+ # events = parser.parse(body, signature)
42+ # rescue Line::Bot::V2::WebhookParser::InvalidSignatureError
43+ # halt 400, { 'Content-Type' => 'text/plain' }, 'Bad Request'
44+ # end
45+ #
46+ # # Handle events...
47+ # end
1948 def parse ( body , signature )
2049 raise InvalidSignatureError . new ( "Invalid signature: #{ signature } " ) unless verify_signature ( body : body , signature : signature )
2150
Original file line number Diff line number Diff line change @@ -2,9 +2,16 @@ module Line
22 module Bot
33 module V2
44 class WebhookParser
5+ class InvalidSignatureError < ::StandardError
6+ end
7+
58 def initialize : (channel_secret: String) -> void
69
7- def parse : (String body, String signature) -> Hash[Symbol, untyped ]
10+ def parse : (
11+ body: String,
12+ signature: String
13+ ) -> Array[Webhook::Event | ::OpenStruct]
14+
815
916 private
1017
@@ -17,6 +24,8 @@ module Line
1724 def pascalize : (String|Symbol str) -> String
1825
1926 def singularize : (String|Symbol str) -> String
27+
28+ def deep_hash_to_ostruct : (untyped ) -> untyped
2029 end
2130 end
2231 end
You can’t perform that action at this time.
0 commit comments