I am using Ruby on Rails 3 and I would like to have a “central place” where to definitely set the SSL certificate to use inside my application.
Almost everywhere in code I have HTTPS requests like this
e = Typhoeus::Request.get("https://example.com/action",
:ssl_cacert => "ca_file.cer",
:ssl_cert => "acert.crt",
:ssl_key => "akey.key",
[...]
end
So, in order to specify a SSL certificate for all my request, I would like to set a global variable (I heard that global variable can be dangerous…) or something like that in a safe way.
You could build a SSL helper class, and use that (I’d put it in lib, but that’s mostly just user preference):
Where reverse_merge! is a convenient Rails extension to give default options for a hash (if the original hash has those keys already, they won’t be overwritten). You avoid setting a global variable, too, by using a class constant. And in the rest of your code, you’re able to call
or
which are a lot cleaner, in comparison.
Hope this helps!