Mange / roadie

Making HTML emails comfortable for the Ruby rockstars

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Support URLs with Mandrill *|MERGE VARS|*

dblandin opened this issue · comments

I'm currently getting the following error when roadie tries to process a template with a Mandrill merge var:

Roadie::InvalidUrlPath: Cannot use path "https://domain.com/path?token=*|token|*" in URL generation. Caused by: bad URI(is not URI?): https://domain.com/path?token=*|token|*
        from /var/www/app/shared/bundle/ruby/2.1.0/gems/roadie-3.0.0/lib/roadie/url_generator.rb:114:in `rescue in parse_path'
        from /var/www/app/shared/bundle/ruby/2.1.0/gems/roadie-3.0.0/lib/roadie/url_generator.rb:112:in `parse_path'
        from /var/www/app/shared/bundle/ruby/2.1.0/gems/roadie-3.0.0/lib/roadie/url_generator.rb:108:in `path_is_absolute?'
        from /var/www/app/shared/bundle/ruby/2.1.0/gems/roadie-3.0.0/lib/roadie/url_generator.rb:58:in `generate_url'
        from /var/www/app/shared/bundle/ruby/2.1.0/gems/roadie-3.0.0/lib/roadie/url_rewriter.rb:47:in `generate_url'
        from /var/www/app/shared/bundle/ruby/2.1.0/gems/roadie-3.0.0/lib/roadie/url_rewriter.rb:71:in `transform_element'
        from /var/www/app/shared/bundle/ruby/2.1.0/gems/roadie-3.0.0/lib/roadie/url_rewriter.rb:25:in `block in transform_dom'

URI, which roadie's URLGenerator is using, doesn't seem to like this type of URL:

>> URI("https://domain.com/path?token=*|token|*")
=> URI::InvalidURIError: bad URI(is not URI?): https://domain.com/path?token=*|token|*

Any thoughts on how to resolve this issue? It's preventing us from upgrading to 3.0.

You need to encode these URLs using standard URL encoding, but I guess
whatever tool interpolates these keywords don't support URL-encoded tokens.

You've got two options then. Either you disable URL handling, or you encode
them in your source code, but add an after_transformation that restore
them to the expected state.

I'll look into handling this better in the future. Maybe I can add an
option to ignore invalid URLs or something.

Let me know if you need help with implementing any of these workarounds.

I've marked this as a feature now and will look into making it possible to have Roadie just ignore URLs that are clearly absolute, even when they cannot be parsed correctly.

Roadie 3.0.1 should handle this gracefully now (if the URL is absolute).

Unfortunately some Mandrill merge variables will still fail, like the auto-unsubscribe link, as the variable is prepended to the URL like this:

<a href="_|UNSUB:http://example.com|_"&gt;Click here to unsubscribe.</a>

Would this help?

document.after_transformation = ->(doc) {
  doc.css('[data-mandrill-href]').each do |element|
    element['href'] = element.delete 'data-mandrill-href'
  end
}

# roadie-rails:
def roadie_options
  super.combine(after_transform: method(&:inline_mandrill))
end

def inline_mandrill(doc)
  doc.css('[data-mandrill-href]').each do |element|
    element['href'] = element.delete 'data-mandrill-href'
  end
end
<a data-mandrill-href="*|no longer invalid HTML|*">Link</a>

method(:inline_mandrill)) instead of method(&:inline_mandrill)). ;)

;-)

tis 28 apr 2015 17:49 Nima Izadi notifications@github.com skrev:

method(:inline_mandrill)) instead of method(&:inline_mandrill)). ;)


Reply to this email directly or view it on GitHub
#91 (comment).

If we wanted to do something like this is there a way to configure it via an initializer or is overriding roadie_options in the mailer the only way? (need to do something similar for sendgrid's unsubscribe tag)

The initializer takes the same options as the roadie_options in the
mailers. It's up to you where you want to configure it.

Den mån 11 juli 2016 22:27Greg Jastrab notifications@github.com skrev:

If we wanted to do something like this is there a way to configure it via
an initializer or is overriding roadie_options in the mailer the only
way? (need to do something similar for sendgrid's unsubscribe tag)


You are receiving this because you modified the open/close state.
Reply to this email directly, view it on GitHub
#91 (comment), or mute
the thread
https://github.com/notifications/unsubscribe/AAAGP9hswsRqAhHkriT9e2LYZxxgFE_Nks5qUqc9gaJpZM4CZ6bK
.

In your example code above that looks like you're overriding roadie_options within the mailer. I was unsure how to do that in the initializer, but then realized that was the top method in your example to just set the after_transformation, so in an initializer it would be like:

config.roadie.after_transformation = ->(doc) {
  doc.css('[data-sendgrid-href]').each do |elem|
    elem['href'] = elem.delete 'data-sendgrid-href'
  end
}

instead of operating on document. Thanks!

@Mange thanks for all the help here. I got my configuration working on this for the most part, but am having trouble getting the merge tags to transfer to the href properly; instead, some part of the process is HTML escaping the merge tags, so my link output ends up looking something like this:

<a href="*%7Csome_url%7C*">Click here</a>

Not sure if this is Nokogiri doing this or what, but wondering if you have some insight, thanks!