I'm trying to get a better understanding of the following lines of code involving EventMachine. Trying to learn Ruby the hard way.
What does EventMachine.run does in this code?
What does |chunk| means in this case?
Also, what does 'while line' does? Is line a Ruby syntax? I couldn't seem to find anything related to that.
#create an HTTP request ob to URL above with user authorization
EventMachine.run do
http = EventMachine::HttpRequest.new(url).get :head => { 'Authorization' => [ user, password ] }
# Initiate an empty string for the buffer
buffer = ""
# Read the stream by line
http.stream do |chunk|
buffer += chunk
while line = buffer.slice!(/.+\r?\n/) #cut each line at newline
handle_tweet JSON.parse(line) #send each tweet object to handle_tweet method
end
end
end