在Rails应用程序中解析curl信息

时间:2016-07-19 20:02:49

标签: ruby-on-rails json curl stripe-payments plaid

这是我第一次在我的Rails 4 App中使用curl。我正在尝试使用条纹格子。我能够成功地为条带银行帐户令牌交换公共令牌。

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导入控制器?

更新

我正在使用费加罗宝石来隐藏参数/凭证..

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" }

2 个答案:

答案 0 :(得分:1)

你不应该从Ruby代码中卷曲,特别是涉及用户输入时。

相反,你应该使用内置的Ruby HTTP Client,像RestClient这样的宝石,或者更好的使用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

答案 1 :(得分:0)

只需创建格子图案的新方法,如下图所示。

此外,很高兴使用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