Sha256: 3aaba5003b7930b73d6d4053e294e2d89f8c698db9a9732cee33ead4c1140472

Contents?: true

Size: 805 Bytes

Versions: 4

Compression:

Stored size: 805 Bytes

Contents

#!/usr/bin/env ruby

# Rewrites and proxies requests to a third-party API, with HTTP basic authentication.

$:<< '../lib' << 'lib'

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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
goliath-1.0.7 examples/api_proxy.rb
goliath-1.0.6 examples/api_proxy.rb
goliath-1.0.5 examples/api_proxy.rb
goliath-1.0.4 examples/api_proxy.rb