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
Restart your Sinatra application and reload the webpage. You'll see the following output:
["The Louvre","The Butler Institute","Doria Pamphilj"]
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.