Sha256: 116531a8dc9c99a8ac90b9b473632a080e8f029f1bfdbd56e4210c7925af165f

Contents?: true

Size: 1.81 KB

Versions: 11

Compression:

Stored size: 1.81 KB

Contents

require 'uri'
require 'net/http'

module Nuri::Net
end

module Nuri::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

11 entries across 11 versions & 1 rubygems

Version Path
sfpagent-0.1.13 lib/sfpagent/net_helper.rb
sfpagent-0.1.12 lib/sfpagent/net_helper.rb
sfpagent-0.1.11 lib/sfpagent/net_helper.rb
sfpagent-0.1.10 lib/sfpagent/net_helper.rb
sfpagent-0.1.9 lib/sfpagent/net_helper.rb
sfpagent-0.1.6 lib/sfpagent/net_helper.rb
sfpagent-0.1.5 lib/sfpagent/net_helper.rb
sfpagent-0.1.4 lib/sfpagent/net_helper.rb
sfpagent-0.1.3 lib/sfpagent/net_helper.rb
sfpagent-0.1.2 lib/sfpagent/net_helper.rb
sfpagent-0.1.1 lib/sfpagent/net_helper.rb