twitter_friendly 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,9 +4,18 @@ module TwitterFriendly
4
4
 
5
5
  MAX_IDS_PER_REQUEST = 100
6
6
 
7
+ # @return [Hash]
8
+ #
9
+ # @overload retweeters_ids(tweet, options = {})
10
+ #
11
+ # @param user [Integer, String] A Twitter user ID or screen name.
12
+ #
13
+ # @option options [Integer] :count The number of tweets to return per page, up to a maximum of 5000.
7
14
  def retweeters_ids(*args)
8
- args << {count: MAX_IDS_PER_REQUEST}.merge(args.extract_options!)
9
- fetch_resources_with_cursor(__method__, args)
15
+ # このメソッドではページングができない
16
+ options = {count: MAX_IDS_PER_REQUEST}.merge(args.extract_options!)
17
+ push_operations(options, __method__)
18
+ fetch_resources_with_cursor(__method__, args[0], options)
10
19
  end
11
20
  end
12
21
  end
@@ -2,29 +2,67 @@ module TwitterFriendly
2
2
  module REST
3
3
  module Users
4
4
  def verify_credentials(options = {})
5
- @twitter.send(__method__, {skip_status: true}.merge(options))&.to_hash
5
+ @twitter.verify_credentials({skip_status: true}.merge(options))&.to_hash
6
6
  end
7
7
 
8
- def user?(*args)
9
- @twitter.send(__method__, *args)
8
+ def user?(user, options = {})
9
+ @twitter.user?(user, options)
10
10
  end
11
11
 
12
12
  def user(*args)
13
- @twitter.send(__method__, *args)&.to_hash
13
+ @twitter.user(*args)&.to_hash
14
14
  end
15
15
 
16
16
  MAX_USERS_PER_REQUEST = 100
17
17
 
18
18
  def users(values, options = {})
19
19
  if values.size <= MAX_USERS_PER_REQUEST
20
- @twitter.send(__method__, values, options)&.compact&.map(&:to_hash)
20
+ key = CacheKey.gen(__method__, values, options.merge(hash: credentials_hash))
21
+
22
+ @cache.fetch(key, args: [__method__, options]) do
23
+ Instrumenter.perform_request(args: [__method__, super_operation: options[:super_operation]]) do
24
+ @twitter.send(__method__, values, options.except(:parallel, :super_operation, :recursive))&.compact&.map(&:to_hash)
25
+ end
26
+ end
21
27
  else
28
+ options[:recursive] = true
22
29
  _users(values, options)
23
30
  end
24
31
  end
25
32
 
26
33
  def blocked_ids(*args)
27
- @twitter.send(__method__, *args)&.attrs&.fetch(:ids)
34
+ @twitter.blocked_ids(*args)&.attrs&.fetch(:ids)
35
+ end
36
+
37
+ module Instrumenter
38
+
39
+ module_function
40
+
41
+ # 他のメソッドと違い再帰的に呼ばれるため、全体をキャッシュすると、すべてを再帰的にキャッシュしてしまう。
42
+ # それを防ぐために、特別にここでキャッシュの処理を登録している。
43
+
44
+ def perform_request(options, &block)
45
+ payload = {operation: 'request', args: options[:args]}
46
+ ::ActiveSupport::Notifications.instrument('request.twitter_friendly', payload) { yield(payload) }
47
+ end
48
+ end
49
+
50
+ module Caching
51
+ %i(
52
+ users
53
+ ).each do |name|
54
+ define_method(name) do |*args, &block|
55
+ options = args.extract_options!
56
+ do_request = Proc.new { options.empty? ? super(*args, &block) : super(*args, options, &block) }
57
+
58
+ if options[:recursive]
59
+ do_request.call
60
+ else
61
+ TwitterFriendly::CachingAndLogging::Instrumenter.start_processing(name, options)
62
+ TwitterFriendly::CachingAndLogging::Instrumenter.complete_processing(name, options, &do_request)
63
+ end
64
+ end
65
+ end
28
66
  end
29
67
 
30
68
  private
@@ -40,7 +78,7 @@ module TwitterFriendly
40
78
  end.flatten
41
79
  else
42
80
  values.each_slice(MAX_USERS_PER_REQUEST).map do |targets|
43
- @twitter.send(:users, targets, options)
81
+ users(targets, options)
44
82
  end
45
83
  end&.flatten&.compact&.map(&:to_hash)
46
84
  end
@@ -6,6 +6,12 @@ module TwitterFriendly
6
6
  def credentials_hash
7
7
  Digest::MD5.hexdigest(access_token + access_token_secret + consumer_key + consumer_secret)
8
8
  end
9
+
10
+ def push_operations(options, operation)
11
+ options[:super_operation] = [] unless options[:super_operation]
12
+ options[:super_operation] = [options[:super_operation]] unless options[:super_operation].is_a?(Array)
13
+ options[:super_operation].prepend operation
14
+ end
9
15
  end
10
16
  end
11
17
  end
@@ -1,6 +1,19 @@
1
1
  module TwitterFriendly
2
- class Utils
3
- class << self
2
+ module Utils
3
+ def uid_or_screen_name?(object)
4
+ raise NotImplementedError.new("You must implement ##{__method__}.")
5
+ object.kind_of?(String) || object.kind_of?(Integer)
6
+ end
7
+
8
+ def authenticating_user?(target)
9
+ raise NotImplementedError.new("You must implement ##{__method__}.")
10
+ user.id.to_i == user(target).id.to_i
11
+ end
12
+
13
+ def authorized_user?(target)
14
+ raise NotImplementedError.new("You must implement ##{__method__}.")
15
+ target_user = user(target)
16
+ !target_user.protected? || friendship?(user.id.to_i, target_user.id.to_i)
4
17
  end
5
18
  end
6
19
  end
@@ -1,3 +1,3 @@
1
1
  module TwitterFriendly
2
- VERSION = "0.2.1"
2
+ VERSION = "0.3.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: twitter_friendly
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - ts-3156
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-01-08 00:00:00.000000000 Z
11
+ date: 2019-01-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -180,7 +180,7 @@ files:
180
180
  - lib/twitter_friendly.rb
181
181
  - lib/twitter_friendly/cache.rb
182
182
  - lib/twitter_friendly/cache_key.rb
183
- - lib/twitter_friendly/caching.rb
183
+ - lib/twitter_friendly/caching_and_logging.rb
184
184
  - lib/twitter_friendly/client.rb
185
185
  - lib/twitter_friendly/log_subscriber.rb
186
186
  - lib/twitter_friendly/logger.rb
@@ -188,6 +188,8 @@ files:
188
188
  - lib/twitter_friendly/rest/api.rb
189
189
  - lib/twitter_friendly/rest/base.rb
190
190
  - lib/twitter_friendly/rest/collector.rb
191
+ - lib/twitter_friendly/rest/extension/clusters.rb
192
+ - lib/twitter_friendly/rest/extension/timelines.rb
191
193
  - lib/twitter_friendly/rest/favorites.rb
192
194
  - lib/twitter_friendly/rest/friends_and_followers.rb
193
195
  - lib/twitter_friendly/rest/lists.rb
@@ -1,86 +0,0 @@
1
- module TwitterFriendly
2
- module Caching
3
- %i(
4
- verify_credentials
5
- user?
6
- user
7
- users
8
- blocked_ids
9
- friendship?
10
- ).each do |name|
11
- define_method(name) do |*args|
12
- options = args.extract_options!
13
- Instrumenter.start_processing(name, options)
14
-
15
- Instrumenter.complete_processing(name, options) do
16
- do_request =
17
- Proc.new {Instrumenter.perform_request(name, options) {options.empty? ? super(*args) : super(*args, options)}}
18
-
19
- if Utils.cache_disabled?(options)
20
- do_request.call
21
- else
22
- user = (name == :friendship?) ? args[0, 2] : args[0]
23
- @cache.fetch(name, user, options.merge(args: [name, options], hash: credentials_hash), &do_request)
24
- end
25
- end
26
- end
27
- end
28
-
29
- # これらのメソッドは、内部で呼ぶメソッドをキャッシュするため、
30
- # 全体をキャッシュすることはない。
31
- %i(
32
- friend_ids
33
- follower_ids
34
- friends
35
- followers
36
- friend_ids_and_follower_ids
37
- friends_and_followers
38
- search
39
- favorites
40
- home_timeline
41
- user_timeline
42
- mentions_timeline
43
- memberships
44
- list_members
45
- retweeters_ids
46
- ).each do |name|
47
- define_method(name) do |*args|
48
- options = args.extract_options!
49
- Instrumenter.start_processing(name, options)
50
-
51
- Instrumenter.complete_processing(name, options) do
52
- options.empty? ? super(*args) : super(*args, options)
53
- end
54
- end
55
- end
56
-
57
- module Instrumenter
58
-
59
- module_function
60
-
61
- def start_processing(operation, options)
62
- payload = {operation: operation}.merge(options)
63
- ::ActiveSupport::Notifications.instrument('start_processing.twitter_friendly', payload) {}
64
- end
65
-
66
- def complete_processing(operation, options)
67
- payload = {operation: operation}.merge(options)
68
- ::ActiveSupport::Notifications.instrument('complete_processing.twitter_friendly', payload) { yield(payload) }
69
- end
70
-
71
- def perform_request(caller, options, &block)
72
- payload = {operation: 'request', args: [caller, options]}
73
- ::ActiveSupport::Notifications.instrument('request.twitter_friendly', payload) { yield(payload) }
74
- end
75
- end
76
-
77
- module Utils
78
-
79
- module_function
80
-
81
- def cache_disabled?(options)
82
- options.is_a?(Hash) && options.has_key?(:cache) && !options[:cache]
83
- end
84
- end
85
- end
86
- end