Skip to content

Commit a0b6f0b

Browse files
committed
ruby: Add convenient construction of rawPayload messages
1 parent acc11bb commit a0b6f0b

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

ruby/lib/svix/message_api.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# frozen_string_literal: true
22

33
module Svix
4+
module_function
5+
46
class MessageAPI
57
def initialize(api_client)
68
@api = MessageApi.new(api_client)
@@ -22,4 +24,31 @@ def expunge_content(app_id, msg_id)
2224
return @api.v1_message_expunge_content(app_id, msg_id)
2325
end
2426
end
27+
28+
# Creates a [`MessageIn`] with the payload already being serialized.
29+
#
30+
# The payload is not normalized on the server. Normally, payloads are required
31+
# to be JSON, and Svix will minify the payload before sending the webhook
32+
# (for example, by removing extraneous whitespace or unnecessarily escaped
33+
# characters in strings). With this function, the payload will be sent
34+
# "as is", without any minification or other processing.
35+
#
36+
# `attributes[:payload]` must be a string. An extra attribute `content_type`
37+
# is accepted that sets the `content-type` header of the webhook, overwriting
38+
# the default of `application/json` if specified. Other than that, the
39+
# attributes are forwarded [`MessageIn.new`], so all the same ones are
40+
# accepted.
41+
def message_in_raw(attributes = {})
42+
attributes[:transformations_params] ||= {}
43+
attributes[:transformations_params][:rawPayload] = attributes[:payload]
44+
attributes[:payload] = {}
45+
46+
content_type = attributes.delete(:content_type)
47+
if content_type != nil
48+
attributes[:transformations_params][:headers] ||= {}
49+
attributes[:transformations_params][:headers][:'content-type'] = content_type
50+
end
51+
52+
return MessageIn.new(attributes)
53+
end
2554
end

0 commit comments

Comments
 (0)