lib/twitter_friendly/rest/collector.rb in twitter_friendly-0.2.1 vs lib/twitter_friendly/rest/collector.rb in twitter_friendly-0.3.0
- old
+ new
@@ -1,39 +1,41 @@
module TwitterFriendly
module REST
module Collector
- def collect_with_max_id(user, collection, max_id, options, &block)
- fetch_options = options.dup
- fetch_options[:max_id] = max_id
- fetch_options.merge!(args: [__method__, fetch_options], hash: credentials_hash)
+ 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]))
- # TODO Handle {cache: false} option
+ # TODO Handle {cache: false} option
tweets =
- @cache.fetch(__method__, user, fetch_options) do
- Instrumenter.perform_request(args: [__method__, max_id: max_id, super_operation: options[:super_operation]]) do
- yield(max_id)
- end
+ @cache.fetch(key, args: [__method__, options]) do
+ Instrumenter.perform_request(__method__, options) {yield(max_id)}
end
return collection if tweets.nil?
- options[:recursive] = true
-
collection.concat tweets
- tweets.empty? ? collection.flatten : collect_with_max_id(user, collection, tweets.last[:id] - 1, options, &block)
+ 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)
+ 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)
- fetch_options = options.dup
- fetch_options[:cursor] = cursor
- fetch_options.merge!(args: [__method__, fetch_options], hash: credentials_hash)
+ key = CacheKey.gen(__method__, user, options.merge(cursor: cursor, hash: credentials_hash))
# TODO Handle {cache: false} option
response =
- @cache.fetch(__method__, user, fetch_options) do
- Instrumenter.perform_request(args: [__method__, cursor: cursor, super_operation: options[:super_operation]]) do
- yield(cursor).attrs
- end
+ @cache.fetch(key, args: [__method__, options]) do
+ Instrumenter.perform_request(__method__, options) {yield(cursor).attrs}
end
return collection if response.nil?
options[:recursive] = true
@@ -47,12 +49,12 @@
module_function
# 他のメソッドと違い再帰的に呼ばれるため、全体をキャッシュすると、すべてを再帰的にキャッシュしてしまう。
# それを防ぐために、特別にここでキャッシュの処理を登録している。
- def perform_request(options, &block)
- payload = {operation: 'collect', args: options[:args]}
+ 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
@@ -65,11 +67,11 @@
do_request = Proc.new { options.empty? ? super(*args, &block) : super(*args, options, &block) }
if options[:recursive]
do_request.call
else
- TwitterFriendly::Caching::Instrumenter.start_processing(name, options)
- TwitterFriendly::Caching::Instrumenter.complete_processing(name, options, &do_request)
+ TwitterFriendly::CachingAndLogging::Instrumenter.start_processing(name, options)
+ TwitterFriendly::CachingAndLogging::Instrumenter.complete_processing(name, options, &do_request)
end
end
end
end
end
\ No newline at end of file