mailjet / mailjet-gem

[API v3] Mailjet official Ruby GEM
https://dev.mailjet.com
Other
130 stars 72 forks source link

Issues using this gem outside of Rails #257

Open wout opened 6 months ago

wout commented 6 months ago

Using the example code from the docs to send an email, this gem raises the following error:

/home/wout/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/mailjet-1.7.10/lib/mailjet/resource.rb:345:in `block in formatted_payload': undefined method `blank?' for false (NoMethodError)

        v.blank?
         ^^^^^^^
    from /home/wout/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/activesupport-7.1.3.2/lib/active_support/hash_with_indifferent_access.rb:334:in `reject!'
    from /home/wout/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/activesupport-7.1.3.2/lib/active_support/hash_with_indifferent_access.rb:334:in `block in reject'
    from <internal:kernel>:90:in `tap'
    from /home/wout/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/activesupport-7.1.3.2/lib/active_support/hash_with_indifferent_access.rb:334:in `reject'
    from /home/wout/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/mailjet-1.7.10/lib/mailjet/resource.rb:344:in `formatted_payload'
    from /home/wout/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/mailjet-1.7.10/lib/mailjet/resource.rb:281:in `save'
    from /home/wout/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/mailjet-1.7.10/lib/mailjet/resource.rb:304:in `save!'
    from /home/wout/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/mailjet-1.7.10/lib/mailjet/resource.rb:125:in `block in create'
    from <internal:kernel>:90:in `tap'
    from /home/wout/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/mailjet-1.7.10/lib/mailjet/resource.rb:124:in `create'
    from (irb):7:in `<main>'
    from <internal:kernel>:187:in `loop'
    from /home/wout/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/irb-1.11.0/exe/irb:9:in `<top (required)>'
    from /home/wout/.rbenv/versions/3.3.0/bin/irb:25:in `load'
    from /home/wout/.rbenv/versions/3.3.0/bin/irb:25:in `<main>'

That's because the persisted property (boolean) is also stored in the attributes object: https://github.com/mailjet/mailjet-gem/blob/master/lib/mailjet/resource.rb#L344

I could get around it by monkey-patching the code:

      payload = attributes.reject do |_, v|
        v.nil? ||
          (v.is_a?(String) && v.strip.empty?) ||
          (v.respond_to?(:empty?) && v.empty?)
      end

But I'm not sure if that covers all cases.

Juksefantomet commented 2 months ago

Adding some more details here :smile:

I had the same issue, this was in my case due to ruby v3.3.3.

I rolled my Ruby version down to 2.6.6 (which was being used in a working project) for testing purposes and the mailjet gem did not attempt this "stunt" and the email was sent successfully.