redd 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +32 -10
- data/lib/redd/client/authenticated/moderation.rb +1 -1
- data/lib/redd/client/unauthenticated/account.rb +4 -4
- data/lib/redd/client/unauthenticated/listing.rb +4 -0
- data/lib/redd/client/unauthenticated/utilities.rb +19 -0
- data/lib/redd/object/comment.rb +4 -0
- data/lib/redd/response/raise_error.rb +1 -2
- data/lib/redd/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: 9995a3135f22f152099ca5061fda02f8a28edc3d
|
4
|
+
data.tar.gz: 380b6410fe23d07d9d154e598d57cc0084160dca
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1be5b63773da2565be7c503357ee562db3448519db4ba4d158a4f86e4cc9fecf6edfdff576dabf043b13a0535ffb6101aff6ae7b0b6667eb3d7889e635b8135e
|
7
|
+
data.tar.gz: c13ad09d5157a56e02fff9a5811cf30c5c8d017aeafb7fc026eda6996ef45a0807a8cd87fc0482bb68bc2deaea3e581157742a9c56265d966c88f19940bb1c1d
|
data/README.md
CHANGED
@@ -4,8 +4,7 @@
|
|
4
4
|
<a href="https://gemnasium.com/avidw/redd"><img src="https://gemnasium.com/avidw/redd.svg" alt="Dependency Status"></a>
|
5
5
|
</p>
|
6
6
|
|
7
|
-
**redd** is an API wrapper for [reddit](http://reddit.com/dev/api) written in ruby that focuses on being *simple and extensible*.
|
8
|
-
**NOTE: Major features are not implemented yet!**
|
7
|
+
**redd** is an API wrapper for [reddit](http://reddit.com/dev/api) written in ruby that focuses on being *simple and extensible*.
|
9
8
|
|
10
9
|
---
|
11
10
|
|
@@ -20,17 +19,40 @@
|
|
20
19
|
---
|
21
20
|
|
22
21
|
## Getting Started
|
23
|
-
|
22
|
+
Ruby and redd make creating reddit bots accessible and fun. To demonstrate, let's create a simple bot in three steps that responds to "Hello?" with "World!". *Note: this is just a tutorial; although you're welcome to take it on a test drive on a testing subreddit, don't actually host this bot.*
|
24
23
|
|
25
|
-
|
26
|
-
|
24
|
+
1. **Installing**
|
25
|
+
You can either install the gem directly by running `gem install redd` or by placing the gem into your `Gemfile` and running `bundle install`.
|
26
|
+
```ruby
|
27
|
+
source "https://rubygems.org"
|
28
|
+
gem "redd"
|
29
|
+
|
30
|
+
# or if you're feeling adventurous
|
31
|
+
gem "redd", github: "avidw/redd"
|
32
|
+
```
|
27
33
|
|
28
|
-
|
29
|
-
|
34
|
+
2. **Setting Up**
|
35
|
+
Let's load up redd and create a client for us to work with. (The username and password aren't real!)
|
36
|
+
```ruby
|
37
|
+
require "redd"
|
38
|
+
#=> true
|
39
|
+
|
40
|
+
r = Redd.client "HelloWorldBot", "hunter2"
|
41
|
+
# => #<Redd::Client::Authenticated:0xY4D4y4D4y4dA ...
|
42
|
+
|
43
|
+
# This is generally a good thing to do:
|
44
|
+
r.user_agent = "HelloWorldBot v1.0 (Redd), written by /u/Mustermind"
|
45
|
+
```
|
30
46
|
|
31
|
-
|
32
|
-
|
33
|
-
```
|
47
|
+
3. **Scouting**
|
48
|
+
Redd has a really cool method similar to praw's `helpers.comment_stream` that "streams" comments to you while avoiding duplicates. You won't have to take care of rate-limiting either; Redd `sleep`s after requests to avoid ratelimit errors. If you want to write a rate limiting class yourself, take a look at `lib/redd/rate_limit.rb`
|
49
|
+
```ruby
|
50
|
+
r.comment_stream "test" do |comment|
|
51
|
+
if comment.body =~ /^Hello\?$/i
|
52
|
+
comment.reply "World!"
|
53
|
+
end
|
54
|
+
end
|
55
|
+
```
|
34
56
|
|
35
57
|
## Extending Redd
|
36
58
|
Extending any ruby library, including redd is incredibly easy. Let's try this out by adding a gilding extension. Reddit provides an api to be able to gild posts and comments, given that you have "creddits".
|
@@ -23,7 +23,7 @@ module Redd
|
|
23
23
|
|
24
24
|
def accept_moderator_invite(subreddit)
|
25
25
|
name = extract_attribute(subreddit, :display_name)
|
26
|
-
post
|
26
|
+
post "/r/#{name}/api/accept_moderator_invite", api_type: "json"
|
27
27
|
end
|
28
28
|
|
29
29
|
def leave_contributor_status(subreddit)
|
@@ -6,11 +6,11 @@ module Redd
|
|
6
6
|
meth = :post
|
7
7
|
path = "/api/login"
|
8
8
|
params = {
|
9
|
-
|
10
|
-
passwd: password, rem: remember
|
9
|
+
|
11
10
|
}
|
12
|
-
response =
|
13
|
-
|
11
|
+
response = post "/api/login",
|
12
|
+
api_type: "json", user: username, passwd: password, rem: remember
|
13
|
+
data = response[:json][:data]
|
14
14
|
|
15
15
|
require "redd/client/authenticated"
|
16
16
|
Redd::Client::Authenticated.new(data[:cookie], data[:modhash])
|
@@ -11,6 +11,25 @@ module Redd
|
|
11
11
|
module Client
|
12
12
|
class Unauthenticated
|
13
13
|
module Utilities
|
14
|
+
def comment_stream(*args, &block)
|
15
|
+
submission_stream(:comments, *args, &block)
|
16
|
+
end
|
17
|
+
|
18
|
+
def submission_stream(listing, subreddit = "all", params = {}, &block)
|
19
|
+
loop do
|
20
|
+
# Get the latest comments from the subreddit. By the way, this line
|
21
|
+
# is the one where the sleeping/rate-limiting happens.
|
22
|
+
objects = get_listing(listing, subreddit, params)
|
23
|
+
unless objects.empty?
|
24
|
+
# Set the latest comment.
|
25
|
+
params[:before] = objects.first.fullname
|
26
|
+
# Run the loop for each of the new comments accessed.
|
27
|
+
# I should probably add it to some sort of Set to avoid duplicates.
|
28
|
+
objects.reverse_each { |object| block.call(object) }
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
14
33
|
private
|
15
34
|
|
16
35
|
def extract_attribute(object, attribute)
|
data/lib/redd/object/comment.rb
CHANGED
data/lib/redd/version.rb
CHANGED