简体   繁体   English

在自定义Rails应用中实现第三方API资源

[英]Implementing third party API resources in a custom rails app

I am a student studying web development at a prominent bootcamp in the United States. 我是一名在美国著名的训练营学习Web开发的学生。 For my capstone project, I have decided to try and build a fairly simple application wherein one authenticated user can send an amount of Bitcoin to another user (I described to my teacher as a bottled tipping platform). 对于我的顶峰项目,我决定尝试构建一个相当简单的应用程序,其中一个经过身份验证的用户可以将一定数量的比特币发送给另一位用户(我向我的老师描述为瓶装小费平台)。 While this sounds simple in theory, there are many points at which I am worried about tripping up. 从理论上讲,这听起来很简单,但是我担心很多地方会绊倒。

I am building this application with rails, using the Coinbase API . 我正在使用Coinbase API使用rails构建此应用程序。 At my school, we have been taught rails with a fairly strict MVC architecture. 在我的学校,我们已经学会了使用相当严格的MVC架构的Rails。 Herein is where I have found my first problem. 在这里,我找到了我的第一个问题。 Regarding resources, we have essentially been building from scratch; 关于资源,我们基本上是从头开始构建的。 this will mark my first time attempting to implement a third-party API on the back end. 这将标志着我第一次尝试在后端实现第三方API。 I have noticed in the Coinbase documentation that there are multiple clearly defined resources which I would need to use. 我在Coinbase文档中注意到,我需要使用多个明确定义的资源。 The most notable resources are users and accounts (I scaffolded accounts as 'profiles', but they serve the same purpose in an ERD). 最值得注意的资源是用户和帐户(我将帐户作为“配置文件”进行了脚手架,但它们在ERD中的作用相同)。

If I am working with these pre-built resources, do I need to fully scaffold them, or can I simply import them into my files, then custom write the code to my specifications? 如果我正在使用这些预先构建的资源,是否需要完全固定它们,还是可以简单地将它们导入到我的文件中,然后根据我的要求自定义编写代码? If so, would it make sense for me to simply rip up my scaffolding and start from scratch? 如果是这样,对我来说简单地撕掉脚手架并从头开始就有意义吗? More broadly, how should I go about fitting pre-existing resources into my MVC architecture? 更广泛地讲,我应该如何将现有资源装入我的MVC体系结构?

Any help is tremendously appreciated! 任何帮助深表感谢!

Going to use Stripe for an example because their docs are the best: 以Stripe为例,因为他们的文档是最好的:

Inside your Rails app, you have a User model. 在Rails应用程序中,您有一个User模型。 For the sake of this example, your User has an id , email , and stripe_customer_id 在此示例中,您的用户具有idemailstripe_customer_id

user.rb : user.rb

before_create :create_stripe_customer

def create_stripe_customer
  stripe_customer = Stripe::Customer.create(
    :email => "your@email.com",
  ) 
  self.stripe_customer_id = stripe_customer.id
end

def stripe_customer
  @stripe_customer ||= Stripe::Customer.find(self.stripe_customer_id)
end 

Using before_create against an external API is frowned upon ("what if it timeouts or errors?") but for the sake of learning what to do, it's fine. before_create在外部API上使用before_create (“它超时还是出错?”),但是为了学习该怎么做,这很好。

If you're asking "should I replicate all the data in my own database?" 如果您问“我应该复制自己数据库中的所有数据吗?” the answer is no; 答案是不; let the API do the work, but cache the results and make sure you don't block your app's requests with external API requests. 让API完成工作,但要缓存结果并确保您不会因外部API请求而阻止应用程序的请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM