Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more to the API #13

Open
jwoertink opened this issue Sep 7, 2022 · 1 comment
Open

Add more to the API #13

jwoertink opened this issue Sep 7, 2022 · 1 comment

Comments

@jwoertink
Copy link
Member

The Sendgrid API has a lot more options than what we're passing currently https://docs.sendgrid.com/api-reference/mail-send/mail-send

Two big ones we miss out on are categories and attachments. There's a few small issues with adding these though. As I was digging around, I noticed some of the docs were actually wrong. For example, one key saying it was a string, but should have said object. The next hurdle is with most of these keys being optional, the compiler starts to get finicky with how they are added.

For example, with the dynamic templates:

def template_id
nil
end
# Define the dynamic data to be replaced
# in your email template. This should be a
# dynamic Hash(String, Any) where the keys
# must match with your template values.
# If `nil`, then no data is sent.
def dynamic_template_data
nil
end

You can either define the content with local templates OR use the dynamic templates, but you can't use both. Sendgrid doesn't really like that. So for this, there's basically an if condition to say if these have a value, then do that otherwise fallback to local templates.

I wanted to avoid requiring you to override 50 methods just to get the customization you need on every single email, but the Crystal compiler doesn't really like when you try to define a hash of random unknown types. We can't use JSON::Any because that doesn't allow you to build dynamically e.g. json["extra_args"] = [] of String.

Proposal

I think maybe we could look at breaking this out more like the Bugsnag shard, and each key becomes a new class defined. Then this hash becomes something like Hash(String, Carbon::SendgridAdapter::Value). That could just be a module in each of these classes.

Now, how to hook it in.... I'm not so sure... What does this look like? 🤷‍♂️

class SomeEmail < Carbon::Email
  def initialize(@recipient)
  end
  
  to @recipient
  subject "Hey"
  # does it look like this?
  categories Carbon::SendgridAdapter::Categories.new("1", "2")

  # maybe you take advantage of the new callbacks?
  before_send do
    # `append_data` would have to be a SendgridAdapter specific method
    adapter.append_data("categories", Carbon::SendgridAdapter::Categories.new("1", "2"))
  end
end
@jwoertink
Copy link
Member Author

@robacarp had a good idea for a configure block that just hooks in to it.

class SomeEmail < BaseEmail
  some_specific_sendgrid_deal do |data|
     data.attachments << Sendgrid::Attachments.new("my_face.jpg")
   end

end

It would look something along these lines. That would be specific to this shard.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant