Created
July 7, 2018 00:28
-
-
Save agush22/40c56b45be382fb15d998a24f25d84b3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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