Authenticating Users with a Sinatra API, Page 2
$ gem install sinatra-jsonp
Once installed, revise
app.rb to look like this:
require 'sinatra' require "sinatra/jsonp" museums = [ "The Louvre", "The Butler Institute", "Doria Pamphilj" ] get '/museums' do JSONP museums end
["The Louvre","The Butler Institute","Doria Pamphilj"]
Incidentally, repeatedly restarting your Sinatra application quickly becomes tedious. Check out the shotgun gem, which will automatically restart your app every time it detects changes to the script.
Authenticating Users via an API Key
Most APIs require users to supply a valid API key with each request, which is subsequently checked against a database in order to ensure the requesting client is allowed to communicate with the API. While Sinatra doesn't offer any native authentication capabilities, it's pretty easy to incorporate such a feature into your application. Such a solution is explained in this Stack Overflow post.
Are you doing anything interesting with Sinatra? So far I'm having a blast, and would love to hear about how you're getting along with it in the comments!
About the Author
Jason Gilmore -- Contributing Editor, PHP -- is the founder of EasyPHPWebsites.com, and author of the popular book, "Easy PHP Websites with the Zend Framework". Jason is a cofounder and speaker chair of CodeMash, a nonprofit organization tasked with hosting an annual namesake developer's conference, and was a member of the 2008 MySQL Conference speaker selection board.
Originally published on http://www.developer.com.
Page 2 of 2