simply_reddit 0.1.1 → 0.1.3
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/README.md +73 -14
- data/lib/simply_reddit/base_client.rb +68 -0
- data/lib/simply_reddit/client.rb +63 -0
- data/lib/simply_reddit/subreddit.rb +26 -0
- data/lib/simply_reddit/user.rb +18 -0
- data/lib/simply_reddit/version.rb +1 -1
- data/lib/simply_reddit.rb +16 -4
- metadata +6 -3
- data/lib/simply_reddit/translator.rb +0 -14
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 7b190572f13f32f358a3170d73f3bbd407b086c1fc07fdb70a420120445bf290
|
|
4
|
+
data.tar.gz: 26cdcac41d91a02dc8d9fb9dc40d1d1793864c0d8dbaf87bf526725086a3e7bd
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: d677d6c7f8aa9d8182c23bdbc8ffdff479b8f920f91215bca24de71feac824d203ba57e643a2753d264795fdc9277260f4ee90eb8500ca1c1368495051abcd06
|
|
7
|
+
data.tar.gz: 953de1f9852f38829a7bd2cb312dd1b728778e0792f176c8af8320d2b45fcb9a74979ef9f0424dbc5809f4687249e626b598b31275093f608b1e57b5ce25a396
|
data/README.md
CHANGED
|
@@ -1,35 +1,94 @@
|
|
|
1
1
|
# SimplyReddit
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/simply_reddit`. To experiment with that code, run `bin/console` for an interactive prompt.
|
|
3
|
+
A simple Ruby wrapper for the Reddit API.
|
|
6
4
|
|
|
7
5
|
## Installation
|
|
8
6
|
|
|
9
|
-
|
|
7
|
+
Add this line to your application's Gemfile:
|
|
8
|
+
|
|
9
|
+
```ruby
|
|
10
|
+
gem 'simply_reddit'
|
|
11
|
+
```
|
|
10
12
|
|
|
11
|
-
|
|
13
|
+
And then execute:
|
|
12
14
|
|
|
13
15
|
```bash
|
|
14
|
-
bundle
|
|
16
|
+
bundle install
|
|
15
17
|
```
|
|
16
18
|
|
|
17
|
-
|
|
19
|
+
Or install it yourself as:
|
|
18
20
|
|
|
19
21
|
```bash
|
|
20
|
-
gem install
|
|
22
|
+
gem install simply_reddit
|
|
21
23
|
```
|
|
22
24
|
|
|
23
25
|
## Usage
|
|
24
26
|
|
|
25
|
-
|
|
27
|
+
### Authentication
|
|
28
|
+
|
|
29
|
+
```ruby
|
|
30
|
+
require 'simply_reddit'
|
|
31
|
+
|
|
32
|
+
client = SimplyReddit::Client.new(
|
|
33
|
+
client_id: 'your_client_id',
|
|
34
|
+
secret: 'your_client_secret',
|
|
35
|
+
username: 'your_username',
|
|
36
|
+
password: 'your_password'
|
|
37
|
+
)
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
### Getting Current User Information
|
|
41
|
+
|
|
42
|
+
```ruby
|
|
43
|
+
# Get information about the authenticated user
|
|
44
|
+
response = client.me
|
|
45
|
+
puts response.body # User data as JSON
|
|
46
|
+
puts response.status # HTTP status code
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### Working with Subreddits
|
|
26
50
|
|
|
27
|
-
|
|
51
|
+
```ruby
|
|
52
|
+
# Access a specific subreddit
|
|
53
|
+
subreddit = client.subreddit('ruby')
|
|
28
54
|
|
|
29
|
-
|
|
55
|
+
# The subreddit object provides access to subreddit-specific functionality
|
|
56
|
+
# (specific methods depend on your Subreddit class implementation)
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
### Working with Users
|
|
60
|
+
|
|
61
|
+
```ruby
|
|
62
|
+
# Access a specific user's profile
|
|
63
|
+
user = client.user('spez')
|
|
64
|
+
|
|
65
|
+
# The user object provides access to user-specific functionality
|
|
66
|
+
# (specific methods depend on your User class implementation)
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
### Response Format
|
|
70
|
+
|
|
71
|
+
All API responses are wrapped in a `Response` object with the following structure:
|
|
72
|
+
|
|
73
|
+
```ruby
|
|
74
|
+
response.status # HTTP status code (200, 404, etc.)
|
|
75
|
+
response.headers # HTTP headers as a hash
|
|
76
|
+
response.body # Parsed JSON response body
|
|
77
|
+
```
|
|
30
78
|
|
|
31
|
-
|
|
79
|
+
### Error Handling
|
|
32
80
|
|
|
33
|
-
|
|
81
|
+
```ruby
|
|
82
|
+
response = client.me
|
|
34
83
|
|
|
35
|
-
|
|
84
|
+
case response.status
|
|
85
|
+
when 200
|
|
86
|
+
puts "Success: #{response.body}"
|
|
87
|
+
when 401
|
|
88
|
+
puts "Authentication failed"
|
|
89
|
+
when 403
|
|
90
|
+
puts "Forbidden - check your credentials"
|
|
91
|
+
else
|
|
92
|
+
puts "Error: #{response.status}"
|
|
93
|
+
end
|
|
94
|
+
```
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
require "faraday"
|
|
2
|
+
|
|
3
|
+
class SimplyReddit::BaseClient
|
|
4
|
+
# Response struct to wrap API responses
|
|
5
|
+
Response = Struct.new(:status, :headers, :body)
|
|
6
|
+
|
|
7
|
+
def initialize(base_url:, adapter: Faraday.default_adapter, headers: {})
|
|
8
|
+
@base_url = base_url
|
|
9
|
+
@adapter = adapter
|
|
10
|
+
@default_headers = headers
|
|
11
|
+
@connection = nil
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
# HTTP methods
|
|
15
|
+
def get(path, params = {})
|
|
16
|
+
response = connection.get(path, params)
|
|
17
|
+
wrap_response(response)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def post(path, body = {})
|
|
21
|
+
response = connection.post(path, body)
|
|
22
|
+
wrap_response(response)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def put(path, body = {})
|
|
26
|
+
response = connection.put(path, body)
|
|
27
|
+
wrap_response(response)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def delete(path, params = {})
|
|
31
|
+
response = connection.delete(path, params)
|
|
32
|
+
wrap_response(response)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
protected
|
|
36
|
+
|
|
37
|
+
def configure_connection(conn)
|
|
38
|
+
conn.request :json
|
|
39
|
+
conn.response :json, content_type: "application/json"
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def connection_headers
|
|
43
|
+
@default_headers
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
private
|
|
47
|
+
|
|
48
|
+
def wrap_response(faraday_response)
|
|
49
|
+
Response.new(
|
|
50
|
+
body: faraday_response.body,
|
|
51
|
+
status: faraday_response.status,
|
|
52
|
+
headers: faraday_response.headers
|
|
53
|
+
)
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def connection
|
|
57
|
+
@connection ||= Faraday.new do |conn|
|
|
58
|
+
conn.url_prefix = @base_url
|
|
59
|
+
conn.adapter @adapter
|
|
60
|
+
|
|
61
|
+
connection_headers.each do |key, value|
|
|
62
|
+
conn.headers[key] = value
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
configure_connection(conn)
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
end
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
require "faraday"
|
|
2
|
+
require_relative "base_client"
|
|
3
|
+
require_relative "subreddit"
|
|
4
|
+
require_relative "user"
|
|
5
|
+
|
|
6
|
+
class SimplyReddit::Client < SimplyReddit::BaseClient
|
|
7
|
+
BASE_URL = "https://oauth.reddit.com"
|
|
8
|
+
DEFAULT_GRANT_TYPE = "client_credentials"
|
|
9
|
+
USER_AGENT = "Ruby:SimplyReddit:v#{SimplyReddit::VERSION} (by unknown)"
|
|
10
|
+
|
|
11
|
+
def initialize(client_id:, secret:, username:, password:, adapter: Faraday.default_adapter)
|
|
12
|
+
@user_agent = USER_AGENT
|
|
13
|
+
@client_id = client_id
|
|
14
|
+
@secret = secret
|
|
15
|
+
@username = username
|
|
16
|
+
@password = password
|
|
17
|
+
@access_token = request_access_token
|
|
18
|
+
|
|
19
|
+
super(
|
|
20
|
+
base_url: BASE_URL,
|
|
21
|
+
adapter: adapter,
|
|
22
|
+
headers: {
|
|
23
|
+
"User-Agent" => @user_agent,
|
|
24
|
+
"Authorization" => "Bearer #{@access_token}"
|
|
25
|
+
}
|
|
26
|
+
)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def subreddit(name)
|
|
30
|
+
SimplyReddit::Subreddit.new(client: self, name: name)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def user(username)
|
|
34
|
+
SimplyReddit::User.new(client: self, username: username)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def me
|
|
38
|
+
get("/api/v1/me")
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
private
|
|
42
|
+
|
|
43
|
+
def request_access_token(grant_type = DEFAULT_GRANT_TYPE, options = {})
|
|
44
|
+
endpoint = "https://www.reddit.com/api/v1/access_token"
|
|
45
|
+
|
|
46
|
+
auth_connection = Faraday.new do |conn|
|
|
47
|
+
conn.headers['User-Agent'] = @user_agent
|
|
48
|
+
conn.request :authorization, :basic, @client_id, @secret
|
|
49
|
+
conn.response :json, content_type: "application/json"
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
response = auth_connection.post(endpoint) do |req|
|
|
53
|
+
req.body = URI.encode_www_form({
|
|
54
|
+
grant_type: grant_type,
|
|
55
|
+
username: @username,
|
|
56
|
+
password: @password
|
|
57
|
+
})
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
response.body["access_token"]
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
class SimplyReddit::Subreddit
|
|
2
|
+
def initialize(client:, name:)
|
|
3
|
+
@client = client
|
|
4
|
+
@name = name
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
def hot(limit: 25)
|
|
8
|
+
@client.get("/r/#{@name}/hot", limit: limit)
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def new(limit: 25)
|
|
12
|
+
@client.get("/r/#{@name}/new", limit: limit)
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def top(limit: 25, t: 'day')
|
|
16
|
+
@client.get("/r/#{@name}/top", limit: limit, t: t)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def rising(limit: 25)
|
|
20
|
+
@client.get("/r/#{@name}/rising", limit: limit)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def about
|
|
24
|
+
@client.get("/r/#{@name}/about")
|
|
25
|
+
end
|
|
26
|
+
end
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
class SimplyReddit::User
|
|
2
|
+
def initialize(client:, username:)
|
|
3
|
+
@client = client
|
|
4
|
+
@username = username
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
def about
|
|
8
|
+
@client.get("/user/#{@username}/about")
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def posts(limit: 25, sort: 'new')
|
|
12
|
+
@client.get("/user/#{@username}/submitted", limit: limit, sort: sort)
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def comments(limit: 25, sort: 'new')
|
|
16
|
+
@client.get("/user/#{@username}/comments", limit: limit, sort: sort)
|
|
17
|
+
end
|
|
18
|
+
end
|
data/lib/simply_reddit.rb
CHANGED
|
@@ -1,12 +1,24 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require_relative "simply_reddit/version"
|
|
4
|
-
|
|
4
|
+
|
|
5
5
|
|
|
6
6
|
module SimplyReddit
|
|
7
|
+
autoload :BaseClient, "simply_reddit/base_client"
|
|
8
|
+
autoload :Client, "simply_reddit/client"
|
|
9
|
+
autoload :Subreddit, "simply_reddit/subreddit"
|
|
10
|
+
autoload :User, "simply_reddit/user"
|
|
11
|
+
|
|
7
12
|
class Error < StandardError; end
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
13
|
+
|
|
14
|
+
# Convenience method for creating a client
|
|
15
|
+
def self.client(client_id:, secret:, username:, password:, **options)
|
|
16
|
+
Client.new(
|
|
17
|
+
client_id: client_id,
|
|
18
|
+
secret: secret,
|
|
19
|
+
username: username,
|
|
20
|
+
password: password,
|
|
21
|
+
**options
|
|
22
|
+
)
|
|
11
23
|
end
|
|
12
24
|
end
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: simply_reddit
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.1.
|
|
4
|
+
version: 0.1.3
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Ken Lu
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: exe
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2025-08-
|
|
11
|
+
date: 2025-08-31 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: faraday
|
|
@@ -34,7 +34,10 @@ files:
|
|
|
34
34
|
- README.md
|
|
35
35
|
- Rakefile
|
|
36
36
|
- lib/simply_reddit.rb
|
|
37
|
-
- lib/simply_reddit/
|
|
37
|
+
- lib/simply_reddit/base_client.rb
|
|
38
|
+
- lib/simply_reddit/client.rb
|
|
39
|
+
- lib/simply_reddit/subreddit.rb
|
|
40
|
+
- lib/simply_reddit/user.rb
|
|
38
41
|
- lib/simply_reddit/version.rb
|
|
39
42
|
- sig/simply_reddit.rbs
|
|
40
43
|
homepage: https://github.com/kenyounot123/simply_reddit
|