How to get the output of curl [duplicate] - ruby

This question already has answers here:
Getting output of system() calls in Ruby
(18 answers)
Closed 2 years ago.
I am using the system curl as I cannot get the ruby curl to work. How do I get the output of the curl into my ruby variable? the example below shows that the variable did not get the output of the curl
url1="\"https://ca.yahoo.com/\""
a=Kernel.system "curl #{url1}"
irb(main):026:0> a
=> true

Kernel#system Doesn't Capture Standard Output
Kernel#system only returns true, false or nil, so you can't capture command output that way. Instead, use Kernel#` (or its alternative %x{} syntax) to capture standard output. For example:
url = 'https://ca.yahoo.com/'
output = %x(curl "#{url}")
For more complex things, you may also want to look at the Open3 module from the Ruby Standard Library. It has various methods for supporting pipelines, and different ways to treat standard input, standard output, and standard error separately when necessary.

Related

Why is Ruby STDOUT buffering when I don't expect it to?

The following code is a simplification of my current situation. I have a JSON log source which I continuously fetch and write to stdout with puts.
#!/usr/bin/env ruby
require "json"
loop do
puts({ value: "foobar" }.to_json)
sleep 1
end
I want to be able to pipe the output of this script into jq for further processing, but in a 'stream'-friendly way, using unix pipes. Running the above code like so:
./my_script | jq
Results in an empty output. However, if I place an exit statement after the sleep call, the output is sent through the pipe to jq as expected. I was able to solve this problem by calling $stdout.flush following the puts call. While it's working now, I'm not sure why. $stdout.sync is set to true by default (see IO#sync). It seems to me that if sync was enabled, then Ruby should be doing no output buffering, and calling $stdout.flush should not be required - yet it is.
My follow-up question is about using tail instead of jq. It seems to me that I should be able to pipe a text stream into tail the same way I pipe it into jq, but neither method (with the $stdout.flush call or without it) works - the output is just empty.
As #Ry points out in the comments, $stdout.sync is true by default in IRB, but this is not necessarily the same for scripts.
So you should set $stdout.sync = true to be sure to prevent buffering.

How to execute a Ruby code from inside a Ruby script?

I'm trying to execute a Ruby script file.
Assuming the input is a string that contains the file content.
What are the possible ways? taking into considerations that I need to keep the output of the executed file whether stdout or not separated from the Main script.
As an example of what I'm trying to do is have a function called execute(code)
Then calling execute('4 + 5') would return 9 although I can write a whole Ruby script in the place of '4 + 5'.
If anyone can forward me to any related tutorials or books, I'd be thankful :)
You can call shell commands in Ruby, it's as simple and intuitive as surrounding your desired command in backticks.
The output gets returned, so just save it to a variable:
script1.rb:
puts "asdf"
script2.rb:
output = `ruby script1.rb`
puts output
"asdf"
I question what exactly it is you're trying to do, though. Because this is totally unintuitive and roundabout. Are you sure you aren't just looking for a module or something?

Decode URL Unix/Bash Command Line (without sed) [duplicate]

This question already has answers here:
Bash script to convert from HTML entities to characters
(12 answers)
Closed 4 years ago.
I am scraping a website with curl and parsing out what I need.
The URLs are returned with Ascii encoded characters like
GET v2.12/...?fields={fieldname_of_type_Tab} HTTP/1.1
How can I convert this to UTF-8 (char) directly from the command line (ideally something I can pipe | to) so that the result is...
GET v2.12/...?fields={fieldname_of_type_Tab} HTTP/1.1
EDIT: There are a number of solutions with sed but the regex that goes along with it is quite ugly. Since the provided answer leveraging perl is very clean I hope we can leave this question open
It's html-entities.
Decode like this using perl :
$ echo 'http://domain.tld/?fields={fieldname_of_type_Tab&#125' |
perl -MHTML::Entities -pe 'decode_entities($_)'
Output :
http://domain.tld/?fields={fieldname_of_type_Tab}

Find declared function from user input

I have a prompt in my "app", like irb that takes an input,I want it to parse the input and execute a function that I've defined.
Similarly, my app takes an input through gets, and calls the function.
For example,
command = gets.gsub("\n","")
takes an input "pwd", now I want to call the function pwd, which is defined below:
def pwd
Dir.pwd
end
Now, I could simply use if conditions to do so, but a lot of these conditions wouldn't be as elegant as Ruby's philosophy requires it to be.
The Question
I want to parse the input to call a defined function.
Like, "pwd" calls pwd,
"ls" calls its equivalent function that I have defined.
How do I do this?
Comment if question is still not clear.
Cheers.
Addressing Possible Duplicate
The question suggested specifically wants to run Shell commands. Whereas, I am using only built-in Ruby methods and classes and maybe in the future I'll even use Gems, so as to attain platform independence. My commands may look and behave like shell, but I'm not actually running shell. Therefore, my question is not a possible duplicate.
Furthermore, future readers will find it helpful for parsing input in Ruby.
You can write a mapping for the different inputs in the form of a case:
case command
when "pwd"
Dir.pwd # or move it into another method and call it here
when "ls"
# etc
end
or a hash, which is slightly more concise
actions = {
pwd: -> { Dir.pwd },
ls: -> { <foobar> }
}
actions[command.to_sym].call
You can also make method names matching the commands and use send, but don't do this if the input is coming from strangers:
def pwd
Dir.pwd
end
command = command.to_sym
send(command)

How do I capture the output of a pry shell command?

I'm using pry and I want to capture, and work with output of a shell command.
For example, If I run
pry(main)> .ls
I want to get the list of files into an array that I can work with in Ruby.
How can I do this?
This is a pretty old question but I'll answer it anyways. There are two primary methods of getting data out of pry commands. The first is if the command sets the keep_retval option to true, which the shell command does not. The second, is to use the virtual pipe. In your example this can be done as:
fizz = []
.ls | {|listing| fizz = listing.split("\n")} # can also be written as
.ls do |listing|
fizz = listing.split("\n")
end
I assume it's some kind of pry's magic ;-)
After quick look at what's happening (I didn't look at pry's source), you might want to use this:
`ls`.split("\n")
or
Dir['./*']
What's good about this solution is that it will work outside of pry

Resources