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 +4 -4
- data/lib/reddit_api/client.rb +8 -8
- data/lib/reddit_api/comments.rb +12 -8
- data/lib/reddit_api/requestor.rb +9 -1
- data/lib/reddit_api/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 004a4afe844fc852c876d856654312a3351a1412
|
4
|
+
data.tar.gz: 1343c16d1c128aab75c6415b1352de8fe9073d8e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a64b90257ca848a0fdf03171cb0d798bb6d6600a64d079c0550b2a455f1c713c966440c1ca0ad29e3345fee7e5466896190117008e97ab4770b47ec0ca38b98f
|
7
|
+
data.tar.gz: 6efb3c4f163a9fcd554de328df64274ad8c95d989ddb6b3f9a6c218724ca51f5a261acd9683e338a5282cac39e89e2cea2b4b27cc14388b1296faf63be8112ef
|
data/lib/reddit_api/client.rb
CHANGED
@@ -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
|
-
|
46
|
-
|
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,
|
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,
|
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
|
data/lib/reddit_api/comments.rb
CHANGED
@@ -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)
|
data/lib/reddit_api/requestor.rb
CHANGED
@@ -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
|
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,
|
data/lib/reddit_api/version.rb
CHANGED