Skip to content

Instantly share code, notes, and snippets.

@francisluong
Last active July 24, 2016 20:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save francisluong/cd4427383f5fbe8925bd4706d2217568 to your computer and use it in GitHub Desktop.
Save francisluong/cd4427383f5fbe8925bd4706d2217568 to your computer and use it in GitHub Desktop.
EM-HTTP-Request - Example 000 - Single Request
require 'eventmachine'
require 'em-http-request'
require 'logger'
require 'pry'
##
# Single request
@start_time = Time.now
@logger = Logger.new($stdout)
http = nil
status_code = nil
def runtime
Time.now - @start_time
end
EventMachine.run {
url = 'http://ipv4.download.thinkbroadband.com/5MB.zip'
# Create HTTP Request and issue get, which returns an HTTPConnection
http = EventMachine::HttpRequest.new(url).get
@logger.debug("[#{__method__}] [url=#{url}] [SUBMITTED] [runtime=#{runtime}]")
# setup callbacks and errbacks to deal with normal and errored completion
http.callback do
status_code = http.response_header.status
@logger.debug("[#{__method__}] [url=#{url}] [CALLBACK #{status_code}] [runtime=#{runtime}]")
p http.response_header
EM.stop
end
http.errback do
status_code = http.response_header.status
@logger.debug("[#{__method__}] [url=#{url}] [ERRBACK #{status_code}] [runtime=#{runtime}]")
p http.response_header
EM.stop
end
}
# lauch Pry in case we want to do any REPL-y things
binding.pry
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment