Parse curl information in Rails application - ruby-on-rails

This is my first time using curl in my Rails 4 App. I am trying to use Plaid with Stripe. I am able to successful exchange the public token for the stripe bank account token.
Stripe with Plaid ACH
Here's my controller action.
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
In Terminal with JSON.parse(results)
{"account_id"=>"ACCOUNT_ID", "stripe_bank_account_token"=>"12345678abcd", "sandbox"=>true, "access_token"=>"test_citi"}
How does one get the stripe_bank_account_token into the controller?
UPDATE
I am using the Figaro Gem to hide the 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" }

You shouldn't pipe to curl from Ruby code especially when it involves user input.
Rather you should use the built in Ruby HTTP Client, a gem like RestClient, or even better the Plaid Ruby Gem.
gem install plaid
then just
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

Just create new method for plaid, smth like below.
Also, Good to use HTTP client or REST client
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

Related

Updating all active twilio Flow to ended error

So I want to run this pythong script to end all active Flows currently executing
executions = client.studio \
.v1 \
.flows('FWXXXXXX') \
.executions \
.list(limit=20)
for record in executions:
if record.status == 'active':
execution = client.studio \
.flows('FWXXXXXX') \
.executions(record.sid) \
.update(status='ended')
print('ending', execution)
I get this error "'ExecutionContext' object has no attribute 'update'"
I want to end a Twilio flow, but the documentation on the twilio website does not work for me in this case.
Twilio developer evangelist here.
Since you have already collected the executions when you list them from the API, you should be able to use that object to update the status. Try this:
executions = client.studio \
.v1 \
.flows('FWXXXXXX') \
.executions \
.list(limit=20)
for record in executions:
if record.status == 'active':
record.update(status='ended')
print('ending', record)

Instagram Graph API Returning "error_message"=>"Invalid platform app"

Im trying to get authenticate with Instagram Graph API. When I do the following with a curl command it returns my access_token.
When I use HTTParty gem, Instagram Graph API returns the following:
[22] pry(InstagramService)* )
=> {"error_type"=>"OAuthException", "code"=>400, "error_message"=>"Invalid platform app"}
I really tried but I can't see what im doing wrong. Here is my code:
class InstagramService
def self.get_access_token(code)
token_params = {
client_id: ENV['INSTAGRAM_APP_ID'],
client_secret: ENV['INSTAGRAM_APP_SECRET'],
grant_type: "athorization_code",
redirect_uri: ENV['INSTAGRAM_OAUTH_REDIRECT_URL'],
code: code # <-- This is the code sent by Instagram API. Used to get access token.
}
request = HTTParty.post(
"https://api.instagram.com/oauth/access_token",
body: token_params.to_json,
headers: { 'Content-Type' => 'application/json' }
)
end
end
Does anyone know whats wrong? Why am I getting this error instead of the access_token, the same that I get when I use the curl command.
curl -X POST \
https://api.instagram.com/oauth/access_token \
-F client_id=684477648739411 \
-F client_secret=eb8c7... \
-F grant_type=authorization_code \
-F redirect_uri=https://socialsizzle.herokuapp.com/auth/ \
-F code=AQDp3TtBQQ...

Curl request in Ruby for Facebook Api

I'm trying to make a curl call with curb gem equivalent to:
curl \
-F 'name=My new CA' \
-F 'subtype=CUSTOM' \
-F 'description=People who bought from my website' \
-F 'access_token=<ACCESS_TOKEN>' \
https://graph.facebook.com/v2.5/act_<AD_ACCOUNT_ID>/customaudiences
So far my code looks like:
cr = Curl::Easy.http_post("https://graph.facebook.com/v2.5/act_XXXXXXXXXXXXXXX/customaudiences?access_token=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx") do |curl|
curl.headers['name']='My new CA'
curl.headers['subtype']='CUSTOM'
curl.headers['description']='People who bought from my website'
curl.headers['access_token']='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
end
pp cr.body_str
However, as a response I get this:
=> "{\"error\":{\"message\":\"(#100) Missing parameter(s): subtype\",\"type\":\"OAuthException\",\"code\":100,\"fbtrace_id\":\"BOd\\/mmhQkkP\"}}"
Could someone explain me what am I doing wrong?
Thank you!
You can also just use a system call like this:
name = "something"
url = "http://www.example.com"
result = `curl -F 'name=#{name}' #{url}`
result will hold the output of your system call. For more sophisticated http requests, you probably want to take a look at faraday (https://github.com/lostisland/faraday)

MALFORMED_REQUEST on PayPal execute payment

I've lost almost two whole days trying to solve PayPal related issues.
I'm trying to execute a payment after user's approval, but I'm getting MALFORMED_REQUEST everytime I send the Curl request.
def execute
# Get the access_token
token = get_paypal_token(false)
payer_id = params[:PayerID]
payment_id = params[:paymentId]
# Creating the data object
stringJson = {:payer_id => "#{payer_id}"}
# You can see that I hard-coded the payer_id to ensure the JSON is correct
curlString = `curl -v -H "Authorization: Bearer #{token}" -H "Content-Type: application/json" -d '{"payer_id" : "QULQSFESGMCX2"}' https://api.sandbox.paypal.com/v1/payments/payment/#{payment_id}/execute/`
end
And the response is:
"{\"name\":\"MALFORMED_REQUEST\",\"message\":\"The request JSON is not well formed.\",\"information_link\":\"https://developer.paypal.com/webapps/developer/docs/api/#MALFORMED_REQUEST\",\"debug_id\":\"6431589715660\"}"
Looks like you are running it from windows. I tried your command from my windows and my test server receiving the json as '{payer_id. It is happening because of your single quote.
I have changed the single quotes into double and it is working fine here with me.
-d "{\"payer_id\" : \"QULQSFESGMCX2\"}"
You can run the curl command from the console and see what happens, after that you can push it inside your ruby script code.

cURL to Rails "-d" format conversion

Hello I'm working with the STRAVA api and a gem built off of it. The command I'm trying to do is:
$ curl -G https://www.strava.com/api/v3/segments/explore \
-H "Authorization: Bearer 83ebeabdec09f6670863766f792ead24d61fe3f9" \
-d bounds=37.821362,-122.505373,37.842038,-122.465977 \
How do I format my ruby on rails command to make the request. Currently:
#client.segment_explorer([37.821362,-122.505373,37.842038,-122.465977])
The source code for the api gem looks like this:
def segment_explorer(args = {}, options = {}, &block)
# Fetches the connections for given object.
api_call('segments/explore', args, 'get', options, &block)
end
How would I pass the "-d" as a longitude into this method to successful create an api call?
Which gem are you using? I did a quick search, and couldn't find the code you refer to anywhere. However, at a guess, you might want to try passing through a hash of arguments, where they key is the same name as the URL parameter.
For example:
#client.segment_explorer({
bounds: "37.821362,-122.505373,37.842038,-122.465977"
})
If that doesn't work, can you provide a link to the gem you're using?

Resources