redd 0.1.2 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 737c5bb6b96b0507169cd88c0d2b6d7117241dc6
4
- data.tar.gz: 277672cec830f047ffe1b17082325da5bf88fc46
3
+ metadata.gz: 9995a3135f22f152099ca5061fda02f8a28edc3d
4
+ data.tar.gz: 380b6410fe23d07d9d154e598d57cc0084160dca
5
5
  SHA512:
6
- metadata.gz: ae62116a74fcb27b218e55024133082861d63ecc5b8cba99b1a46d86d94cdb9350e59104fef2ed2963d13f31d5e7dfcf5a83f4d501adb643bb86e245b37a554b
7
- data.tar.gz: 3ae8c2e8870bcdcc0e2b3705073e1b4ae46aacc735ae662d9fedcdd07618351aa53982e911f371b5432e186dadfdab388867f248d798a57a2aa5c5ec11c6a174
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
- TODO: Elaborate.
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
- ```ruby
26
- require "redd"
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
- client = Redd.client
29
- redditdev = client.subreddit("redditdev")
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
- latest_post = redditdev.get_new.first
32
- puts latest_post.title
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, "/r/#{name}/api/accept_moderator_invite", api_type: "json"
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
- api_type: "json", user: username,
10
- passwd: password, rem: remember
9
+
11
10
  }
12
- response = send(meth, path, params)
13
- data = response.body[:json][:data]
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])
@@ -27,6 +27,10 @@ module Redd
27
27
  get_listing(:controversial, *args)
28
28
  end
29
29
 
30
+ def get_comments(*args)
31
+ get_listing(:comments, *args)
32
+ end
33
+
30
34
  private
31
35
 
32
36
  def get_listing(type, subreddit = nil, params = {})
@@ -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)
@@ -51,6 +51,10 @@ module Redd
51
51
  def gilded?
52
52
  gilded > 0
53
53
  end
54
+
55
+ def reply(text)
56
+ client.reply(self, text)
57
+ end
54
58
  end
55
59
  end
56
60
  end
@@ -10,8 +10,7 @@ module Redd
10
10
  @app.call(faraday).on_complete do |env|
11
11
  error = Redd::Error.from_response(env)
12
12
  if error
13
- info = Redd::Error.parse_error(env[:body])
14
- fail error, info
13
+ fail error, env
15
14
  end
16
15
  end
17
16
  end
data/lib/redd/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # The main Redd module.
2
2
  module Redd
3
3
  # The semantic version number for Redd.
4
- VERSION = "0.1.2"
4
+ VERSION = "0.1.3"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: redd
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Avinash Dwarapu