Class: Twitter::Cache::Wrapper

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/twitter/cache/wrapper.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#cache, #config, #friends_cache, #user_cache

Constructor Details

#initialize(twitter) ⇒ Wrapper

Returns a new instance of Wrapper.



7
8
9
# File 'lib/twitter/cache/wrapper.rb', line 7

def initialize(twitter)
  @twitter = twitter
end

Instance Attribute Details

#twitterObject (readonly)

Returns the value of attribute twitter.



5
6
7
# File 'lib/twitter/cache/wrapper.rb', line 5

def twitter
  @twitter
end

Instance Method Details

#current_idObject



46
47
48
# File 'lib/twitter/cache/wrapper.rb', line 46

def current_id
  current_user.id
end

#current_userObject



42
43
44
# File 'lib/twitter/cache/wrapper.rb', line 42

def current_user
  @current_user ||= twitter.current_user
end

#friend_ids(id = current_id) ⇒ Object



17
18
19
20
21
# File 'lib/twitter/cache/wrapper.rb', line 17

def friend_ids(id = current_id)
  friends_cache.get(id, ttl: config.ttl) do
    twitter.friend_ids(id).to_a
  end
end

#friends(id = current_id, per: 100, page: 0) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/twitter/cache/wrapper.rb', line 23

def friends(id = current_id, per: 100, page: 0)
  per = per.to_i
  ids = friend_ids(id)
  case page
  when :rand, :random, 'rand', 'random'
    users(ids.sample(per))
  else
    start = per * page.to_i
    users(ids[start, per])
  end
end

#user(id = current_id) ⇒ Object



11
12
13
14
15
# File 'lib/twitter/cache/wrapper.rb', line 11

def user(id = current_id)
  user_cache.get(id, ttl: config.ttl) do
    config.convert_user(twitter.user(id))
  end
end

#users(ids) ⇒ Object



35
36
37
38
39
40
# File 'lib/twitter/cache/wrapper.rb', line 35

def users(ids)
  known_ids(ids).tap do |known_ids|
    fetch_users(ids - known_ids) unless known_ids == ids
  end
  ids.map { |id| user_cache.get(id) }
end