Commit c218a18fb4e27e2171a1e63f7d307e457a997b8c

Authored by Leandro Santos
1 parent 7361f343

adding level enpoint

lib/gamification_plugin/api.rb
... ... @@ -6,6 +6,10 @@ class GamificationPlugin::API < Grape::API
6 6 get 'badges' do
7 7 present current_person.badges
8 8 end
  9 + get 'level' do
  10 + {:level => current_person.level, :percent => current_person.gamification_plugin_level_percent}
  11 + end
  12 +
9 13 end
10 14  
11 15 resource :people do
... ... @@ -15,6 +19,12 @@ class GamificationPlugin::API < Grape::API
15 19 present person.badges
16 20 end
17 21  
  22 + get ':id/level' do
  23 + person = environment.people.visible_for_person(current_person).find_by_id(params[:id])
  24 + return not_found! if person.blank?
  25 + {:level => person.level, :percent => person.gamification_plugin_level_percent}
  26 + end
  27 +
18 28 end
19 29 end
20 30 end
... ...
test/unit/api_test.rb
... ... @@ -9,7 +9,7 @@ class APITest < ActiveSupport::TestCase
9 9 environment.enable_plugin(GamificationPlugin)
10 10 end
11 11  
12   - should 'get badges my own badges' do
  12 + should 'get my own badges' do
13 13 badge = GamificationPlugin::Badge.create!(:owner => environment, :name => 'test_badge')
14 14 person.add_badge(badge.id)
15 15 get "/api/v1/gamification_plugin/my/badges?#{params.to_query}"
... ... @@ -17,6 +17,15 @@ class APITest < ActiveSupport::TestCase
17 17 assert_equal 'test_badge', json.first['name']
18 18 end
19 19  
  20 + should 'get my level' do
  21 + badge = GamificationPlugin::Badge.create!(:owner => environment, :name => 'test_badge')
  22 + person.add_badge(badge.id)
  23 + get "/api/v1/gamification_plugin/my/level?#{params.to_query}"
  24 + json = JSON.parse(last_response.body)
  25 + assert_not_nil json['level']
  26 + assert_not_nil json['percent']
  27 + end
  28 +
20 29 should 'get badges of the public person' do
21 30 badge = GamificationPlugin::Badge.create!(:owner => environment, :name => 'test_badge')
22 31 another_person = create(User, :environment => environment).person
... ... @@ -28,6 +37,16 @@ class APITest < ActiveSupport::TestCase
28 37 assert_equal 'test_badge', json.first['name']
29 38 end
30 39  
  40 + should 'get level of the public person' do
  41 + another_person = create(User, :environment => environment).person
  42 + another_person.visible=true
  43 + another_person.save
  44 + get "/api/v1/gamification_plugin/people/#{another_person.id}/level?#{params.to_query}"
  45 + json = JSON.parse(last_response.body)
  46 + assert_not_nil json['level']
  47 + assert_not_nil json['percent']
  48 + end
  49 +
31 50 should 'not get badges of the private person' do
32 51 badge = GamificationPlugin::Badge.create!(:owner => environment, :name => 'test_badge')
33 52 another_person = create(User, :environment_id => environment.id).person
... ... @@ -39,4 +58,13 @@ class APITest < ActiveSupport::TestCase
39 58 assert_equal 404, last_response.status
40 59 end
41 60  
  61 + should 'not get level of the private person' do
  62 + another_person = create(User, :environment_id => environment.id).person
  63 + another_person.visible=false
  64 + another_person.save
  65 + get "/api/v1/gamification_plugin/people/#{another_person.id}/level?#{params.to_query}"
  66 + json = JSON.parse(last_response.body)
  67 + assert_equal 404, last_response.status
  68 + end
  69 +
42 70 end
... ...