Method: VTools::SharedMethods::Common#network_call

Defined in:
lib/vtools/shared_methods.rb

#network_call(url) ⇒ Object

calls TCP/IP applications



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/vtools/shared_methods.rb', line 59

def network_call url
  require "socket"

  url =~ %r#^([a-z]+://)?(?:www.)?([^/:]+)(:[\d]+)?(.*)$#
  protocol, host, port, route =
    ($1 || '')[0...-3], $2, ($3 || ":80")[1..-1].to_i, "/#{$4.to_s.gsub(/^\//, '')}"

  begin
    sock = TCPSocket.open(host, port)
    sock.print "GET #{route} HTTP/1.0\r\n\r\n"
    response = sock.read.split("\r\n\r\n", 2).reverse[0]
    sock.close
  rescue => e
    log :error, e
  end

  response
end