twitter_friendly 1.2.3 → 2.0.0.pre.alpha

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f3211d8ab10b2526b0ffcee549513f415670f741a7e9a131a6f1134ae3177e97
4
- data.tar.gz: 40d6900e2723a7880dbca3ba09c028e96f17be9eadce2ffbe12952bc5bb65610
3
+ metadata.gz: 317720f96610724f2d1aaf2009fc7aab40edf8fd207a82601066dfd83541eab5
4
+ data.tar.gz: 0e055652b985fbf44344683e6b6dbdf2e823f9303e9a581faad23020a303f826
5
5
  SHA512:
6
- metadata.gz: ea09e0f3a441d67077e6799a2b7c9a9728500b8d343a808e70f4348dd5b9b344353017b1d6891db9dfed2c24dc7683c9e9260e71fd56ac57ffbf61c8d0776591
7
- data.tar.gz: 55f9920997f77570d6decf5ab4029700a47d83eaf71f4b42c6cf22184eb6eb6a15c993e6edef7d44e062bd884f1b9a9aa3deb25e55ddb168b0a8324f83d0373b
6
+ metadata.gz: 3b4ca53a272e37c0184b838660b8800ef31353757627d392a97c823bb477b58b7564dfa022af0a425dfb55dfe266c642680f604d6b5875a797362ecf4ac86ded
7
+ data.tar.gz: 2d54d1bd9019648b03ff517a65d43b1bae25c71c55ebf5694184eb890fb879ca717588cfe7de35fc107c83e9d88cba8dfe570205ad3fed1af344b21964a13230
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- twitter_friendly (1.2.3)
4
+ twitter_friendly (2.0.0.pre.alpha)
5
5
  activesupport (>= 4.2, < 6.0)
6
6
  oj (~> 3.7.6)
7
7
  parallel (~> 1.12.1)
data/bin/console CHANGED
@@ -7,8 +7,8 @@ require "twitter_friendly"
7
7
  # with your gem easier. You can also use a different console, if you like.
8
8
 
9
9
  # (If you use this, don't forget to add pry to your Gemfile!)
10
- # require "pry"
11
- # Pry.start
10
+ require "pry"
11
+ Pry.start
12
12
 
13
- require "irb"
14
- IRB.start(__FILE__)
13
+ # require "irb"
14
+ # IRB.start(__FILE__)
@@ -15,10 +15,15 @@ module TwitterFriendly
15
15
  end
16
16
  end
17
17
 
18
- def fetch_resources_with_cursor(method_name, *args)
18
+ def fetch_resources_with_cursor(method_name, max_count, *args)
19
19
  options = args.dup.extract_options!
20
20
 
21
- collect_with_cursor([], -1) do |next_cursor|
21
+ total_count = options.delete(:count) || max_count
22
+ call_count = total_count / max_count + (total_count % max_count == 0 ? 0 : 1)
23
+ options[:count] = [max_count, total_count].min
24
+ collect_options = {call_count: call_count, total_count: total_count}
25
+
26
+ collect_with_cursor([], -1, collect_options) do |next_cursor|
22
27
  options[:cursor] = next_cursor unless next_cursor.nil?
23
28
  send(method_name, *args)
24
29
  end
@@ -37,20 +42,27 @@ module TwitterFriendly
37
42
  end
38
43
  end
39
44
 
40
- def collect_with_cursor(collection, cursor, &block)
45
+ def collect_with_cursor(collection, cursor, collect_options, &block)
41
46
  response = yield(cursor)
42
- return collection if response.nil?
47
+ if response.nil?
48
+ logger.warn "#{__method__}: response is nil." if respond_to?(:logger)
49
+ return collection
50
+ end
43
51
 
44
52
  # Notice: If you call response.to_a, it automatically fetch all results and the results are not cached.
45
53
 
46
54
  # cursor でリクエストするメソッドは cursor ごとキャッシュに保存するので、このメソッドで
47
55
  # ids, users または lists でリソースを取得する必要がある。
48
- collection.concat(response[:ids] || response[:users] || response[:lists])
56
+ fetched_resources = response[:ids] || response[:users] || response[:lists]
57
+ if fetched_resources.nil? || fetched_resources.empty?
58
+ logger.warn "#{__method__}: fetched_resources is nil or empty." if respond_to?(:logger)
59
+ end
60
+ collection.concat(fetched_resources)
49
61
 
50
- if response[:next_cursor].zero?
62
+ if response[:next_cursor].zero? || (collect_options[:call_count] -= 1) < 1
51
63
  collection.flatten
52
64
  else
53
- collect_with_cursor(collection, response[:next_cursor], &block)
65
+ collect_with_cursor(collection, response[:next_cursor], collect_options, &block)
54
66
  end
55
67
  end
56
68
  end
@@ -24,7 +24,7 @@ module TwitterFriendly
24
24
  if options.has_key?(:cursor)
25
25
  @twitter.friend_ids(*args)&.attrs
26
26
  else
27
- fetch_resources_with_cursor(__method__, *args)
27
+ fetch_resources_with_cursor(__method__, MAX_IDS_PER_REQUEST, *args)
28
28
  end
29
29
  end
30
30
 
@@ -35,7 +35,7 @@ module TwitterFriendly
35
35
  if options.has_key?(:cursor)
36
36
  @twitter.follower_ids(*args)&.attrs
37
37
  else
38
- fetch_resources_with_cursor(__method__, *args)
38
+ fetch_resources_with_cursor(__method__, MAX_IDS_PER_REQUEST, *args)
39
39
  end
40
40
  end
41
41
 
@@ -23,7 +23,7 @@ module TwitterFriendly
23
23
  if options.has_key?(:cursor)
24
24
  @twitter.memberships(*args)&.attrs
25
25
  else
26
- fetch_resources_with_cursor(__method__, *args)
26
+ fetch_resources_with_cursor(__method__, MAX_LISTS_PER_REQUEST, *args)
27
27
  end
28
28
  end
29
29
 
@@ -44,7 +44,7 @@ module TwitterFriendly
44
44
  if options.has_key?(:cursor)
45
45
  @twitter.list_members(*args)&.attrs
46
46
  else
47
- fetch_resources_with_cursor(__method__, *args)
47
+ fetch_resources_with_cursor(__method__, MAX_MEMBERS_PER_REQUEST, *args)
48
48
  end
49
49
  end
50
50
  end
@@ -1,3 +1,3 @@
1
1
  module TwitterFriendly
2
- VERSION = "1.2.3"
2
+ VERSION = "2.0.0-alpha"
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: 1.2.3
4
+ version: 2.0.0.pre.alpha
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-04-21 00:00:00.000000000 Z
11
+ date: 2019-08-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport