Sha256: 966badd3fae4f500fb5bb12ae014af69b108c9ada19c1a4bf4e1a29dc94d356c

Contents?: true

Size: 1.81 KB

Versions: 1

Compression:

Stored size: 1.81 KB

Contents

require 'uri'
require 'net/http'

module Sfp::Net
end

module Sfp::Net::Helper
	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	

	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

	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

	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
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sfpagent-0.1.14 lib/sfpagent/net_helper.rb