Skip to content

Instantly share code, notes, and snippets.

@gerhard
Forked from subelsky/twilio_goliath.rb
Created September 15, 2011 23:01
Show Gist options
  • Save gerhard/1220732 to your computer and use it in GitHub Desktop.
Save gerhard/1220732 to your computer and use it in GitHub Desktop.
Using Goliath as an API Proxy
require 'goliath'
require 'em-synchrony/em-http'
class TwilioResponse < Goliath::API
use Goliath::Rack::Params
use Goliath::Rack::JSONP
HEADERS = { authorization: ENV.values_at("TWILIO_SID","TWILIO_AUTH_TOKEN") }
BASE_URL = "https://api.twilio.com/2010-04-01/Accounts/#{ENV['TWILIO_SID']}/AvailablePhoneNumbers/US"
def response(env)
url = "#{BASE_URL}#{env['REQUEST_PATH']}?#{env['QUERY_STRING']}"
logger.debug "Proxying #{url}"
http = EM::HttpRequest.new(url).get head: HEADERS
logger.info "Received #{http.response_header.status} from Twilio"
[200, {'X-Goliath' => 'Proxy','Content-Type' => 'application/javascript'}, http.response]
end
end
class Twilio < Goliath::API
get %r{^/(Local|TollFree)}, TwilioResponse
end
@rb2k
Copy link

rb2k commented Dec 9, 2011

Just in case people find this using google. This is the matching blog post: http://www.subelsky.com/2011/09/using-goliath-as-api-proxy.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment