Skip to content

Commit 86f88e7

Browse files
committed
ruby: Add jinja templates
1 parent c02b864 commit 86f88e7

8 files changed

+323
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
def get_or_create(application_in, options = {})
2+
options = options.transform_keys(&:to_s)
3+
path = "/api/v1/app"
4+
res = @client.execute_request(
5+
"POST",
6+
path,
7+
query_params:{
8+
"get_if_exists"=> true
9+
},
10+
headers: {
11+
"idempotency-key" => options["idempotency-key"]
12+
},
13+
body: application_in
14+
)
15+
ApplicationOut.deserialize(res)
16+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Creates a [`MessageIn`] with a raw string payload.
2+
#
3+
# The payload is not normalized on the server. Normally, payloads are required
4+
# to be JSON, and Svix will minify the payload before sending the webhook
5+
# (for example, by removing extraneous whitespace or unnecessarily escaped
6+
# characters in strings). With this function, the payload will be sent
7+
# "as is", without any minification or other processing.
8+
#
9+
# `attributes[:payload]` must be a string. An extra attribute `content_type`
10+
# is accepted that sets the `content-type` header of the webhook sent by Svix,
11+
# overwriting the default of `application/json` if specified. Other than that,
12+
# the attributes are forwarded [`MessageIn.new`], so all the same ones are
13+
# accepted.
14+
def message_in_raw(attributes = {})
15+
attributes[:transformations_params] ||= {}
16+
attributes[:transformations_params][:rawPayload] = attributes[:payload]
17+
attributes[:payload] = {}
18+
19+
content_type = attributes.delete(:content_type)
20+
if content_type != nil
21+
attributes[:transformations_params][:headers] ||= {}
22+
attributes[:transformations_params][:headers][:'content-type'] = content_type
23+
end
24+
25+
MessageIn.new(attributes)
26+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
{% set resource_type_name = resource.name | to_upper_camel_case -%}
2+
# frozen_string_literal: true
3+
# This file is @generated
4+
5+
require 'net/http'
6+
7+
8+
module Svix
9+
{% if resource.name == "message" -%}
10+
module_function
11+
{% include "api_extra/message.rb" %}
12+
{% endif -%}
13+
class {{ resource_type_name }}
14+
def initialize(client)
15+
@client = client
16+
end
17+
{% for op in resource.operations %}
18+
{% set func_args -%}
19+
{# path params -#}
20+
{%- for p in op.path_params -%}
21+
{{ p | to_snake_case }},
22+
{%- endfor -%}
23+
{# request body -#}
24+
{%- if op.request_body_schema_name is defined -%}
25+
{{ op.request_body_schema_name | to_snake_case }},
26+
{%- endif -%}
27+
{# header/query params -#}
28+
{%- if op | has_query_or_header_params -%}
29+
options = {}
30+
{%- endif -%}
31+
{% endset -%}
32+
33+
def {{ op.name | to_snake_case }}({{ func_args | strip_trailing_comma }})
34+
{% if op | has_query_or_header_params -%}
35+
options = options.transform_keys(&:to_s)
36+
{% endif -%}
37+
path = "{{ op.path | generate_ruby_path_str(op.path_params) }}"
38+
{% if op.response_body_schema_name is defined -%}res = {% endif -%}
39+
@client.execute_request(
40+
"{{ op.method | upper }}",
41+
path,
42+
{% if op.query_params | length >0 -%}
43+
query_params:{
44+
{% for p in op.query_params -%}
45+
"{{ p.name }}" => options["{{ p.name }}"],
46+
{% endfor -%}
47+
},
48+
{% endif -%}
49+
{% if op.header_params | length >0 -%}
50+
headers:{
51+
{% for p in op.header_params -%}
52+
"{{ p.name }}" => options["{{ p.name }}"],
53+
{% endfor -%}
54+
},
55+
{% endif -%}
56+
{% if op.request_body_schema_name is defined -%}
57+
body: {{ op.request_body_schema_name | to_snake_case }},
58+
{% endif -%}
59+
)
60+
{% if op.response_body_schema_name is defined -%}
61+
{{ op.response_body_schema_name | to_upper_camel_case }}.deserialize res
62+
{% endif %}
63+
end
64+
65+
{% set extra_path -%}
66+
api_extra/{{ resource.name | to_snake_case }}_{{ op.name | to_snake_case }}.rb
67+
{%- endset -%}
68+
{% include extra_path ignore missing %}
69+
70+
{% endfor -%}
71+
end
72+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{% if type.kind == "struct" -%}
2+
{% include "types/struct.rb.jinja" -%}
3+
{%- elif type.kind == "string_enum"-%}
4+
{% include "types/string_enum.rb.jinja" -%}
5+
{%- elif type.kind == "integer_enum" %}
6+
{%- include "types/integer_enum.rb.jinja" -%}
7+
{%- endif %}
+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# frozen_string_literal: true
2+
# This file is @generated
3+
4+
require "json"
5+
require "openssl"
6+
require "base64"
7+
require "uri"
8+
require "logger"
9+
10+
{% macro require_api(resource) -%}
11+
{% if resource.operations | length > 0 -%}
12+
require "svix/api/{{ resource.name | to_snake_case }}"
13+
{% endif -%}
14+
{% endmacro -%}
15+
16+
17+
# API
18+
{% for resource in api.resources -%}
19+
{{ require_api(resource) -}}
20+
{% for _resource_name, resource in resource.subresources | items -%}
21+
{{ require_api(resource) -}}
22+
{% endfor -%}
23+
{% endfor -%}
24+
25+
# Models
26+
{% for type in types -%}
27+
require "svix/models/{{ type | to_snake_case }}"
28+
{% endfor -%}
29+
30+
# Core
31+
require "svix/api_error"
32+
require "svix/errors"
33+
require "svix/svix"
34+
require "svix/util"
35+
require "svix/version"
36+
require "svix/webhook"
37+
require "svix/http_error_out"
38+
require "svix/http_validation_error"
39+
require "svix/validation_error"
40+
require "svix/svix_http_client.rb"
41+
require "svix/internal"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# frozen_string_literal: true
2+
# This file is @generated
3+
{% set class_ty = type.name | to_upper_camel_case -%}
4+
5+
module Svix
6+
{% if type.description is defined -%}
7+
{{ type.description | to_doc_comment(style="ruby") | indent(4) }}
8+
{% endif -%}
9+
class {{ class_ty }}
10+
{% for name, value in type.variants -%}
11+
{{ name | to_upper_snake_case }} = {{ value }}.freeze
12+
{% endfor %}
13+
def self.all_vars
14+
@all_vars ||= [
15+
{%- for name, value in type.variants -%}
16+
{{ name | to_upper_snake_case }},
17+
{%- endfor -%}
18+
].freeze
19+
end
20+
21+
def initialize(value)
22+
unless {{ class_ty }}.all_vars.include?(value)
23+
raise "Invalid ENUM value '#{value}' for class #{{ class_ty }}"
24+
end
25+
@value = value
26+
end
27+
28+
def self.deserialize(value)
29+
return value if {{ class_ty }}.all_vars.include?(value)
30+
raise "Invalid ENUM value '#{value}' for class #{{ class_ty }}"
31+
end
32+
33+
def serialize
34+
@value
35+
end
36+
end
37+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# frozen_string_literal: true
2+
# This file is @generated
3+
{% set class_ty = type.name | to_upper_camel_case -%}
4+
5+
module Svix
6+
{% if type.description is defined -%}
7+
{{ type.description | to_doc_comment(style="ruby") | indent(4) }}
8+
{% endif -%}
9+
class {{ class_ty }}
10+
{% for value in type.values -%}
11+
{{ value | to_upper_snake_case }} = "{{ value }}".freeze
12+
{% endfor %}
13+
def self.all_vars
14+
@all_vars ||= [
15+
{%- for value in type.values -%}
16+
{{ value | to_upper_snake_case }},
17+
{%- endfor -%}
18+
].freeze
19+
end
20+
21+
def initialize(value)
22+
unless {{ class_ty }}.all_vars.include?(value)
23+
raise "Invalid ENUM value '#{value}' for class #{{ class_ty }}"
24+
end
25+
@value = value
26+
end
27+
28+
def self.deserialize(value)
29+
return value if {{ class_ty }}.all_vars.include?(value)
30+
raise "Invalid ENUM value '#{value}' for class #{{ class_ty }}"
31+
end
32+
33+
def serialize
34+
@value
35+
end
36+
end
37+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# frozen_string_literal: true
2+
# This file is @generated
3+
{% set class_ty = type.name | to_upper_camel_case -%}
4+
require 'json'
5+
6+
module Svix
7+
{% if type.description is defined -%}
8+
{{ type.description | to_doc_comment(style="ruby") | indent(4) }}
9+
{% endif -%}
10+
class {{ class_ty }}
11+
{% for field in type.fields -%}
12+
attr_accessor :{{ field.name | to_snake_case }}
13+
{% endfor %}
14+
ALL_FIELD ||= [
15+
{%- for f in type.fields -%}
16+
"{{ f.name | to_snake_case }}"{% if not loop.last %},{% endif %}
17+
{%- endfor -%}
18+
].freeze
19+
private_constant :ALL_FIELD
20+
21+
def initialize(attributes = {})
22+
unless attributes.is_a?(Hash)
23+
fail ArgumentError, "The input argument (attributes) must be a hash in `Svix::{{ class_ty }}` new method"
24+
end
25+
attributes.each do |k, v|
26+
unless ALL_FIELD.include? k.to_s
27+
fail ArgumentError, "The field #{k} is not part of Svix::{{ class_ty }}"
28+
end
29+
instance_variable_set("@#{k}", v)
30+
instance_variable_set("@__#{k}_is_defined", true)
31+
end
32+
end
33+
34+
def self.deserialize(attributes = {})
35+
attributes = attributes.transform_keys(&:to_s)
36+
attrs = Hash.new
37+
{% for field in type.fields -%}
38+
{% set val_expr %}attributes['{{ field.name }}']{% endset -%}
39+
{% set not_nil_check = "" -%}
40+
{% if not field.required -%}
41+
{% set not_nil_check %}if {{ val_expr }}{% endset -%}
42+
{% endif -%}
43+
44+
{% if (field.type.is_set() or field.type.is_list()) and field.type.inner_type().is_schema_ref() -%}
45+
{% set val_expr -%}
46+
attributes['{{ field.name }}'].map { | v | Svix::{{ field.type.inner_type().to_ruby() }}.deserialize v } {{ not_nil_check }}
47+
{%- endset -%}
48+
{% elif field.type.is_schema_ref() -%}
49+
{% set val_expr -%}
50+
Svix::{{ field.type.to_ruby() }}.deserialize(attributes['{{ field.name }}']) {{ not_nil_check }}
51+
{%- endset -%}
52+
{% elif field.type.is_datetime() -%}
53+
{% set val_expr %}DateTime.rfc3339({{ val_expr }}).to_time {{ not_nil_check }}{% endset -%}
54+
{% endif -%}
55+
attrs['{{ field.name | to_snake_case }}'] = {{ val_expr }}
56+
{% endfor -%}
57+
new attrs
58+
end
59+
60+
def serialize
61+
out = Hash.new
62+
{% for field in type.fields -%}
63+
{% set val_expr %}@{{ field.name | to_snake_case }}{% endset -%}
64+
65+
{% if (field.type.is_set() or field.type.is_list()) and field.type.inner_type().is_schema_ref() -%}
66+
{% set val_expr %}{{ val_expr }}.map { | v | v.serialize }{% endset -%}
67+
{% elif field.type.is_schema_ref() -%}
68+
{% set val_expr %}{{ val_expr }}.serialize{% endset -%}
69+
{% else -%}
70+
{% set val_expr %}Svix::serialize_primitive({{ val_expr }}){% endset -%}
71+
{% endif -%}
72+
{% if type.name is endingwith "Patch" and field.nullable -%}
73+
out['{{ field.name }}'] = {{ val_expr }} if @__{{ field.name | to_snake_case }}_is_defined
74+
{% else -%}
75+
out['{{ field.name }}'] = {{ val_expr }} if @{{ field.name | to_snake_case }}
76+
{% endif -%}
77+
{% endfor -%}
78+
out
79+
end
80+
81+
# Serializes the object to a json string
82+
# @return String
83+
def to_json
84+
JSON.dump(serialize)
85+
end
86+
end
87+
end

0 commit comments

Comments
 (0)