Created
February 25, 2011 18:42
-
-
Save Servana/844274 to your computer and use it in GitHub Desktop.
STDIN JSON parser
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'fcntl' | |
require 'json' | |
block = "" | |
# Set $stdin to be non-blocking | |
$stdin.fcntl(Fcntl::F_SETFL,Fcntl::O_NONBLOCK) | |
begin | |
$stdin.each_line { |line| | |
block << line | |
} | |
json = JSON.parse(block) | |
print json['data'].unpack("m") | |
rescue Errno::EAGAIN | |
# Remove this thread since we won't be reading from $stdin | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment