reddit_api 0.1.9 → 0.1.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 695f6cabfc01e4d67dd792e8a4310420c384d4cf
4
- data.tar.gz: 75f4d2b68c25333749a7693b79513a2cbe2b5bbc
3
+ metadata.gz: 004a4afe844fc852c876d856654312a3351a1412
4
+ data.tar.gz: 1343c16d1c128aab75c6415b1352de8fe9073d8e
5
5
  SHA512:
6
- metadata.gz: a41131435f118405286bad1fc923c0b180dc017716734d3e61664887d9428a0c40195f24cd41a28978b27c50f1a0bae5b49101863f10299d0395e05b4b061604
7
- data.tar.gz: 0d394fa9542b1bd14f5921391915c91fa53f73b51d2ae762ea5ea1ba3c74cdea3e607e12cc48e97f60c5514f5be0e10863ad6425f274344c9e6d44aca7329a05
6
+ metadata.gz: a64b90257ca848a0fdf03171cb0d798bb6d6600a64d079c0550b2a455f1c713c966440c1ca0ad29e3345fee7e5466896190117008e97ab4770b47ec0ca38b98f
7
+ data.tar.gz: 6efb3c4f163a9fcd554de328df64274ad8c95d989ddb6b3f9a6c218724ca51f5a261acd9683e338a5282cac39e89e2cea2b4b27cc14388b1296faf63be8112ef
@@ -25,9 +25,9 @@ module RedditApi
25
25
  @last_record = nil
26
26
  end
27
27
 
28
- def get(endpoint, count, resource_type)
28
+ def get(endpoint, count, resource_type, offset = nil)
29
29
  if count > 1
30
- get_many(endpoint, count, resource_type)
30
+ get_many(endpoint, count, resource_type, offset)
31
31
  else
32
32
  get_one(endpoint, count, resource_type)
33
33
  end
@@ -39,20 +39,20 @@ module RedditApi
39
39
  attr_reader :client, :base_url, :null_response_factory, :last_record,
40
40
  :requestor
41
41
 
42
- def get_many(endpoint, count, resource_type)
42
+ def get_many(endpoint, count, resource_type, offset)
43
43
  sleep(SLEEP_TIME)
44
44
  records = {}
45
- loop do
46
- new_records = request_records(endpoint, resource_type, count)
45
+ self.last_record = offset
46
+ while !break_get?(records, count)
47
+ new_records = request_records(endpoint, count, resource_type)
47
48
  collect_records(new_records, records, count)
48
- break if break_get?(records, count)
49
49
  end
50
50
  self.last_record = nil
51
51
  records.keys
52
52
  end
53
53
 
54
54
  def get_one(endpoint, count, resource_type)
55
- request_records(endpoint, resource_type, count)
55
+ request_records(endpoint, count, resource_type)
56
56
  end
57
57
 
58
58
  def collect_records(new_records, collected_records, count)
@@ -62,7 +62,7 @@ module RedditApi
62
62
  end
63
63
  end
64
64
 
65
- def request_records(endpoint, resource_type, count)
65
+ def request_records(endpoint, count, resource_type)
66
66
  response = send_request(endpoint, resource_type)
67
67
  parse_response(response, count)
68
68
  end
@@ -2,32 +2,36 @@
2
2
  module RedditApi
3
3
  class Comments
4
4
 
5
- def initialize
6
- @client = RedditApi::Client.new
5
+ def initialize(args = {})
6
+ @client = args.fetch(:client, RedditApi::Client.new)
7
7
  @comment_factory = RedditApi::Comment
8
+ @offset = nil
8
9
  end
9
10
 
10
11
  def most_recent_subreddits(user, count)
11
12
  subreddits = {}
12
13
  while subreddits.length < count
13
- comments = most_recent_comments(user)
14
+ comments = most_recent_comments(user, 100, offset)
15
+ self.offset = comments.last.reddit_id
14
16
  collect_subreddits(comments, count, subreddits)
15
17
  end
16
18
  subreddits.keys
17
19
  end
18
20
 
19
- def most_recent_comments(user, count = 100)
20
- comments_data = most_recent_comment_data(user.username, count)
21
+ def most_recent_comments(user, count = 100, offset = nil)
22
+ comments_data = most_recent_comment_data(user.username, count, offset)
21
23
  build_all_comments(comments_data)
22
24
  end
23
25
 
26
+ protected
27
+ attr_writer :offset
24
28
  private
25
- attr_reader :client, :comment_factory
29
+ attr_reader :client, :comment_factory, :offset
26
30
 
27
- def most_recent_comment_data(username, count)
31
+ def most_recent_comment_data(username, count, offset)
28
32
  return [] if username == "[deleted]"
29
33
  endpoint = "user/#{username}/comments.json"
30
- client.get(endpoint, count, :comment)
34
+ client.get(endpoint, count, :comment, offset)
31
35
  end
32
36
 
33
37
  def build_all_comments(comments_data)
@@ -49,10 +49,18 @@ module RedditApi
49
49
 
50
50
  def build_after(resource_type, record)
51
51
  prefix = TYPE_PREFIXES[resource_type]
52
- last_resource_id = record["data"]["id"]
52
+ last_resource_id = record_id(record)
53
53
  "#{prefix}_#{last_resource_id}"
54
54
  end
55
55
 
56
+ def record_id(record)
57
+ if record.is_a?(Hash)
58
+ record["data"]["id"]
59
+ else
60
+ record
61
+ end
62
+ end
63
+
56
64
  def generate_access_token
57
65
  url = "https://www.reddit.com/api/v1/access_token"
58
66
  basic_auth = { username: id,
@@ -1,3 +1,3 @@
1
1
  module RedditApi
2
- VERSION = "0.1.9"
2
+ VERSION = "0.1.10"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: reddit_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.9
4
+ version: 0.1.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Anthony Fuentes