Skip to content

Commit e505fdb

Browse files
committed
ruby: Add convenient construction of rawPayload messages
1 parent 55d615d commit e505fdb

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
@@ -22,4 +22,33 @@ def expunge_content(app_id, msg_id)
2222
return @api.v1_message_expunge_content(app_id, msg_id)
2323
end
2424
end
25+
26+
# Creates a [`MessageIn`] with the payload already being serialized.
27+
#
28+
# The payload is not normalized on the server (usually whitespace outside
29+
# of string literals, unnecessarily escaped characters in string and such
30+
# are fixed up by the server), and is not even required to be JSON.
31+
#
32+
# `attributes[:payload]` must be a string. An extra attribute `content_type`
33+
# is accepted that sets the `content-type` header of the payload, overwriting
34+
# the default of `application/json` if specified. Other than that, the
35+
# attributes are forwarded [`MessageIn.new`], so all the same ones are
36+
# accepted.
37+
def message_in_raw(attributes = {})
38+
if !attributes.key?(:transformations_params)
39+
attributes[:transformations_params] = {}
40+
end
41+
attributes[:transformations_params][:rawPayload] = attributes[:payload]
42+
attributes[:payload] = {}
43+
44+
content_type = attributes.delete(:content_type)
45+
if content_type != nil
46+
if !attributes[:transformations_params]key?(:headers)
47+
attributes[:transformations_params][:headers] = {}
48+
end
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)