simply_reddit 0.1.0 → 0.1.2

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
  SHA256:
3
- metadata.gz: 63988e436474e6963cfe8ae6e2b6d576464263e3a4231d17960534e4d771f9e7
4
- data.tar.gz: 8bd108b0fdec68d1ff25e1b2895c7eeacd37b2bf64c56da999a541ccdda056d6
3
+ metadata.gz: e2c47a3eb7e7e510061f81c05bd7d89ff41d6bc537c862d613ee8d17a23e1e44
4
+ data.tar.gz: f58fdb9e8f300cc94d22ce81697c6857e600bea0e30a7bd459f8e9aadf607197
5
5
  SHA512:
6
- metadata.gz: a91b7eabe7606ced0f60af675fae673b60a365c2bd6378ade2b4bf8a3658c3527c80c1c21a77c28d9c3eb8eb2c6225bee9303db58f77e1eef287cfd60b6d9c19
7
- data.tar.gz: cd84424bf974a22abdbf9b26bd9bc1fb2638b0b6667b7dc9050eb6e7021f7d385edea5110a390d8c20f4a6a120c2fd5c198adf637a14e6393b76e7e97e08976d
6
+ metadata.gz: 17143add4b23d73e7f51462ad6d018820cdfd2086b2c438f4f75dbe67b16857bdcf85b6b2c9691895232abcc460b25c0dce09c6aa94a2b50ca81b30cbda7a52d
7
+ data.tar.gz: 869cf82cef3132470c4ed03260c9af6a59e8190c487cb33edc400678b759204144fa0eb6461d82e938f4073c6804173bbf2b0c3a2b5db7feb44c3d288b35cdef
data/README.md CHANGED
@@ -1,35 +1,37 @@
1
1
  # SimplyReddit
2
2
 
3
- TODO: Delete this and the text below, and describe your gem
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
- TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'simply_reddit'
11
+ ```
10
12
 
11
- Install the gem and add to the application's Gemfile by executing:
13
+ And then execute:
12
14
 
13
15
  ```bash
14
- bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
16
+ bundle install
15
17
  ```
16
18
 
17
- If bundler is not being used to manage dependencies, install the gem by executing:
19
+ Or install it yourself as:
18
20
 
19
21
  ```bash
20
- gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
22
+ gem install simply_reddit
21
23
  ```
22
24
 
23
25
  ## Usage
24
26
 
25
- TODO: Write usage instructions here
26
-
27
- ## Development
27
+ ```ruby
28
+ require 'simply_reddit'
28
29
 
29
- After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
-
31
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
-
33
- ## Contributing
34
-
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/simply_reddit.
30
+ client = SimplyReddit.client(
31
+ client_id: 'your_client_id',
32
+ secret: 'your_client_secret',
33
+ username: 'your_username',
34
+ password: 'your_password'
35
+ )
36
+ ```
37
+ ```
data/Rakefile CHANGED
@@ -1,4 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "bundler/gem_tasks"
4
- task default: %i[]
4
+
5
+ desc "Run tests"
6
+ task :test do
7
+ ruby "test/simply_reddit_test.rb"
8
+ end
9
+
10
+ task default: [:test]
@@ -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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SimplyReddit
4
- VERSION = "0.1.0"
4
+ VERSION = "0.1.2"
5
5
  end
data/lib/simply_reddit.rb CHANGED
@@ -2,9 +2,23 @@
2
2
 
3
3
  require_relative "simply_reddit/version"
4
4
 
5
+
5
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
+
6
12
  class Error < StandardError; end
7
- def self.hi
8
- puts "hello world"
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
+ )
9
23
  end
10
24
  end
metadata CHANGED
@@ -1,13 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simply_reddit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ken Lu
8
+ autorequire:
8
9
  bindir: exe
9
10
  cert_chain: []
10
- date: 1980-01-02 00:00:00.000000000 Z
11
+ date: 2025-08-24 00:00:00.000000000 Z
11
12
  dependencies:
12
13
  - !ruby/object:Gem::Dependency
13
14
  name: faraday
@@ -23,6 +24,7 @@ dependencies:
23
24
  - - "~>"
24
25
  - !ruby/object:Gem::Version
25
26
  version: 2.13.4
27
+ description:
26
28
  email:
27
29
  - kenlu519@gmail.com
28
30
  executables: []
@@ -32,6 +34,10 @@ files:
32
34
  - README.md
33
35
  - Rakefile
34
36
  - lib/simply_reddit.rb
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
35
41
  - lib/simply_reddit/version.rb
36
42
  - sig/simply_reddit.rbs
37
43
  homepage: https://github.com/kenyounot123/simply_reddit
@@ -39,6 +45,7 @@ licenses: []
39
45
  metadata:
40
46
  homepage_uri: https://github.com/kenyounot123/simply_reddit
41
47
  source_code_uri: https://github.com/kenyounot123/simply_reddit
48
+ post_install_message:
42
49
  rdoc_options: []
43
50
  require_paths:
44
51
  - lib
@@ -53,7 +60,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
53
60
  - !ruby/object:Gem::Version
54
61
  version: '0'
55
62
  requirements: []
56
- rubygems_version: 3.6.9
63
+ rubygems_version: 3.3.26
64
+ signing_key:
57
65
  specification_version: 4
58
66
  summary: A simple reddit api wrapper
59
67
  test_files: []