thoughtbot/griddler

Are `charsets` used anywhere at this point?

Closed this issue ยท 2 comments

Hey, folks. Great library ๐ŸŽ‰

We were just bumping into some "illegal/malformed utf-8" exceptions trying to throw some data into a JSON field in postgres and that led me down the path of looking into charsets (which looked promising since we use sendgrid) but it looks like Griddler no longer makes use of charsets.

I searched griddler and the adapter gems and it looks like the only current usage is the griddler docs and test suite.

It looks like usage may have been removed by 4404b71

But I could be missing something. So, are charsets used anywhere at this point? Or should I read that commit referenced above to imply that they're not reliable enough to be trusted?

So a cursory glance makes it look like the charsets param is a SendGrid feature. As such, you'd probably want to look for that in the griddler-sendgrid project. It does indeed look like the usage of that feature was stripped in 4404b71 though. That was prior to Griddler moving the various adapters into independent gems (which is why you see the commit in this project).

I've used charsets to manually encode text and html attributes if email.text.valid_encoding? is false. Like this:

  def charsets
    @_charsets ||= JSON.parse(params[:charsets])
  end

  def fix_encoding(str, charset_key)
    # charset_key is "text" or "html"
    if str && !str.valid_encoding?
      str = str.force_encoding(charsets[charset_key]) rescue str
      if !str.valid_encoding?
        str = str.encode('UTF-8', 'binary', invalid: :replace, undef: :replace, replace: '')
      end
    end
    str
  end