Skip to content

Instantly share code, notes, and snippets.

@godfat
Created June 10, 2011 09:10
Show Gist options
  • Select an option

  • Save godfat/1018526 to your computer and use it in GitHub Desktop.

Select an option

Save godfat/1018526 to your computer and use it in GitHub Desktop.
# 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
@dougal
Copy link
Copy Markdown

dougal commented Jun 17, 2011

Thanks for this!

Took me a few reads through to realise this was for em-synchrony rather than plain eventmachine.

@godfat
Copy link
Copy Markdown
Author

godfat commented Jun 20, 2011 via email

@dougal
Copy link
Copy Markdown

dougal commented Jun 20, 2011

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