lib/ayadn/nicerank.rb in ayadn-1.4.6 vs lib/ayadn/nicerank.rb in ayadn-1.5.0

- old
+ new

@@ -4,29 +4,53 @@ def initialize @url = 'http://api.nice.social/user/nicerank?ids=' end + def get_posts_day ids + resp = JSON.parse(CNX.get("#{@url}#{ids.join(',')}&show_details=Y")) + if resp.nil? || resp['meta']['code'] != 200 + [] + else + resp['data'].map do |obj| + pday = obj['user']['posts_day'] == -1 ? 0 : obj['user']['posts_day'] + { + id: obj['user_id'], + posts_day:pday.round(2) + } + end + end + end + + def from_ids ids + blocs, ranks = [], [] + blank = JSON.parse({'meta' => {'code' => 404}, 'data' => []}.to_json) + until ids.empty? + blocs << ids.shift(200) + end + blocs.each do |bloc| + got = CNX.get("#{@url}#{bloc.join(',')}&show_details=Y") + if got.nil? || got.empty? + ranks << [{}] + else + resps = JSON.parse(got) + ranks << resps['data'] + end + end + return ranks.flatten! + end + def get_ranks stream user_ids, get_these, table, niceranks = [], [], {}, {} stream['data'].each do |post| user_ids << post['user']['id'].to_i table[post['user']['id'].to_i] = post['user']['username'] end user_ids.uniq! db_ranks = Databases.get_niceranks user_ids - if Settings.options[:nicerank].nil? - expire = 172800 # 48h - else - if Settings.options[:nicerank][:cache].nil? - Settings.options[:nicerank][:cache] = 48 - expire = 172800 - else - expire = Settings.options[:nicerank][:cache] * 3600 - end - end + expire = Settings.options[:nicerank][:cache] * 3600 # Time.now needs seconds db_ranks.each do |id, ranks| if ranks.nil? || (Time.now - ranks[:cached]) > expire get_these << id else