解析 Rails 应用中的 curl 信息

Parse curl information in Rails application

这是我第一次在 Rails 4 应用程序中使用 curl。我正在尝试将格子与条纹一起使用。我能够成功地将 public 令牌交换为条纹银行帐户令牌。

Stripe with Plaid ACH

这是我的控制器操作。

    def create
      results = `curl https://tartan.plaid.com/exchange_token \
        -d client_id="CLIENT_ID" \
        -d secret="SECRET_KEY" \
        -d public_token="#{params[:public_token]}" \
        -d account_id="#{params[:meta][:account_id]}"`
    end

在终端中 JSON.parse(结果)

{"account_id"=>"ACCOUNT_ID", "stripe_bank_account_token"=>"12345678abcd", "sandbox"=>true, "access_token"=>"test_citi"}

如何将 stripe_bank_account_token 放入控制器?

更新

我正在使用 Figaro Gem 来隐藏 params/credentials..

results = 
`curl https://tartan.plaid.com/exchange_token \
  -d client_id="#{ ENV['PLAID_CLIENT_ID'] }" \
  -d secret="#{ ENV['PLAID_SECRET_KEY'] }" \
  -d public_token="#{params[:public_token]}" \
  -d account_id="#{params[:meta][:account_id]}"`

   # here's how I get the stripe_bank_account_token
    break_down = JSON.parse(results)
    x =  break_down.select { |key, val| key == "stripe_bank_account_token" }

只需为格子创建新方法,如下所示。

此外,最好使用 HTTP 客户端或 REST 客户端

HTTP client

REST client

def create
    res = plain_curl(params)
    puts res.inspect #there you will see your respond json obj in rails console.
end

private

def plain_curl(params)
  #it should return you json object, if not just add return before result.
  results = `curl https://tartan.plaid.com/exchange_token \
    -d client_id="CLIENT_ID" \
    -d secret="SECRET_KEY" \
    -d public_token="#{params[:public_token]}" \
    -d account_id="#{params[:meta][:account_id]}"`
end

您不应该通过管道从 Ruby 代码卷曲,尤其是当它涉及用户输入时。

你应该使用内置的 Ruby HTTP 客户端,一个像 RestClient 这样的 gem,或者更好的 Plaid Ruby Gem.

gem install plaid

然后

require 'Plaid'
Plaid.config do |p|
  p.client_id = '<<< Plaid provided client ID >>>'
  p.secret = '<<< Plaid provided secret key >>>'
  p.env = :tartan  # or :api for production
end

user = Plaid::User.exchange_token(params[:public_token], params[:meta][:account_id], product: :auth)
user.stripe_bank_account_token