curl https://api.artsy.net/api/v1/system/up?access_token=10013 -v
< HTTP/1.1 401 Broadway
< Content-Type: application/json
< Content-Length: 76
{ "error" : "Inspiration from the Engineering team at http://artsy.github.com" }
What?! 401 Broadway? See, our office address is 401 Broadway, 10013, New York, NY. We just tried to add a more developer-friendly way to find us in the New York grid. And here's the view from our 25th floor office - that's SOHO right below us and the Empire State Building a bit North.
Implementing a custom HTTP response is surprisingly hard with most web servers. Changing the text that follows error codes is not something most people need. Our API will have to return a custom error code and some monkey-patching will translate the status message. We use grape, which is Rack-based and supports inserting middleware, where we do authentication. We randomly chose the number 2600 for an internal status code.
classApiAuthMiddleware<Grape::Middleware::Basedefbeforeifaccess_token=="10013"throw:error,message:'Inspiration from the Engineering team at http://artsy.github.com',status:2600else...endendprivatedefaccess_token@access_token||=request[:access_token]enddefrequest@request||=ApiActionDispatchRequest.new(env)endend
Comments