Skip to content

Commit 9b6cf7c

Browse files
authored
ruby: Add convenient construction of rawPayload messages (#1541)
Part of svix/monorepo-private#9470.
2 parents acc11bb + 8feda79 commit 9b6cf7c

File tree

2 files changed

+79
-4
lines changed

2 files changed

+79
-4
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

ruby/spec/message_api_spec.rb

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,59 @@
1212
let(:api_instance_mock) { double("MessageApi") }
1313
let(:api_class_mock) { double("MessageApiClass") }
1414

15-
# Mock out the API calls
16-
before(:each) do
17-
stub_const("Svix::MessageApi", api_class_mock)
18-
expect(api_class_mock).to receive(:new).with(param_mock).and_return(api_instance_mock)
15+
describe "message_in_raw" do
16+
it "works without content-type" do
17+
payload = "{ \"x\": true }"
18+
msg_in = Svix.message_in_raw(event_type: "x", payload: payload)
19+
20+
expect(msg_in.event_type).to eq("x")
21+
expect(msg_in.payload).to eq({})
22+
expect(msg_in.transformations_params[:rawPayload]).to eq(payload)
23+
expect(msg_in.transformations_params[:headers]).to eq(nil)
24+
end
25+
26+
it "works with content-type" do
27+
payload = "Hello, World!"
28+
content_type = "text/plain"
29+
msg_in = Svix.message_in_raw(event_type: "x", payload: payload, content_type: content_type)
30+
31+
expect(msg_in.event_type).to eq("x")
32+
expect(msg_in.payload).to eq({})
33+
expect(msg_in.transformations_params[:rawPayload]).to eq(payload)
34+
expect(msg_in.transformations_params[:headers][:'content-type']).to eq(content_type)
35+
end
36+
37+
it "works with other transformations params" do
38+
payload = "Hello, World!"
39+
content_type = "text/plain"
40+
msg_in = Svix.message_in_raw(
41+
event_type: "x",
42+
payload: payload,
43+
content_type: content_type,
44+
transformations_params: {
45+
:foo => "bar",
46+
:headers => {
47+
:'x-custom' => "baz",
48+
},
49+
},
50+
)
51+
52+
expect(msg_in.event_type).to eq("x")
53+
expect(msg_in.payload).to eq({})
54+
expect(msg_in.transformations_params[:foo]).to eq("bar")
55+
expect(msg_in.transformations_params[:rawPayload]).to eq(payload)
56+
expect(msg_in.transformations_params[:headers][:'content-type']).to eq(content_type)
57+
expect(msg_in.transformations_params[:headers][:'x-custom']).to eq("baz")
58+
end
1959
end
2060

2161
describe "#get" do
62+
# Mock out the API calls
63+
before(:each) do
64+
stub_const("Svix::MessageApi", api_class_mock)
65+
expect(api_class_mock).to receive(:new).with(param_mock).and_return(api_instance_mock)
66+
end
67+
2268
it "passes it's parameters to the correct method" do
2369
# Assert that the correct method is called with the correct parameters
2470
expect(api_instance_mock).to receive(:v1_message_get).with(app_id, msg_id, options)

0 commit comments

Comments
 (0)