Commit ac400b4a06ab814334509148fe4d00df2bf4469f

Authored by Hugo Melo
1 parent 004dd02a

Add first points endpoints and simple test

lib/gamification_plugin/api.rb
@@ -6,7 +6,7 @@ class GamificationPlugin::API < Grape::API @@ -6,7 +6,7 @@ class GamificationPlugin::API < Grape::API
6 environment.gamification_plugin_badges.group(:name).count 6 environment.gamification_plugin_badges.group(:name).count
7 end 7 end
8 8
9 - resource :my do 9 + resource :my do
10 get 'badges' do 10 get 'badges' do
11 authenticate! 11 authenticate!
12 present current_person.badges, :with => Noosfero::API::Entities::Badge 12 present current_person.badges, :with => Noosfero::API::Entities::Badge
@@ -16,6 +16,11 @@ class GamificationPlugin::API < Grape::API @@ -16,6 +16,11 @@ class GamificationPlugin::API < Grape::API
16 authenticate! 16 authenticate!
17 {:level => current_person.level, :percent => current_person.gamification_plugin_level_percent, :score => current_person.points} 17 {:level => current_person.level, :percent => current_person.gamification_plugin_level_percent, :score => current_person.points}
18 end 18 end
  19 +
  20 + get 'points' do
  21 + authenticate!
  22 + {points: current_person.points}
  23 + end
19 end 24 end
20 25
21 resource :people do 26 resource :people do
@@ -25,6 +30,12 @@ class GamificationPlugin::API < Grape::API @@ -25,6 +30,12 @@ class GamificationPlugin::API < Grape::API
25 present person.badges 30 present person.badges
26 end 31 end
27 32
  33 + get ':id/points' do
  34 + person = environment.people.visible_for_person(current_person).find_by_id(params[:id])
  35 + return not_found! if person.blank?
  36 + {:points => person.points}
  37 + end
  38 +
28 get ':id/level' do 39 get ':id/level' do
29 person = environment.people.visible_for_person(current_person).find_by_id(params[:id]) 40 person = environment.people.visible_for_person(current_person).find_by_id(params[:id])
30 return not_found! if person.blank? 41 return not_found! if person.blank?
lib/merit/point_rules.rb
@@ -3,90 +3,100 @@ module Merit @@ -3,90 +3,100 @@ module Merit
3 include Merit::PointRulesMethods 3 include Merit::PointRulesMethods
4 4
5 AVAILABLE_RULES = { 5 AVAILABLE_RULES = {
6 - :comment_author => {  
7 - :action => 'comment#create',  
8 - :undo_action => 'comment#destroy',  
9 - :to => :author,  
10 - :value => 1,  
11 - :description => _('Comment author'),  
12 - :default_weight => 150 6 + comment_author: {
  7 + action: 'comment#create',
  8 + undo_action: 'comment#destroy',
  9 + to: :author,
  10 + value: 1,
  11 + description: _('Comment author'),
  12 + default_weight: 150
13 }, 13 },
14 - :comment_article_author => {  
15 - :action => 'comment#create',  
16 - :undo_action => 'comment#destroy',  
17 - :to => lambda {|comment| comment.source.author},  
18 - :value => 1,  
19 - :description => _('Article author of a comment'),  
20 - :default_weight => 50 14 + comment_article_author: {
  15 + action: 'comment#create',
  16 + undo_action: 'comment#destroy',
  17 + to: lambda {|comment| comment.source.author},
  18 + value: 1,
  19 + description: _('Article author of a comment'),
  20 + default_weight: 50
21 }, 21 },
22 - :comment_article => {  
23 - :action => 'comment#create',  
24 - :undo_action => 'comment#destroy',  
25 - :to => lambda {|comment| comment.source},  
26 - :value => 1,  
27 - :description => _('Source article of a comment'),  
28 - :default_weight => 50 22 + comment_article: {
  23 + action: 'comment#create',
  24 + undo_action: 'comment#destroy',
  25 + to: lambda {|comment| comment.source},
  26 + value: 1,
  27 + description: _('Source article of a comment'),
  28 + default_weight: 50
29 }, 29 },
30 - :comment_community => {  
31 - :action => 'comment#create',  
32 - :undo_action => 'comment#destroy',  
33 - :to => lambda {|comment| comment.profile},  
34 - :value => 1,  
35 - :description => _('Article community of a comment'),  
36 - :default_weight => 50,  
37 - :condition => lambda {|target| target.profile.community? } 30 + comment_community: {
  31 + action: 'comment#create',
  32 + undo_action: 'comment#destroy',
  33 + to: lambda {|comment| comment.profile},
  34 + value: 1,
  35 + description: _('Article community of a comment'),
  36 + default_weight: 50,
  37 + condition: lambda {|target| target.profile.community? }
38 }, 38 },
39 - :article_author => {  
40 - :action => 'article#create',  
41 - :undo_action => 'article#destroy',  
42 - :to => :author,  
43 - :value => 1,  
44 - :description => _('Article author'),  
45 - :default_weight => 500 39 + article_author: {
  40 + action: 'article#create',
  41 + undo_action: 'article#destroy',
  42 + to: :author,
  43 + value: 1,
  44 + description: _('Article author'),
  45 + default_weight: 500
46 }, 46 },
47 - :article_community => {  
48 - :action => 'article#create',  
49 - :undo_action => 'article#destroy',  
50 - :to => :profile,  
51 - :value => 1,  
52 - :description => _('Article community'),  
53 - :default_weight => 600,  
54 - :condition => lambda {|target| target.profile.community? } 47 + article_community: {
  48 + action: 'article#create',
  49 + undo_action: 'article#destroy',
  50 + to: :profile,
  51 + value: 1,
  52 + description: _('Article community'),
  53 + default_weight: 600,
  54 + condition: lambda {|target| target.profile.community? }
55 }, 55 },
56 - :vote_voteable_author => {  
57 - :action => 'vote#create',  
58 - :undo_action => 'vote#destroy',  
59 - :to => lambda {|vote| vote.voteable.author},  
60 - :profile => lambda {|vote| vote.voteable.profile},  
61 - :value => lambda {|vote| vote.vote},  
62 - :description => _('Author of a voted content'),  
63 - :default_weight => 50, 56 + vote_voteable_author: {
  57 + action: 'vote#create',
  58 + undo_action: 'vote#destroy',
  59 + to: lambda {|vote| vote.voteable.author},
  60 + profile: lambda {|vote| vote.voteable.profile},
  61 + value: lambda {|vote| vote.vote},
  62 + description: _('Author of a voted content'),
  63 + default_weight: 50,
64 }, 64 },
65 - :vote_voteable => {  
66 - :action => 'vote#create',  
67 - :undo_action => 'vote#destroy',  
68 - :to => lambda {|vote| vote.voteable},  
69 - :profile => lambda {|vote| vote.voteable.profile},  
70 - :value => lambda {|vote| vote.vote},  
71 - :description => _('Voted content'),  
72 - :default_weight => 50 65 + vote_voteable: {
  66 + action: 'vote#create',
  67 + undo_action: 'vote#destroy',
  68 + to: lambda {|vote| vote.voteable},
  69 + profile: lambda {|vote| vote.voteable.profile},
  70 + value: lambda {|vote| vote.vote},
  71 + description: _('Voted content'),
  72 + default_weight: 50
73 }, 73 },
74 - :vote_voter => {  
75 - :action => 'vote#create',  
76 - :undo_action => 'vote#destroy',  
77 - :to => lambda {|vote| vote.voter},  
78 - :value => lambda {|vote| 1},  
79 - :description => _('Voter'),  
80 - :default_weight => 10 74 + vote_voter: {
  75 + action: 'vote#create',
  76 + undo_action: 'vote#destroy',
  77 + to: lambda {|vote| vote.voter},
  78 + value: lambda {|vote| 1},
  79 + description: _('Voter'),
  80 + default_weight: 10
81 }, 81 },
82 - :friends => {  
83 - :action => 'friendship#create',  
84 - :undo_action => 'friendship#destroy',  
85 - :to => lambda {|friendship| friendship.person},  
86 - :value => 1,  
87 - :description => _('Friends'),  
88 - :default_weight => 5 82 + friends: {
  83 + action: 'friendship#create',
  84 + undo_action: 'friendship#destroy',
  85 + to: lambda {|friendship| friendship.person},
  86 + value: 1,
  87 + description: _('Friends'),
  88 + default_weight: 5
89 }, 89 },
  90 + profile_completion: {
  91 + action: ['account#create', 'account#update'],
  92 + undo_action: 'account#destroy',
  93 + to: lambda {|user| user.person},
  94 + value: 1,
  95 + description: _('Profile Completion'),
  96 + default_weight: 5,
  97 + model_name: "User",
  98 + condition: lambda {|user| user.person.profile_completion_score_condition }
  99 + }
90 } 100 }
91 101
92 def weight(category) 102 def weight(category)
@@ -110,7 +120,7 @@ module Merit @@ -110,7 +120,7 @@ module Merit
110 120
111 AVAILABLE_RULES.each do |category, setting| 121 AVAILABLE_RULES.each do |category, setting|
112 [setting[:action], setting[:undo_action]].compact.zip([1, -1]).each do |action, signal| 122 [setting[:action], setting[:undo_action]].compact.zip([1, -1]).each do |action, signal|
113 - score lambda {|target| signal * calculate_score(target, category, setting[:value])}, :on => action, :to => setting[:to], :category => category do |target| 123 + score lambda {|target| signal * calculate_score(target, category, setting[:value])}, on: action, to: setting[:to], category: category do |target|
114 condition(setting, target) 124 condition(setting, target)
115 end 125 end
116 end 126 end