Class: Atig::Agent::Following

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/atig/agent/following.rb

Instance Method Summary collapse

Methods included from ExceptionUtil

daemon, safe

Constructor Details

#initialize(context, api, db) ⇒ Following

Returns a new instance of Following.



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/atig/agent/following.rb', line 10

def initialize(context, api, db)
  @opts = context.opts
  @log  = context.log
  @db  = db
  log :info, "initialize"

  api.repeat(3600){|t| update t }
  @db.followings.on_invalidated{
    log :info, "invalidated followings"
    api.delay(0){|t| update t }
  }
end

Instance Method Details

#update(api) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/atig/agent/following.rb', line 23

def update(api)
  if @db.followings.empty?
    friends = api.page("friends/list", :users, {:user_id => @db.me.id, :count => 100})
  else
    @db.me = api.post("account/update_profile")
    return if @db.me.friends_count == @db.followings.size
    friends = api.page("friends/list", :users, {:user_id => @db.me.id, :count => 100})
  end

  if @opts.only
    followers = api.page("friends/ids", :ids, {:user_id => @db.me.id, :count => 2500})
    friends.each do|friend|
      friend[:only] = !followers.include?(friend.id)
    end
  end

  @db.followings.transaction do|d|
    d.update friends
  end
end