Module: Recognition::Database
- Defined in:
- lib/recognition/database.rb
Overview
Handle all Transactions and logging to Redis
Class Method Summary collapse
- .get(key) ⇒ Object
- .get_user_counter(id, counter) ⇒ Object
- .get_user_points(id) ⇒ Object
- .get_user_transactions(id, page = 0, per = 20) ⇒ Object
- .get_user_voucher(id, code) ⇒ Object
- .get_voucher_transactions(code, page = 0, per = 20) ⇒ Object
- .log(id, amount, bucket, code = nil) ⇒ Object
- .redeem_voucher(id, code, amount) ⇒ Object
- .update_points(object, action, condition) ⇒ Object
Class Method Details
.get(key) ⇒ Object
19 20 21 |
# File 'lib/recognition/database.rb', line 19 def self.get key Recognition.backend.get key end |
.get_user_counter(id, counter) ⇒ Object
27 28 29 30 |
# File 'lib/recognition/database.rb', line 27 def self.get_user_counter id, counter counter = Recognition.backend.hget("recognition:user:#{ id }:counters", counter) counter.to_i end |
.get_user_points(id) ⇒ Object
23 24 25 |
# File 'lib/recognition/database.rb', line 23 def self.get_user_points id get_user_counter id, 'points' end |
.get_user_transactions(id, page = 0, per = 20) ⇒ Object
32 33 34 35 36 37 |
# File 'lib/recognition/database.rb', line 32 def self.get_user_transactions id, page = 0, per = 20 start = page * per stop = (1 + page) * per keypart = "user:#{ id }" self.get_transactions keypart, start, stop end |
.get_user_voucher(id, code) ⇒ Object
69 70 71 72 |
# File 'lib/recognition/database.rb', line 69 def self.get_user_voucher id, code bucket = "Voucher:redeem##{ code }" Database.get_user_counter id, bucket end |
.get_voucher_transactions(code, page = 0, per = 20) ⇒ Object
39 40 41 42 43 44 |
# File 'lib/recognition/database.rb', line 39 def self.get_voucher_transactions code, page = 0, per = 20 start = page * per stop = (1 + page) * per keypart = "voucher:#{ code }" self.get_transactions keypart, start, stop end |
.log(id, amount, bucket, code = nil) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/recognition/database.rb', line 6 def self.log id, amount, bucket, code = nil hash = Time.now.to_f.to_s Recognition.backend.multi do Recognition.backend.hincrby "recognition:user:#{ id }:counters", 'points', amount Recognition.backend.hincrby "recognition:user:#{ id }:counters", bucket, amount Recognition.backend.zadd "recognition:user:#{ id }:transactions", hash, { hash: hash, amount: amount, bucket: bucket, datetime: DateTime.now.to_s }.to_json Recognition.backend.zadd 'recognition:transactions', hash, { hash: hash, id: id, amount: amount, bucket: bucket, datetime: DateTime.now.to_s }.to_json unless code.nil? Recognition.backend.zadd "recognition:voucher:#{ code }:transactions", hash, { hash: hash, id: id, bucket: bucket, datetime: DateTime.now.to_s }.to_json end end end |
.redeem_voucher(id, code, amount) ⇒ Object
64 65 66 67 |
# File 'lib/recognition/database.rb', line 64 def self.redeem_voucher id, code, amount bucket = "Voucher:redeem##{ code }" Database.log(id, amount.to_i, bucket, code) end |
.update_points(object, action, condition) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/recognition/database.rb', line 46 def self.update_points object, action, condition if condition[:bucket].nil? bucket = "#{ object.class.to_s.camelize }:#{ action }" else bucket = condition[:bucket] end user = parse_user(object, condition) if condition[:amount].nil? && condition[:gain].nil? && condition[:loss].nil? false else total = parse_amount(condition[:amount], object) + parse_amount(condition[:gain], object) - parse_amount(condition[:loss], object) ground_total = user.recognition_counter(bucket) + total if condition[:maximum].nil? || ground_total <= condition[:maximum] Database.log(user.id, total.to_i, bucket) end end end |