Last active
July 24, 2016 20:38
-
-
Save francisluong/cd4427383f5fbe8925bd4706d2217568 to your computer and use it in GitHub Desktop.
EM-HTTP-Request - Example 000 - Single Request
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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