1

is there a ruby curl library that will allow me to duplicate this request:

curl -d '<hello xmlns="http://checkout.google.com/schema/2"/>' https://S_MERCHANT_ID:S_MERCHANT_KEY@sandbox.google.com/checkout/api/checkout/v2/request/Merchant/S_MERCHANT_ID

i have tried curb, but their PostField.content class is not cooperating with google's checkout api. here is the code from my curb request:

c = Curl::Easy.new("https://MY_ID:MY_KEY@sandbox.google.com/checkout/api/checkout/v2/request/Merchant/MY_ID_AGAIN")
c.http_auth_types = :basic
c.username = 'MY_ID'
c.password = 'MY_KEY'
# c.headers["data"] = '<?xml version="1.0" encoding="UTF-8"?><hello xmlns="http://checkout.google.com/schema/2"/>'
c.http_post(Curl::PostField.content('', '<?xml version="1.0" encoding="UTF-8"?><hello xmlns="http://checkout.google.com/schema/2"/>'))
c.perform

i HAVE managed to get it working using ruby's system command, but im not sure how to handle the response from it.

req = system("curl -d '<hello xmlns=\"http://checkout.google.com/schema/2\"/>' https://MY_ID:MY_KEY@sandbox.google.com/checkout/api/checkout/v2/request/Merchant/MY_ID")

I have been at it for 2 hours now. any help would be greatly appreciated, thanks!

Brandan
  • 14,735
  • 3
  • 56
  • 71
Fred Garbutt
  • 187
  • 2
  • 15

5 Answers5

2

You can use IO.popen to read from the child process:

IO.popen(['curl', '-o', '-', '-d', ..., err: [:child, :out]]) do |io|
  response = io.read
end

This example combines standard out and standard error into one stream in the child process, and it forces curl to redirect output to standard out via -o. You would specify your other options in place of the ....

Brandan
  • 14,735
  • 3
  • 56
  • 71
1

I always use Rest Client gem for such use cases, it is very simple in use and have all REST requests out-of-box with whole batch of tuning parameters.

Your code will look like something similar to this:

url = "sandbox.google.com/checkout/api/checkout/v2/request/Merchant/#{S_MERCHANT_ID}"
credentials = "#{S_MERCHANT_ID}:#{S_MERCHANT_KEY}"
RestClient.post "https://credentials@#{url}", '<hello xmlns="http://checkout.google.com/schema/2"/>' 
  • brilliant! exactly what i needed. I think this is a bit nicer that the IO suggestion below, although they both are getting the job done quite nicely! thanks so much!!! – Fred Garbutt Oct 18 '12 at 09:39
0

Alternatively, you can use a HTTP request library such as Typheous (https://github.com/typhoeus/typhoeus). Is there anything that binds you with "curl"?

Bryan
  • 3,220
  • 3
  • 26
  • 31
  • ah i will look into typhoeus thanks! i am only bound to curl in as far as it is the only library i can get to emulate that request properly. my main pang is i dont really understand how the -d option works in curl. i looked into it, but there wasnt a lot of information and the intuitive alternatives (curb's PostField.content) werent replicating the request properly – Fred Garbutt Oct 18 '12 at 00:45
  • FYI: Typheous uses libcurl behind the scenes via FFI! – deltheil Oct 18 '12 at 07:33
0

I would have curl put the result in a file, and then open the file using ruby and read it ( File.open)

Or us httparty

Joelio
  • 4,621
  • 6
  • 44
  • 80
  • ah smart i didnt think of that! haha maybe i will do that it seems a bit like a last resort though for just getting a response from an http request. – Fred Garbutt Oct 18 '12 at 00:49
0

I figured it out (YAAAAY!)

if anyone else is having this problem, here is the solution.

executable commands work fine in the command line, but if you are trying to render the output of an executable command from a controller in rails, make sure you use render :json instead of render :text to print the results.

for some reason the render :text was only outputting bits and pieces of my command's output (and driving me insane in the process).

For those of you trying to integrate with google checkout in rails, here is how you make http requests to google:

First step: add rest-client to your Gemfile. here is how to do it from the command line:

$ cd /path/to/your/rails/app
$ sudo nano Gemfile

Next, add the gem to your gemfile by placing the following somewhere in your Gemfile

$ gem "rest-client"

next, run bundle install

$ bundle install

restart your server. if apache2:

$ sudo service apache2 reload

if webrick:

$ rails s

then, in your controller (assuming you have rails set up and are able to access a controller from the browser) write the following code:

$ url = "https://YOUR_GOOGLE_CHECKOUT_MERCHANT_ID:YOUR_GOOGLE_CHECKOUT_KEY@sandbox.google.com/checkout/api/checkout/v2/request/Merchant/YOUR_GOOGLE_CHECKOUT_MERCHANT_ID"
$ req = RestClient.post(url, '<hello xmlns="http://checkout.google.com/schema/2"/>')
render :json => req

Please don't forget to replace YOUR_GOOGLE_MERCHANT_ID with your actual merchant id and YOUR_GOOGLE_CHECKOUT_KEY with your actual google checkout key

<?xml version="1.0" encoding="UTF-8"?>
<bye xmlns="http://checkout.google.com/schema/2" serial-number="1dfc3b90-1fa6-47ea-a585-4d5482b6c785" />

(answer courtesy of nexo)

Termininja
  • 6,620
  • 12
  • 48
  • 49
Fred Garbutt
  • 187
  • 2
  • 15