Module: TwitterWithAutoPagination::REST::Extension::Favoriting

Includes:
Utils
Defined in:
lib/twitter_with_auto_pagination/rest/extension/favoriting.rb

Constant Summary

Constants included from Utils

Utils::DEFAULT_CALL_LIMIT

Instance Method Summary collapse

Methods included from Collector

#collect_with_cursor, #collect_with_max_id

Instance Method Details

#_count_users_with_two_sided_threshold(users, options) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/twitter_with_auto_pagination/rest/extension/favoriting.rb', line 9

def _count_users_with_two_sided_threshold(users, options)
  min = options.has_key?(:min) ? options[:min] : 0
  max = options.has_key?(:max) ? options[:max] : 1000
  users.each_with_object(Hash.new(0)) { |u, memo| memo[u.id] += 1 }.
    select { |_k, v| min <= v && v <= max }.
    sort_by { |_, v| -v }.to_h
end

#_extract_favorite_users(favs, options = {}) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/twitter_with_auto_pagination/rest/extension/favoriting.rb', line 17

def _extract_favorite_users(favs, options = {})
  counted_value = _count_users_with_two_sided_threshold(favs.map { |t| t.user }, options)
  counted_value.map do |uid, cnt|
    fav = favs.find { |f| f.user.id.to_i == uid.to_i }
    Array.new(cnt, fav.user)
  end.flatten
end

#_retrieve_favs(*args) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/twitter_with_auto_pagination/rest/extension/favoriting.rb', line 25

def _retrieve_favs(*args)
  options = args.extract_options!
  if args.empty?
    favorites(options)
  elsif uid_or_screen_name?(args[0])
    favorites(args[0], options)
  elsif args[0].kind_of?(Array) && args[0].all? { |t| t.respond_to?(:text) }
    args[0]
  else
    raise ArgumentError
  end
end

#_retrieve_replying_replied_and_favoriting(*args) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/twitter_with_auto_pagination/rest/extension/favoriting.rb', line 62

def _retrieve_replying_replied_and_favoriting(*args)
  names = %i(replying replied favoriting)
  options = args.extract_options!
  if args.empty?
    _fetch_parallelly(names.map { |n| {method: n, args: [options]} })
  elsif uid_or_screen_name?(args[0])
    _fetch_parallelly(names.map { |n| {method: n, args: [args[0], options]} })
  elsif names.all? { |n| args[0].respond_to?(n) }
    names.map { |n| args[0].send(n) }
  else
    raise ArgumentError
  end
end

#close_friends(user, uniq: false, min: 0, max: 1000, limit: 30) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/twitter_with_auto_pagination/rest/extension/favoriting.rb', line 76

def close_friends(user, uniq: false, min: 0, max: 1000, limit: 30)
  options = {uniq: uniq, min: min, max: max}
  min_max = {min: min, max: max}

  instrument(__method__, nil, options) do
    replying, replied, favoriting = _retrieve_replying_replied_and_favoriting(user, options)

    users = replying + replied + favoriting
    return [] if users.empty?

    score = _count_users_with_two_sided_threshold(users, min_max)
    replying_score = _count_users_with_two_sided_threshold(replying, min_max)
    replied_score = _count_users_with_two_sided_threshold(replied, min_max)
    favoriting_score = _count_users_with_two_sided_threshold(favoriting, min_max)

    score.keys.map { |uid| users.find { |u| u.id.to_i == uid.to_i } }.map do |u|
      u[:score] = score[u.id]
      u[:replying_score] = replying_score[u.id]
      u[:replied_score] = replied_score[u.id]
      u[:favoriting_score] = favoriting_score[u.id]
      u
    end.slice(0, limit)
  end
end

#users_which_you_faved(*args) ⇒ Object Also known as: favoriting



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/twitter_with_auto_pagination/rest/extension/favoriting.rb', line 38

def users_which_you_faved(*args)
  options = args.extract_options!
  instrument(__method__, nil, options) do
    favs = _retrieve_favs(*args, options)
    result = _extract_favorite_users(favs, options)
    if options.has_key?(:uniq) && !options[:uniq]
      result
    else
      result.uniq { |r| r.id }
    end
  end
rescue => e
  logger.warn "#{__method__} #{user.inspect} #{e.class} #{e.message}"
  raise e
end

#users_who_faved_you(*args) ⇒ Object Also known as: favorited

Raises:

  • (NotImplementedError)


56
57
58
# File 'lib/twitter_with_auto_pagination/rest/extension/favoriting.rb', line 56

def users_who_faved_you(*args)
  raise NotImplementedError
end