Skip to content

Instantly share code, notes, and snippets.

@agush22
Created July 7, 2018 00:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save agush22/40c56b45be382fb15d998a24f25d84b3 to your computer and use it in GitHub Desktop.
Save agush22/40c56b45be382fb15d998a24f25d84b3 to your computer and use it in GitHub Desktop.
require 'sinatra'
require 'json'
require 'securerandom'
helpers do
def protected!
return if authorized?
headers['WWW-Authenticate'] = 'Basic realm="Restricted Area"'
halt 401, "Not authorized"
end
def authorized?
@auth ||= Rack::Auth::Basic::Request.new(request.env)
@auth.provided? and @auth.basic? and @auth.credentials and @auth.credentials == ['billing', 'gateway']
end
end
get '/' do
'Hello'
end
get '/validate' do
protected!
send(%i(response_ok response_fail response_503 response_timeout).sample)
end
def response_ok
content_type :json
JSON.dump(
id: SecureRandom.hex(8),
paid: true,
failure_message: nil
)
end
def response_fail
content_type :json
JSON.dump(
id: SecureRandom.hex(8),
paid: false,
failure_message: 'insufficient_funds'
)
end
def response_503
halt 503, 'Service Unavailable'
end
def response_timeout
sleep 15
halt 503, 'Service Unavailable'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment