Module: Nuri::Net::Helper

Included in:
Sfp::BSig
Defined in:
lib/sfpagent/net_helper.rb

Instance Method Summary collapse

Instance Method Details

#get_data(address, port, path, open_timeout = 5, read_timeout = 1800) ⇒ Object

Raises:

  • (Exception)


44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/sfpagent/net_helper.rb', line 44

def get_data(address, port, path, open_timeout=5, read_timeout=1800)
  address = address.to_s.strip
  port = port.to_s.strip
  path = path.to_s.strip
  raise Exception, "Invalid parameters [address:#{address},port:#{port},path:#{path}]" if
    address.length <= 0 or port.length <= 0 or path.length <= 0

  path.sub!(/^\/+/, '')
  url = URI.parse("http://#{address}:#{port}/#{path}")
  req = Net::HTTP::Get.new(url.path)
  http_request(url, req, open_timeout, read_timeout)
end

#http_request(uri, request, open_timeout = 5, read_timeout = 1800) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/sfpagent/net_helper.rb', line 8

def http_request(uri, request, open_timeout=5, read_timeout=1800)
  http = Net::HTTP.new(uri.host, uri.port)
  http.open_timeout = open_timeout
  http.read_timeout = read_timeout
  http.start
  http.request(request) { |res| return [res.code, res.body] }
end

#post_data(address, port, path, data, open_timeout = 5, read_timeout = 1800) ⇒ Object

Raises:

  • (Exception)


16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/sfpagent/net_helper.rb', line 16

def post_data(address, port, path, data, open_timeout=5, read_timeout=1800)
  address = address.to_s.strip
  port = port.to_s.strip
  path = path.to_s.strip
  raise Exception, "Invalid parameters [address:#{address},port:#{port},path:#{path}]" if
    address.length <= 0 or port.length <= 0 or path.length <= 0

  path.sub!(/^\/+/, '')
  url = URI.parse("http://#{address}:#{port}/#{path}")
  req = Net::HTTP::Post.new(url.path)
  req.set_form_data(data)
  http_request(url, req, open_timeout, read_timeout)
end

#put_data(address, port, path, data, open_timeout = 5, read_timeout = 1800) ⇒ Object

Raises:

  • (Exception)


30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/sfpagent/net_helper.rb', line 30

def put_data(address, port, path, data, open_timeout=5, read_timeout=1800)
  address = address.to_s.strip
  port = port.to_s.strip
  path = path.to_s.strip
  raise Exception, "Invalid parameters [address:#{address},port:#{port},path:#{path}]" if
    address.length <= 0 or port.length <= 0 or path.length <= 0

  path.sub!(/^\/+/, '')
  url = URI.parse("http://#{address}:#{port}/#{path}")
  req = Net::HTTP::Put.new(url.path)
  req.set_form_data(data)
  http_request(url, req, open_timeout, read_timeout)
end