Let’s say I’m integrating Twilio (or any third party api) into a Rails project. I’ve got the gem loaded by bundler in my Gemfile and I’ve setup config/twilio.yml to store my api keys.
To instantiate a new Twilio client I’ll have to parse that YAML file and load up whatever the config is for the current Rails.env. To mitigate parsing that file each time I’ve just pushed the config onto MyApp::Application.config.twilio in config/initializers/twilio.rb
Now when I need a Twilio client I’m faced with the verbose: client = Twilio::REST::Client.new(MyApp::Application.config.twilio['sid'], MyApp::Application.config.twilio['auth'])
I’d like to wrap that up in a helper somewhere so it’s easily accessible from different parts of the system. Is there an idiomatic place to define that helper?
This may work for you:
I usually define stuff like that in
lib/my_company/twilio_integration.rb, which defines everything needed for the 3rd party integration, includingMyCompany::TwilioIntegration.client.Perhaps a better location would be in
app/models/twilio_integration.rb, which could defineTwilioIntegration.client