cURL to Rails "-d" format conversion - ruby-on-rails

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?

Related

RestClient syntax for file uploads

Following the indications provided by this gem , testing in the Rails console the RestClient
RestClient.post "https://upload.twitter.com/1.1/media/upload.json?media_category=tweet_image", :myfile => File.new("/Users/main/Desktop/ss2022-11-03_11.39.11.png", 'rb')
one can assert that the file exists by changing its path. Once verified, the response returns
400 Bad Request. So the request is malformed. Alas, I do not grasp what the second element of the File.new represents
The version 1.1 API documentation provides a suggestion console test
twurl -X POST -H upload.twitter.com "/1.1/media/upload.json?media_category=TWEET_IMAGE&additional_owners=3805104374" -f adsapi-heirarchy.png -F media
the response is the same whether the additional_owners is included or not (logical, it is optional).
What is missing in this sequence?
Thr translation of the twurl syntax to curl syntax would be:
curl "https://upload.twitter.com/1.1/media/upload.json?media_category=tweet_image&additional_owners=3805104374" --data-urlencode #/Users/main/Desktop/adsapi-heirarchy.png --data-urlencode 'media'

Parse curl information in Rails application

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

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.

Writing a curl Post call in Ruby

If i issue a POST request using curl command to github api to add "Resolved" label; things are good.
curl -i -H "Authorization: token xxxxxxxxxxxx" -X POST 'https://git.corp.yahoo.com/api/v3/repos/owner/repo/issues/1/labels' -d '["Resolved"]'}
But when i try to do the same using Curl easy in my Ruby script
set gh_api = https://git.corp.yahoo.com/api/v3/repos/owner/repo/issues/1/labels
curl = Curl::Easy.http_post(settings.gh_api,'["Resolved"]')
do |c|
c.headers = ["Authorization: token xxxxxxxx"]
end
The JSON reponse I get is
"{\"message\":\"Not Found\"}"
What am i doing wrong in my ruby script?
It's not at all clear what question you are asking because there is no question.
What's returned is a JSON response. Why don't you parse it with the JSON class and see what it returns:
require 'json'
puts JSON["{\"message\":\"Not Found\"}"]
=> {"message"=>"Not Found"}
I'm guessing that the problem is in the path of the URL if the response is "Not Found".
My URL was wrong. I ended the URL with a '/'.

Resources