【问题标题】:What design pattern to implement for accessing api for different service providers为不同的服务提供者访问 api 实现什么设计模式
【发布时间】:2016-06-24 07:18:42
【问题描述】:

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

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

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

例如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 的配置。

【问题讨论】:

    标签: ruby-on-rails ruby rails-activerecord exotel-api


    【解决方案1】:

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

    【讨论】:

    • 后来我发现了使用哪种设计模式。是的,你是对的,它的Adapter 模式。
    猜你喜欢
    • 2014-10-03
    • 2011-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-05
    • 1970-01-01
    • 2017-03-25
    相关资源
    最近更新 更多