Skip to content

Commit

Permalink
ruby: Add convenient construction of rawPayload messages
Browse files Browse the repository at this point in the history
  • Loading branch information
svix-jplatte committed Nov 29, 2024
1 parent 55d615d commit ad875e0
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions ruby/lib/svix/message_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,33 @@ def expunge_content(app_id, msg_id)
return @api.v1_message_expunge_content(app_id, msg_id)
end
end

# Creates a [`MessageIn`] with the payload already being serialized.
#
# The payload is not normalized on the server (usually whitespace outside
# of string literals, unnecessarily escaped characters in string and such
# are fixed up by the server), and is not even required to be JSON.
#
# `attributes[:payload]` must be a string. An extra attribute `content_type`
# is accepted that sets the `content-type` header of the payload, overwriting
# the default of `application/json` if specified. Other than that, the
# attributes are forwarded [`MessageIn.new`], so all the same ones are
# accepted.
def message_in_raw(attributes = {})
if !attributes.key?(:transformations_params)
attributes[:transformations_params] = {}
end
attributes[:transformations_params][:rawPayload] = attributes[:payload]
attributes[:payload] = {}

content_type = attributes.delete(:content_type)
if content_type != nil
if !attributes[:transformations_params].key?(:headers)
attributes[:transformations_params][:headers] = {}
end
attributes[:transformations_params][:headers][:'content-type'] = content_type
end

return MessageIn.new(attributes)
end
end

0 comments on commit ad875e0

Please sign in to comment.