Last active
August 29, 2015 14:22
-
-
Save ollie/d2a940d379867fddd992 to your computer and use it in GitHub Desktop.
Telnet test
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 'net/telnet' | |
class Client | |
# Fetch weather forecast for NYC. | |
# | |
# @return [String] | |
def response | |
fetch_all_in_one_response | |
# fetch_multiple_responses | |
ensure | |
disconnect | |
end | |
private | |
# Do all the commands at once and return everything on one go. | |
# | |
# @return [String] | |
def fetch_all_in_one_response | |
client.cmd("\nNYC\nX\n") | |
end | |
# Do multiple calls to retrieve the final forecast. | |
# | |
# @return [String] | |
def fetch_multiple_responses | |
client.cmd("\r") do | |
client.cmd("NYC\r") do | |
client.cmd("X\r") do |forecast| | |
return forecast | |
end | |
end | |
end | |
end | |
# Connect to remote server. | |
# | |
# @return [Net::Telnet] | |
def client | |
@client ||= Net::Telnet.new( | |
'Host' => 'rainmaker.wunderground.com', | |
'Timeout' => false, | |
'Output_log' => File.open('output.log', 'w') | |
) | |
end | |
# Close connection to the remote server. | |
def disconnect | |
client.close | |
end | |
end | |
forecast = Client.new.response | |
puts forecast |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment