Created
June 10, 2011 09:10
-
-
Save godfat/1018526 to your computer and use it in GitHub Desktop.
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
# monkey patch heroku to replace rest-client with em-http-request | |
class Heroku::Client | |
def process(method, uri, extra_headers={}, payload=nil) | |
headers = heroku_headers.merge(extra_headers) | |
args = [method, payload, headers].compact | |
# begin HACK | |
# response = resource(uri).send(*args) | |
basic_auth = {'Authorization' => 'Basic ' + | |
["#{user}:#{password}"].pack('m').delete("\r\n")} | |
if uri =~ /^https?/ | |
http = EM::HttpRequest.new(uri) | |
else | |
http = EM::HttpRequest.new("https://api.#{host}#{uri}") | |
end | |
response = http.send(method, | |
:head => {'Accept' => '*/*; q=0.5, application/xml', | |
'Accept-Encoding' => 'gzip, deflate'}. | |
merge(headers.merge(basic_auth)), | |
:body => payload) | |
if status = response && response.response_header.status | |
unless response.error.strip.empty? && status == 200 | |
klass = RestClient::Exceptions::EXCEPTIONS_MAP[status] | |
if klass | |
raise klass.new | |
else | |
raise RestClient::Exception | |
end | |
end | |
end | |
class << response | |
alias_method :headers, :response_header | |
alias_method :to_s, :response | |
end | |
# end HACK | |
extract_warning(response) | |
response | |
end | |
end |
Ohhh, I'm sorry about that. I should mention this in the gist. Here's
my first blog post about this patch, if you want to know some
motivation behind this:
http://blogger.godfat.org/2010/09/asynchronized-heroku-scaler-in-ruby-19.html
A not very related sequel:
http://blogger.godfat.org/2011/03/asynchronized-heroku-scaler-in-ruby-19.html
Thanks for those, good reading. Nice to know it wasn't me doing something wrong with needing to wrap calling code in a Fiber to avoid "can't yield from root fiber" errors.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for this!
Took me a few reads through to realise this was for em-synchrony rather than plain eventmachine.