Skip to content

Instantly share code, notes, and snippets.

@guiocavalcanti
Forked from fltiago/api.rb
Created October 8, 2012 18:18
Show Gist options
  • Select an option

  • Save guiocavalcanti/3854022 to your computer and use it in GitHub Desktop.

Select an option

Save guiocavalcanti/3854022 to your computer and use it in GitHub Desktop.

Revisions

  1. guiocavalcanti revised this gist Oct 8, 2012. 1 changed file with 0 additions and 3 deletions.
    3 changes: 0 additions & 3 deletions api.rb
    Original file line number Diff line number Diff line change
    @@ -10,9 +10,6 @@ def permit
    end

    resource :walls do
    params do
    requires :token, type: String, desc: "Your api token."
    end
    get '/' do
    permit.able_to?(:read, "something")
    end
  2. guiocavalcanti revised this gist Oct 8, 2012. 1 changed file with 3 additions and 12 deletions.
    15 changes: 3 additions & 12 deletions api.rb
    Original file line number Diff line number Diff line change
    @@ -4,26 +4,17 @@ class Wally < Grape::API
    format :json

    helpers do
    def current_user
    @current_user ||= Author.find_by(token: params[:token])
    end

    def authorize!(action)
    error!('401 Unauthorized', 401) unless current_user and permit.able_to?(:read, action)
    end

    def permit
    @permit ||= Permit::Mechanism.new(:subject_id => current_user.subject_permit, :service_name => "wally")
    @permit ||= Permit::Mechanism.new
    end
    end

    resource :walls do
    params do
    requires :token, type: String, desc: "Your api token."
    end
    get ':resource_id' do
    authorize!(params[:resource_id])
    ...
    get '/' do
    permit.able_to?(:read, "something")
    end
    end
    end
  3. @fltiago fltiago created this gist Oct 8, 2012.
    29 changes: 29 additions & 0 deletions api.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    require "debugger"

    class Wally < Grape::API
    format :json

    helpers do
    def current_user
    @current_user ||= Author.find_by(token: params[:token])
    end

    def authorize!(action)
    error!('401 Unauthorized', 401) unless current_user and permit.able_to?(:read, action)
    end

    def permit
    @permit ||= Permit::Mechanism.new(:subject_id => current_user.subject_permit, :service_name => "wally")
    end
    end

    resource :walls do
    params do
    requires :token, type: String, desc: "Your api token."
    end
    get ':resource_id' do
    authorize!(params[:resource_id])
    ...
    end
    end
    end