redd 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9995a3135f22f152099ca5061fda02f8a28edc3d
4
- data.tar.gz: 380b6410fe23d07d9d154e598d57cc0084160dca
3
+ metadata.gz: a5a75930163e7a981bfd393651e20addf1f12c81
4
+ data.tar.gz: e96ea4f4250086a5ef52a37cc1e822a2ddf49819
5
5
  SHA512:
6
- metadata.gz: 1be5b63773da2565be7c503357ee562db3448519db4ba4d158a4f86e4cc9fecf6edfdff576dabf043b13a0535ffb6101aff6ae7b0b6667eb3d7889e635b8135e
7
- data.tar.gz: c13ad09d5157a56e02fff9a5811cf30c5c8d017aeafb7fc026eda6996ef45a0807a8cd87fc0482bb68bc2deaea3e581157742a9c56265d966c88f19940bb1c1d
6
+ metadata.gz: dac14d6136bb95431162a8f4e73639fc52e4f958c6b25df9a451bd880480c8e7d3437ba079d5a1135a3896cd76dd6eca89b205e57e64d5709facc8ab4ac61413
7
+ data.tar.gz: 556829db6d338e259e1123d1b52cc3193df565e82026131fd1caf827e99b035405b4f16c272a02517c852e984f67b1bad232c6637a1028f618cdc1bb73278a2b
data/README.md CHANGED
@@ -11,7 +11,6 @@
11
11
  <p align="center">
12
12
  <a href="#getting-started">Getting Started</a> |
13
13
  <a href="#extending-redd">Extending Redd</a> |
14
- <a href="#conventions">Conventions</a> |
15
14
  <a href="#supported-rubies">Supported Rubies</a> |
16
15
  <a href="#copyright">Copyright</a>
17
16
  </p>
@@ -19,7 +18,7 @@
19
18
  ---
20
19
 
21
20
  ## Getting Started
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.*
21
+ Ruby and redd make creating reddit bots accessible and fun. To demonstrate, let's create a simple bot in four 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.*
23
22
 
24
23
  1. **Installing**
25
24
  You can either install the gem directly by running `gem install redd` or by placing the gem into your `Gemfile` and running `bundle install`.
@@ -54,6 +53,25 @@ Ruby and redd make creating reddit bots accessible and fun. To demonstrate, let'
54
53
  end
55
54
  ```
56
55
 
56
+ 4. **Just in Case**
57
+ It's also a good idea to escape some common errors from reddit in case they happen:
58
+ ```ruby
59
+ begin
60
+ r.comment_stream "test" do |comment|
61
+ if comment.body =~ /^Hello\?$/i
62
+ comment.reply "World!"
63
+ end
64
+ end
65
+ rescue Redd::Error::RateLimited => e
66
+ time_left = e.time
67
+ sleep(time_left)
68
+ rescue Redd::Error => e
69
+ status = e.message.status
70
+ # 5-something errors are usually errors on reddit's end.
71
+ raise e unless status.to_s.start_with? "5"
72
+ end
73
+ ```
74
+
57
75
  ## Extending Redd
58
76
  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".
59
77
 
@@ -101,30 +119,13 @@ Extending any ruby library, including redd is incredibly easy. Let's try this ou
101
119
  Redd::Object::Comment.include(Gildable)
102
120
  ```
103
121
 
104
- ## Conventions
105
- ### Method Names
106
- - A method that returns a Redd::Object directly is called that in lowercase. For example, a method that returns a single `Redd::Object::Subreddit` is called `subreddit`
107
- - Any method that return a listing is in the format `get_[thing]s`. For example, a method that returns a listing of subreddits is named `get_subreddits`.
108
- - An **internal** method that edits an existing object is **usually** named `edit_[thing]`. Some exeptions to this rule are `vote` that edit a user's vote.
109
- - Any method that returns something specific to the user must have "my" in the middle. For example, a method that returns a users subscribed subreddits is named `get_my_subscriptions`.
110
-
111
- ### Methods
112
- - Most methods that use an http request should usually follow this convention. I haven't had time to check the methods to see if they follow this, but this should be the case
113
- ```ruby
114
- def subreddit(thing, options = {})
115
- fullname = extract_fullname(thing)
116
-
117
- meth = :get
118
- path = "/r/ruby/about.json"
119
- params = options << {additional: "options"}
120
-
121
- object_from_response(meth, path, params)
122
- end
123
- ```
124
-
125
122
  ## Supported Rubies
126
123
  TODO: Travis CI
127
124
 
128
125
  ## Copyright
129
126
  Copyright (c) [Avinash Dwarapu](http://github.com/avidw) under the MIT License. See LICENSE.md for more details.
130
127
  Some code has been used from [RedditKit.rb](http://github.com/samsymons/RedditKit.rb). See RedditKit.LICENSE.md for more details.
128
+
129
+ ---
130
+
131
+ Redd not your cup of tea? Check out [RedditKit.rb](http://github.com/samsymons/RedditKit.rb)!
@@ -74,8 +74,8 @@ module Redd
74
74
  def connection
75
75
  @connection ||= Faraday.new(url: api_endpoint) do |faraday|
76
76
  faraday.use Faraday::Request::UrlEncoded
77
- faraday.use Redd::Response::ParseJson
78
77
  faraday.use Redd::Response::RaiseError
78
+ faraday.use Redd::Response::ParseJson
79
79
  faraday.adapter Faraday.default_adapter
80
80
 
81
81
  faraday.headers["User-Agent"] = user_agent
@@ -63,8 +63,8 @@ module Redd
63
63
  def connection
64
64
  @connection ||= Faraday.new(url: api_endpoint) do |faraday|
65
65
  faraday.use Faraday::Request::UrlEncoded
66
- faraday.use Redd::Response::ParseJson
67
66
  faraday.use Redd::Response::RaiseError
67
+ faraday.use Redd::Response::ParseJson
68
68
  faraday.adapter Faraday.default_adapter
69
69
 
70
70
  faraday.headers["User-Agent"] = user_agent
@@ -15,17 +15,17 @@ module Redd
15
15
  submission_stream(:comments, *args, &block)
16
16
  end
17
17
 
18
- def submission_stream(listing, subreddit = "all", params = {}, &block)
18
+ def submission_stream(listing, subreddit = nil, params = {}, &block)
19
19
  loop do
20
20
  # Get the latest comments from the subreddit. By the way, this line
21
21
  # is the one where the sleeping/rate-limiting happens.
22
22
  objects = get_listing(listing, subreddit, params)
23
23
  unless objects.empty?
24
- # Set the latest comment.
25
- params[:before] = objects.first.fullname
26
24
  # Run the loop for each of the new comments accessed.
27
25
  # I should probably add it to some sort of Set to avoid duplicates.
28
26
  objects.reverse_each { |object| block.call(object) }
27
+ # Set the latest comment.
28
+ params[:before] = objects.first.fullname
29
29
  end
30
30
  end
31
31
  end
data/lib/redd/error.rb CHANGED
@@ -4,7 +4,7 @@ module Redd
4
4
  # @note I ripped off RedditKit.rb :|
5
5
  def from_response(response) # rubocop:disable Style/CyclomaticComplexity Style/MethodLength
6
6
  status = response[:status]
7
- body = parse_error(response[:body])
7
+ body = parse_error(response[:body]).to_s
8
8
  case status
9
9
  when 200
10
10
  case body
@@ -53,7 +53,7 @@ module Redd
53
53
  if body.key?(:json) && body[:json].key?(:errors)
54
54
  body[:json][:errors].first
55
55
  elsif body.key?(:jquery)
56
- body[:jquery].to_s
56
+ body[:jquery]
57
57
  end
58
58
  end
59
59
  end
@@ -78,7 +78,12 @@ module Redd
78
78
 
79
79
  class PermissionDenied < Error; end
80
80
 
81
- class RateLimited < Error; end
81
+ class RateLimited < Error
82
+ attr_reader :time
83
+ def initialize(time = 0)
84
+ @time = time
85
+ end
86
+ end
82
87
 
83
88
  class RequestError < Error; end
84
89
 
@@ -10,7 +10,12 @@ module Redd
10
10
  @app.call(faraday).on_complete do |env|
11
11
  error = Redd::Error.from_response(env)
12
12
  if error
13
- fail error, env
13
+ if error == Redd::Error::RateLimited
14
+ seconds = env[:body][:json][:ratelimit]
15
+ fail error.new(seconds), env
16
+ else
17
+ fail error, env
18
+ end
14
19
  end
15
20
  end
16
21
  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.3"
4
+ VERSION = "0.1.4"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: redd
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Avinash Dwarapu
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-08-04 00:00:00.000000000 Z
11
+ date: 2014-08-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -167,7 +167,6 @@ files:
167
167
  - README.md
168
168
  - Rakefile
169
169
  - RedditKit.LICENSE.md
170
- - TODO.md
171
170
  - github/redd.png
172
171
  - lib/redd.rb
173
172
  - lib/redd/base.rb
@@ -211,7 +210,6 @@ files:
211
210
  - redd.gemspec
212
211
  - spec/redd_spec.rb
213
212
  - spec/spec_helper.rb
214
- - test.rb
215
213
  homepage: ''
216
214
  licenses:
217
215
  - MIT
data/TODO.md DELETED
@@ -1,45 +0,0 @@
1
- ## TODO.md
2
-
3
- #### Redd::Client
4
- - [ ] Unauthenticated
5
- - [ ] Account
6
- - [ ] Links & Comments
7
- - [ ] Listing
8
- - [ ] Live
9
- - [ ] Subreddit
10
- - [ ] Utilities
11
- - [ ] Wiki
12
- - [ ] Authenticated
13
- - [ ] Account
14
- - [ ] Apps
15
- - [ ] Flair
16
- - [ ] Gold
17
- - [ ] Links & Comments
18
- - [ ] Live
19
- - [ ] Moderation
20
- - [ ] Multis
21
- - [ ] Private Messages
22
- - [ ] Subreddits
23
- - [ ] Users
24
- - [ ] Wiki
25
- - [ ] OAuth2
26
-
27
- #### Redd::Thing
28
- - [ ] Creatable
29
- - [ ] Editable
30
- - [ ] Hideable
31
- - [ ] Inboxable
32
- - [ ] Messageable
33
- - [ ] Moderatable
34
- - [ ] Reportable
35
- - [ ] Saveable
36
- - [ ] Voteable
37
-
38
- #### Redd::Object
39
- - [ ] `listing` **Listing**
40
- - [ ] `more` **MoreComments**
41
- - [ ] `t1_` **Comment**
42
- - [ ] `t2_` **User**
43
- - [ ] `t3_` **Submission**
44
- - [ ] `t4_` **PrivateMessage**
45
- - [ ] `t5_` **Subreddit**
data/test.rb DELETED
@@ -1,6 +0,0 @@
1
- require "redd"
2
-
3
- c = Redd.client
4
- s = c.get_info(id: "t3_2cfy08")
5
-
6
- puts c.get_comments(s)