Module: TwitterWithAutoPagination::REST::Extension::Favoriting
- Includes:
- Utils
- Defined in:
- lib/twitter_with_auto_pagination/rest/extension/favoriting.rb
Constant Summary
Constants included from Utils
Instance Method Summary collapse
- #_count_users_with_two_sided_threshold(users, options) ⇒ Object
- #_extract_favorite_users(favs, options = {}) ⇒ Object
- #_retrieve_favs(*args) ⇒ Object
- #_retrieve_replying_replied_and_favoriting(*args) ⇒ Object
- #close_friends(user, uniq: false, min: 0, max: 1000, limit: 30) ⇒ Object
- #users_which_you_faved(*args) ⇒ Object (also: #favoriting)
- #users_who_faved_you(*args) ⇒ Object (also: #favorited)
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, ) min = .has_key?(:min) ? [:min] : 0 max = .has_key?(:max) ? [: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, = {}) counted_value = _count_users_with_two_sided_threshold(favs.map { |t| t.user }, ) 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) = args. if args.empty? favorites() elsif uid_or_screen_name?(args[0]) favorites(args[0], ) 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 (*args) names = %i(replying replied favoriting) = args. if args.empty? _fetch_parallelly(names.map { |n| {method: n, args: []} }) elsif uid_or_screen_name?(args[0]) _fetch_parallelly(names.map { |n| {method: n, args: [args[0], ]} }) 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) = {uniq: uniq, min: min, max: max} min_max = {min: min, max: max} instrument(__method__, nil, ) do , replied, favoriting = (user, ) users = + replied + favoriting return [] if users.empty? score = _count_users_with_two_sided_threshold(users, min_max) = _count_users_with_two_sided_threshold(, 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] = [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) = args. instrument(__method__, nil, ) do favs = _retrieve_favs(*args, ) result = _extract_favorite_users(favs, ) if .has_key?(:uniq) && ![:uniq] result else result.uniq { |r| r.id } end end rescue => e logger.warn "#{__method__} #{user.inspect} #{e.class} #{e.}" raise e end |
#users_who_faved_you(*args) ⇒ Object Also known as: favorited
56 57 58 |
# File 'lib/twitter_with_auto_pagination/rest/extension/favoriting.rb', line 56 def users_who_faved_you(*args) raise NotImplementedError end |