Class: Ayadn::NiceRank

Inherits:
Object show all
Defined in:
lib/ayadn/nicerank.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeNiceRank

Returns a new instance of NiceRank.



9
10
11
12
13
14
15
# File 'lib/ayadn/nicerank.rb', line 9

def initialize
  @url = 'http://api.nice.social/user/nicerank?ids='
  @store = FastCache::Cache.new(5_000, 120*60) # 5000 items with 2 hours TTL each
  @hits = 0
  @ids = 0
  @posts = 0
end

Instance Attribute Details

#storeObject (readonly)

Returns the value of attribute store.



7
8
9
# File 'lib/ayadn/nicerank.rb', line 7

def store
  @store
end

Instance Method Details

#from_ids(ids) ⇒ Object

This is for user lists, no scrolling: no need to cache Even with a lot of requests, it’s within the NR limits because of the slicing (upto 200 objects / call)



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/ayadn/nicerank.rb', line 94

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(',')}")
    if got.nil? || got.empty?
      ranks << [{}]
    else
      resps = JSON.parse(got)
      ranks << resps['data']
    end
  end
  return ranks.flatten!
end

#get_ranks(stream) ⇒ Object

Get posts Get unique posters Get NR response Fetch IDs from store if absent, decode + save to dic + cache in store if present, save to dic from store (and count hits for debug)



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/ayadn/nicerank.rb', line 23

def get_ranks stream
  begin
    user_ids, niceranks = [], {}
    stream['data'].each do |post|
      id = post['user']['id']
      user_ids << id if @store[id].nil?
    end
    user_ids.uniq!
    got = CNX.get "#{@url}#{user_ids.join(',')}" unless user_ids.empty?
    if got.nil? || got == ""
      parsed = {'meta' => {'code' => 404}, 'data' => []}
    else

      begin
        parsed = JSON.parse(got)
      rescue
        parsed = {'meta' => {'code' => 404}, 'data' => []}
      end

      unless parsed['data'].is_a?(Array)
        parsed = {'meta' => {'code' => 404}, 'data' => []}
      end

    end
    parsed['data'].each do |obj|
      res = @store[obj['user_id']]
      if res.nil?
        obj['is_human'] == true ? is_human = 1 : is_human = 0
        content = {
          rank: obj['rank'],
          is_human: is_human
        }
        @store[obj['user_id']] = content
        niceranks[obj['user_id']] = content
      else
        @hits += 1
        niceranks[obj['user_id']] = res
      end
    end


    @posts += stream['data'].size
    @ids += user_ids.size

    if Settings.options[:timeline][:debug] == true
      deb = "\n"
      deb << "+ NICERANK\n"
      deb << "* t#{Time.now.to_i}\n"
      deb << "Posts:\t\t#{stream['data'].size}\n"
      deb << "Requested NR:\t#{user_ids.size}\n"
      deb << "* TOTALS\n"
      deb << "Posts:\t\t#{@posts}\n"
      deb << "Fetched ranks:\t#{@ids}\n"
      deb << "DB hits:\t#{@hits}\n"
      deb << "Uniques:\t#{@store.count}\n"
      deb << "\n"
      puts deb.color(Settings.options[:colors][:debug])
      Logs.rec.debug "NICERANK/POSTS: #{@posts}"
      Logs.rec.debug "NICERANK/NR CALLS: #{@ids}"
      Logs.rec.debug "NICERANK/CACHE HITS: #{@hits}"
      Logs.rec.debug "NICERANK/CACHED IDS: #{@store.count}"
    end
    return niceranks
  rescue => e
    Errors.global_error({error: e, caller: caller, data: [user_ids, niceranks, parsed]})
  end
end