Repository: sendgrid/sendgrid-ruby (Ruby)

Issue:
Numeric values cause 400 Bad Request with no explanation
#### Issue Summary

Was stumped briefly while upgrading to Ver 3 of the API. I had followed the example code but was getting "400 Bad Request" without any other explanation. 

`mail.custom_args = CustomArg.new(:key => 'list_id', :value => self.list_id)`
`mail.custom_args = CustomArg.new(:key => 'message_id', :value => self.id)`
`personalization.substitutions = Substitution.new(:key => '-person_id-', :value => subscriber.id)
`

Finally figured it might be caused by a few numeric values in the CustomArgs and Substitutions. Voila!

`mail.custom_args = CustomArg.new(:key => 'list_id', :value => self.list_id.to_s)`
`mail.custom_args = CustomArg.new(:key => 'message_id', :value => self.id.to_s)`
`personalization.substitutions = Substitution.new(:key => '-person_id-', :value => subscriber.id.to_s)`

It would be great if the gem automatically converted these values to strings -- particularly since the error message is so generic. Thanks.


#### Technical details:

* sendgrid-ruby Version: master (latest commit: 4.0.6)
* Ruby Version: 2.2


Discussion:
Hi @brian4d,

Agreed and thanks for taking the time to spell out the issue in detail!

I have added this to our backlog for implementation.

With Best Regards,

Elmer
Any word on fixing this? Ideally the API would just return a better error message, and/or accept any valid JSON in `custom_args` (or at least any scalar values).
Hi @percyhanna,

I've added your vote to this issue to help it rise in our backlog. It can continue to rise with additional +1s, comments or a PR. A PR would cause the greatest impact.

Thanks!

Elmer
Hey @thinkingserious can I help with this?
It looks like a simple fix in `initialize` in `custom_arg.rb` 
I guess sometimes `id` fields are ruby objects and not string which caused the issue.
Thanks @sitaramshelke! It's all yours :)
Thanks @sitaramshelke !



Patch:
diff --git a/lib/sendgrid/helpers/mail/custom_arg.rb b/lib/sendgrid/helpers/mail/custom_arg.rb
index ed760e76..69b0558d 100644
--- a/lib/sendgrid/helpers/mail/custom_arg.rb
+++ b/lib/sendgrid/helpers/mail/custom_arg.rb
@@ -4,7 +4,7 @@ module SendGrid
   class CustomArg
     def initialize(key: nil, value: nil)
       @custom_arg = {}
-      (key.nil? || value.nil?) ? @custom_arg = nil : @custom_arg[key] = value
+      (key.nil? || value.nil?) ? @custom_arg = nil : @custom_arg[key.to_s] = value.to_s
     end
 
     def custom_arg=(custom_arg)


Test patch:
diff --git a/test/sendgrid/helpers/mail/test_mail.rb b/test/sendgrid/helpers/mail/test_mail.rb
index 664322ec..18466181 100644
--- a/test/sendgrid/helpers/mail/test_mail.rb
+++ b/test/sendgrid/helpers/mail/test_mail.rb
@@ -207,6 +207,21 @@ def test_add_custom_arg
     assert_equal mail.to_json, expected_json
   end
 
+  def test_add_non_string_custom_arg
+    mail = Mail.new
+    mail.add_custom_arg(CustomArg.new(key: "Integer", value: 1))
+    mail.add_custom_arg(CustomArg.new(key: "Array", value: [1, "a", true]))
+    mail.add_custom_arg(CustomArg.new(key: "Hash", value: {"a" => 1, "b" => 2}))
+    expected_json = {
+        "custom_args"=>{
+                "Integer"=>"1",
+                "Array"=>"[1, \"a\", true]",
+                "Hash"=>"{\"a\"=>1, \"b\"=>2}",
+            }
+    }
+    assert_equal mail.to_json, expected_json
+  end
+
   def test_add_attachment
     mail = Mail.new
     mail.add_attachment('foo')