From c218a18fb4e27e2171a1e63f7d307e457a997b8c Mon Sep 17 00:00:00 2001 From: Leandro Nunes dos Santos Date: Mon, 31 Aug 2015 19:42:25 -0300 Subject: [PATCH] adding level enpoint --- lib/gamification_plugin/api.rb | 10 ++++++++++ test/unit/api_test.rb | 30 +++++++++++++++++++++++++++++- 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/lib/gamification_plugin/api.rb b/lib/gamification_plugin/api.rb index db37378..1a36c62 100644 --- a/lib/gamification_plugin/api.rb +++ b/lib/gamification_plugin/api.rb @@ -6,6 +6,10 @@ class GamificationPlugin::API < Grape::API get 'badges' do present current_person.badges end + get 'level' do + {:level => current_person.level, :percent => current_person.gamification_plugin_level_percent} + end + end resource :people do @@ -15,6 +19,12 @@ class GamificationPlugin::API < Grape::API present person.badges end + get ':id/level' do + person = environment.people.visible_for_person(current_person).find_by_id(params[:id]) + return not_found! if person.blank? + {:level => person.level, :percent => person.gamification_plugin_level_percent} + end + end end end diff --git a/test/unit/api_test.rb b/test/unit/api_test.rb index 48f50e4..381abbd 100644 --- a/test/unit/api_test.rb +++ b/test/unit/api_test.rb @@ -9,7 +9,7 @@ class APITest < ActiveSupport::TestCase environment.enable_plugin(GamificationPlugin) end - should 'get badges my own badges' do + should 'get my own badges' do badge = GamificationPlugin::Badge.create!(:owner => environment, :name => 'test_badge') person.add_badge(badge.id) get "/api/v1/gamification_plugin/my/badges?#{params.to_query}" @@ -17,6 +17,15 @@ class APITest < ActiveSupport::TestCase assert_equal 'test_badge', json.first['name'] end + should 'get my level' do + badge = GamificationPlugin::Badge.create!(:owner => environment, :name => 'test_badge') + person.add_badge(badge.id) + get "/api/v1/gamification_plugin/my/level?#{params.to_query}" + json = JSON.parse(last_response.body) + assert_not_nil json['level'] + assert_not_nil json['percent'] + end + should 'get badges of the public person' do badge = GamificationPlugin::Badge.create!(:owner => environment, :name => 'test_badge') another_person = create(User, :environment => environment).person @@ -28,6 +37,16 @@ class APITest < ActiveSupport::TestCase assert_equal 'test_badge', json.first['name'] end + should 'get level of the public person' do + another_person = create(User, :environment => environment).person + another_person.visible=true + another_person.save + get "/api/v1/gamification_plugin/people/#{another_person.id}/level?#{params.to_query}" + json = JSON.parse(last_response.body) + assert_not_nil json['level'] + assert_not_nil json['percent'] + end + should 'not get badges of the private person' do badge = GamificationPlugin::Badge.create!(:owner => environment, :name => 'test_badge') another_person = create(User, :environment_id => environment.id).person @@ -39,4 +58,13 @@ class APITest < ActiveSupport::TestCase assert_equal 404, last_response.status end + should 'not get level of the private person' do + another_person = create(User, :environment_id => environment.id).person + another_person.visible=false + another_person.save + get "/api/v1/gamification_plugin/people/#{another_person.id}/level?#{params.to_query}" + json = JSON.parse(last_response.body) + assert_equal 404, last_response.status + end + end -- libgit2 0.21.2