1

我想在 ruby​​ on rails 中添加一个功能(更像是一个 gem),其中用户指定他们的服务提供者名称,然后它将自动获取相关配置并将其注入模型文件并创建必要的迁移。

我的问题是我如何能做到这一点。我的意思是我应该使用哪种设计模式,这样每当用户指定其提供者名称(例如 Exotel、Sinch、Twilio 等)时,它就会将其相应的配置注入配置文件及其 api 设置以发送电子邮件和消息。

我已经检查过这个问题,但它似乎并没有解决我的问题。

例如,用于 Rails 的 Exotel api 配置是 -

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

发送消息

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.

虽然发送消息的 sinch 配置是 -

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

现在我想让gem根据用户输入的服务提供商名称来做所有这些事情,即,如果用户输入Exotel,那么gem将设置exotel设置,如果它是Sinch,则将设置sinch的配置。

4

1 回答 1

1

听起来你想要适配器模式。https://en.wikipedia.org/wiki/Adapter_pattern或者 api 网关http://microservices.io/patterns/apigateway.html

于 2016-06-28T16:53:53.360 回答