lib/twitter_friendly/rest/collector.rb in twitter_friendly-0.3.0 vs lib/twitter_friendly/rest/collector.rb in twitter_friendly-1.0.0
- old
+ new
@@ -1,80 +1,68 @@
module TwitterFriendly
module REST
module Collector
- def collect_with_max_id(user, collection, max_id, options, collect_options, &block)
- key = CacheKey.gen(__method__, user, options.merge(max_id: max_id, hash: credentials_hash, super_operation: collect_options[:super_operation]))
+ def fetch_tweets_with_max_id(method_name, max_count, *args)
+ options = args.dup.extract_options!
- # TODO Handle {cache: false} option
- tweets =
- @cache.fetch(key, args: [__method__, options]) do
- Instrumenter.perform_request(__method__, options) {yield(max_id)}
+ total_count = options.delete(:count) || max_count
+ call_count = total_count / max_count + (total_count % max_count == 0 ? 0 : 1)
+ options[:count] = [max_count, total_count].min
+ collect_options = {call_count: call_count, total_count: total_count}
+
+ collect_with_max_id([], nil, collect_options) do |max_id|
+ options[:max_id] = max_id unless max_id.nil?
+ result = send(method_name, *args)
+
+ if method_name == :search
+ result.attrs[:statuses]
+ else
+ if result.is_a?(Array) && result[0].respond_to?(:attrs)
+ result.map(&:attrs)
+ else
+ result
end
+ end
+ end
+ end
+
+ # @param method_name [Symbol]
+ # @param user [Integer, String, nil]
+ #
+ # @option options [Integer] :count
+ def fetch_resources_with_cursor(method_name, *args)
+ options = args.dup.extract_options!
+
+ collect_with_cursor([], -1) do |next_cursor|
+ options[:cursor] = next_cursor unless next_cursor.nil?
+ send(method_name, *args)
+ end
+ end
+
+ def collect_with_max_id(collection, max_id, collect_options, &block)
+ tweets = yield(max_id)
return collection if tweets.nil?
collection.concat tweets
if tweets.empty? || (collect_options[:call_count] -= 1) < 1
collection.flatten
else
- options[:recursive] = true
- collect_with_max_id(user, collection, tweets.last[:id] - 1, options, collect_options, &block)
+ collect_with_max_id(collection, tweets.last[:id] - 1, collect_options, &block)
end
end
# @param user [Integer, String, nil]
# @param collection [Array]
# @param cursor [Integer]
#
# @option options [Integer] :count
- # @option options [String] :super_operation
- # @option options [String] :super_super_operation
- def collect_with_cursor(user, collection, cursor, options, &block)
- key = CacheKey.gen(__method__, user, options.merge(cursor: cursor, hash: credentials_hash))
-
- # TODO Handle {cache: false} option
- response =
- @cache.fetch(key, args: [__method__, options]) do
- Instrumenter.perform_request(__method__, options) {yield(cursor).attrs}
- end
+ def collect_with_cursor(collection, cursor, &block)
+ response = yield(cursor)
return collection if response.nil?
- options[:recursive] = true
-
# Notice: If you call response.to_a, it automatically fetch all results and the results are not cached.
collection.concat (response[:ids] || response[:users] || response[:lists])
- response[:next_cursor].zero? ? collection.flatten : collect_with_cursor(user, collection, response[:next_cursor], options, &block)
- end
-
- module Instrumenter
-
- module_function
-
- # 他のメソッドと違い再帰的に呼ばれるため、全体をキャッシュすると、すべてを再帰的にキャッシュしてしまう。
- # それを防ぐために、特別にここでキャッシュの処理を登録している。
-
- def perform_request(method_name, options, &block)
- payload = {operation: 'collect', args: [method_name, options.slice(:max_id, :cursor, :super_operation)]}
- ::ActiveSupport::Notifications.instrument('collect.twitter_friendly', payload) { yield(payload) }
- end
- end
-
- module Caching
- %i(
- collect_with_max_id
- collect_with_cursor
- ).each do |name|
- define_method(name) do |*args, &block|
- options = args.extract_options!
- do_request = Proc.new { options.empty? ? super(*args, &block) : super(*args, options, &block) }
-
- if options[:recursive]
- do_request.call
- else
- TwitterFriendly::CachingAndLogging::Instrumenter.start_processing(name, options)
- TwitterFriendly::CachingAndLogging::Instrumenter.complete_processing(name, options, &do_request)
- end
- end
- end
+ response[:next_cursor].zero? ? collection.flatten : collect_with_cursor(collection, response[:next_cursor], &block)
end
end
end
end
\ No newline at end of file