fawzalnzs/github-gem — pull request #1 by fawzalnzs (2013-09-27 19:46:05+00:00)
Title: Upload

...	...	@@ -492,3 +492,60 @@
492	492	end
493	493	puts "-----"
494	494	end
495	+
496	+helper :http_get do |url|
497	+ parsed_url = URI.parse(url)
498	+ http = Net::HTTP.new(parsed_url.host, parsed_url.port)
499	+ http.use_ssl = parsed_url.scheme == 'https'
500	+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
501	+ http.get(parsed_url.request_uri)
502	+end
503	+
504	+helper :url_encode do |str|
505	+ str.to_s.gsub(/[^a-zA-Z0-9_\.\-]/n) {|s| sprintf('%%%02x', s[0])}
506	+end
507	+
508	+helper :http_post do |url,params|
509	+ parsed_url = URI.parse(url)
510	+ http = Net::HTTP.new(parsed_url.host, parsed_url.port)
511	+ http.use_ssl = parsed_url.scheme == 'https'
512	+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
513	+
514	+ req = Net::HTTP::Post.new(parsed_url.path)
515	+ req.body = params.map {|k,v| "#{url_encode(k)}=#{url_encode(v)}" }.join('&')
516	+ req.content_type = 'application/x-www-form-urlencoded'
517	+ http.request req
518	+end
519	+
520	+helper :prepare_param do |name, value|
521	+ "Content-Disposition: form-data; name=\"#{url_encode(name)}\"\r\n\r\n#{value}\r\n"
522	+end
523	+
524	+helper :http_post_multipart do |url, params|
525	+ parsed_url = URI.parse(url)
526	+ http = Net::HTTP.new(parsed_url.host, parsed_url.port)
527	+ http.use_ssl = parsed_url.scheme == 'https'
528	+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
529	+
530	+ boundary = "#{rand(1000000)}boundaryofdoomydoom#{rand(1000000)}"
531	+
532	+ fp = []
533	+ files = []
534	+
535	+ params.each do |k,v|
536	+ if v.respond_to?(:path) and v.respond_to?(:read) then
537	+ filename = v.path
538	+ content = v.read
539	+ mime_type = MIME::Types.type_for(filename)[0] || MIME::Types["application/octet-stream"][0]
540	+ fp.push(prepare_param("Content-Type", mime_type.simplified))
541	+ files.push("Content-Disposition: form-data; name=\"#{url_encode(k.to_s)}\"; filename=\"#{ filename }\"\r\nContent-Type: #{ mime_type.simplified }\r\n\r\n#{ content }\r\n")
542	+ else
543	+ fp.push(prepare_param(k,v))
544	+ end
545	+ end
546	+
547	+ http.post(parsed_url.path, "--#{boundary}\r\n" + (fp + files).join("--#{boundary}\r\n") + "--#{boundary}--", {
548	+ "Content-Type" => "multipart/form-data; boundary=#{boundary}",
549	+ "User-Agent" => "Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en-us) AppleWebKit/523.10.6 (KHTML, like Gecko) Version/3.0.4 Safari/523.10.6"
550	+ })
551	+end