reddit_api 0.1.8 → 0.1.9
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/requestor.rb +56 -56
- 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: 695f6cabfc01e4d67dd792e8a4310420c384d4cf
|
4
|
+
data.tar.gz: 75f4d2b68c25333749a7693b79513a2cbe2b5bbc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a41131435f118405286bad1fc923c0b180dc017716734d3e61664887d9428a0c40195f24cd41a28978b27c50f1a0bae5b49101863f10299d0395e05b4b061604
|
7
|
+
data.tar.gz: 0d394fa9542b1bd14f5921391915c91fa53f73b51d2ae762ea5ea1ba3c74cdea3e607e12cc48e97f60c5514f5be0e10863ad6425f274344c9e6d44aca7329a05
|
data/lib/reddit_api/requestor.rb
CHANGED
@@ -2,71 +2,71 @@
|
|
2
2
|
module RedditApi
|
3
3
|
class Requestor
|
4
4
|
|
5
|
-
|
6
|
-
AGENT = ENV["REDDIT_AGENT"]
|
7
|
-
ID = ENV["REDDIT_ID"]
|
8
|
-
PASSWORD = ENV["REDDIT_PASSWORD"]
|
9
|
-
SECRET = ENV["REDDIT_SECRET"]
|
10
|
-
USERNAME = ENV["REDDIT_USERNAME"]
|
11
|
-
BASE_URL = "https://oauth.reddit.com/"
|
5
|
+
MAXIMUM_RECORDS = 100
|
12
6
|
|
13
|
-
|
14
|
-
|
15
|
-
|
7
|
+
def initialize(args = {})
|
8
|
+
@client = args.fetch(:client, HTTParty)
|
9
|
+
@agent = ENV["REDDIT_AGENT"]
|
10
|
+
@id = ENV["REDDIT_ID"]
|
11
|
+
@password = ENV["REDDIT_PASSWORD"]
|
12
|
+
@secret = ENV["REDDIT_SECRET"]
|
13
|
+
@username = ENV["REDDIT_USERNAME"]
|
14
|
+
@base_url = "https://oauth.reddit.com/"
|
15
|
+
end
|
16
16
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
17
|
+
def build(endpoint, resource_type, last_record = nil)
|
18
|
+
url = base_url + endpoint
|
19
|
+
headers = generate_headers
|
20
|
+
query = generate_query(resource_type, last_record)
|
21
|
+
[url, { headers: headers, query: query }]
|
22
|
+
end
|
23
23
|
|
24
|
-
|
25
|
-
|
24
|
+
private
|
25
|
+
attr_reader :agent, :base_url, :id, :password, :secret, :username, :client
|
26
26
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
27
|
+
def generate_headers
|
28
|
+
access_token = generate_access_token
|
29
|
+
{
|
30
|
+
"Authorization" => "bearer #{access_token}",
|
31
|
+
"user-agent" => agent
|
32
|
+
}
|
33
|
+
end
|
34
34
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
35
|
+
def generate_query(resource_type, last_record)
|
36
|
+
{
|
37
|
+
limit: MAXIMUM_RECORDS,
|
38
|
+
after: generate_after(resource_type, last_record)
|
39
|
+
}
|
40
|
+
end
|
41
41
|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
end
|
42
|
+
def generate_after(resource_type, last_record)
|
43
|
+
if last_record
|
44
|
+
build_after(resource_type, last_record)
|
45
|
+
else
|
46
|
+
""
|
48
47
|
end
|
48
|
+
end
|
49
49
|
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
50
|
+
def build_after(resource_type, record)
|
51
|
+
prefix = TYPE_PREFIXES[resource_type]
|
52
|
+
last_resource_id = record["data"]["id"]
|
53
|
+
"#{prefix}_#{last_resource_id}"
|
54
|
+
end
|
55
55
|
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
56
|
+
def generate_access_token
|
57
|
+
url = "https://www.reddit.com/api/v1/access_token"
|
58
|
+
basic_auth = { username: id,
|
59
|
+
password: secret }
|
60
|
+
headers = { "user-agent" => agent }
|
61
|
+
body = { grant_type: "password",
|
62
|
+
username: username,
|
63
|
+
password: password }
|
64
|
+
response = client.post(url,
|
65
|
+
basic_auth: basic_auth,
|
66
|
+
headers: headers,
|
67
|
+
body: body)
|
68
|
+
response["access_token"]
|
69
|
+
end
|
70
70
|
|
71
71
|
end
|
72
72
|
end
|
data/lib/reddit_api/version.rb
CHANGED