forked from postrank-labs/goliath
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_proxy.rb
More file actions
24 lines (17 loc) · 781 Bytes
/
api_proxy.rb
File metadata and controls
24 lines (17 loc) · 781 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/usr/bin/env ruby
# Rewrites and proxies requests to a third-party API, with HTTP basic authentication.
require 'goliath'
require 'em-synchrony/em-http'
class Twilio < 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.debug "Received #{http.response_header.status} from Twilio"
[200, {'X-Goliath' => 'Proxy','Content-Type' => 'application/javascript'}, http.response]
end
end