What design pattern to implement for accessing api for different service providers

554 Views Asked by At

I would like to add a functionality(more like a gem) in ruby on rails in which a user specifies their service providers name and then it will automatically fetch the related configuration and inject it to model file and creates the necessary migration.

My Question is how i can achieve this. I mean which design pattern should i use so that whenever a user specifies its provider name (e.g. Exotel, Sinch, Twilio etc.), it will inject their corresponding configuration to config files and their api settings to send emails and msgs.

I have already checked this Question, but it doesn't seems to solve my problem.

For e.g. Exotel api configuration for rails is -

Exotel.configure do |c|
  c.exotel_sid   = "Your exotel sid"
  c.exotel_token = "Your exotel token"
end

To send msg

response = Exotel::Sms.send(:from => 'FROM_NUMBER', :to => 'TO_NUMBER', :body => 'MESSAGE BODY')
sms_id = response.sid #sid is used to find the delivery status and other details of the message in future.

While sinch configuration to send msg is -

SinchSms.send('YOUR_APP_KEY', 'YOUR_APP_SECRET', "Your code is #{code}", phone_number)
render status: 200, nothing: true

Now i want gem to do all this stuff based on user input for serivce providers name, i.e, if user enters Exotel than gem will set exotel settings and if it is Sinch than sinch's configuration will be set.

1

There are 1 best solutions below

1
On BEST ANSWER

Sounds like you want the Adapter pattern. https://en.wikipedia.org/wiki/Adapter_pattern or maybe an api gateway http://microservices.io/patterns/apigateway.html