Created
March 1, 2012 14:48
-
-
Save jaimeiniesta/1950241 to your computer and use it in GitHub Desktop.
Grape API example
This file contains hidden or 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
| # This example API will recognize users that have authenticated via a web form, | |
| # but it fails authenticating from http_basic_auth, which has been enabled in Devise for this app | |
| # curl -v -u "[email protected]:herpassword" http://localhost:3000/api/my_email | |
| module MyApp | |
| class API < Grape::API | |
| prefix 'api' | |
| helpers do | |
| def warden | |
| env['warden'] | |
| end | |
| def current_user | |
| warden.user | |
| end | |
| def authenticate! | |
| error!('401 Unauthorized', 401) unless current_user | |
| end | |
| end | |
| get 'hello' do | |
| {:hello => 'world'} | |
| end | |
| get 'my_email' do | |
| authenticate! | |
| current_user.email | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment