public
Created

em-http-request streaming upload

  • Download Gist
em_http.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
#
# requires:
# a) run https://gist.github.com/2022231 on localhost:3001
# b) provide a streamed.jpg at script location
#
# result:
# this fails for em-http-request v1.0.1
#
 
require "rubygems"
require "em-synchrony"
require "em-http"
 
require "launchy"
 
@target_host = "localhost"
@target_port = "3001"
@target_path = "/"
@source_image_name = "streamed.jpg"
@result_html_name = "result_em_http.html"
 
def script_data_path(filename)
File.join(File.expand_path(File.dirname(__FILE__)), filename)
end
 
EM.run do
 
file_path = script_data_path(@source_image_name)
 
puts "Uploading #{File.size(file_path)} bytes..."
 
# Content-Length is calculated internally and cannot be overridden
request_headers = {"Content-Type" => "image/jpeg", "Accept" => "*/*"}
 
http = EventMachine::HttpRequest.new("http://#{@target_host}:#{@target_port}").post(
:path => @target_path, :head => request_headers, :file => file_path)
 
http.callback do |a_http|
puts "DONE: #{a_http.response_header.status}\n"+
"BODY: #{a_http.response.size} bytes\n"+
"See #{@result_html_name}"
 
result_html_path = script_data_path(@result_html_name)
 
File.open(result_html_path, "w") do |f|
f.puts(a_http.response)
end
 
Launchy.open("file://#{result_html_path}")
EM.stop
end
 
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.