pompon
pompon

Reputation: 31

Rails curl syntax

I can run the following command from my rails app:

Hash.from_xml(%x{curl -d "admin=true" http://localhost:8888} ) rescue nil

now I want to replace the "admin=true" by a variable If I have x = "admin=true" how can I then write the above command?

Thanks a lot

Upvotes: 3

Views: 5927

Answers (2)

gertas
gertas

Reputation: 17145

You can use curl directly in Ruby instead depending on command and hardcoded parameters - current code is harder to maintain and doesn't tell you exactly what may be wrong if something bad happens. See ruby curl.

The ideal option actually would be dropping use curl and use rest-client.

Hash.from_xml(RestClient.get('http://localhost:8888/', :admin=>true))

No dependencies - just pure ruby. Proper exceptions raised in any case. Trivial parameter specing. POST verb available.

Upvotes: 4

Ryan Bigg
Ryan Bigg

Reputation: 107718

x = %Q{"admin=true"}
Hash.from_xml(%x{curl -d "#{x}" http://localhost:8888} ) rescue nil

The %Q{}syntax indicates a quoted string, which is kind of like a "super"/"enhanced" version of a double-quoted string.

The #{} syntax inside %x{} is called interpolation and it allows to evaluate Ruby code inside a string.

Upvotes: 0

Related Questions