Sha256: bd53bbc4f61d9db8c7fd0969b5ba1ba57d5b457da9b591553f88abc798f1a1d1

Contents?: true

Size: 781 Bytes

Versions: 5

Compression:

Stored size: 781 Bytes

Contents

#!/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

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
goliath-1.0.3 examples/api_proxy.rb
goliath-1.0.2 examples/api_proxy.rb
goliath-1.0.1 examples/api_proxy.rb
goliath-1.0.0 examples/api_proxy.rb
goliath-1.0.0.beta.1 examples/api_proxy.rb