Skip to content

Instantly share code, notes, and snippets.

@cthornton
Created July 3, 2014 03:49
Show Gist options
  • Save cthornton/10378c63ef13a6dc2bb2 to your computer and use it in GitHub Desktop.
Save cthornton/10378c63ef13a6dc2bb2 to your computer and use it in GitHub Desktop.
Dry your JSON API - What it probably looks like
class PostsController < ActionController::Base
def index
@user = User.find_by(api_key: request.authorization)
if !@user
return render json: {error: 'invalid api key'}, status: :unauthorized
end
@posts = @user.posts
# ... render JSON
end
def show
@user = User.find_by(api_key: request.authorization)
# ... validate user
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment