redd 0.6.5 → 0.7.0

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.
Files changed (125) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +34 -33
  3. data/.rspec +3 -4
  4. data/.rubocop.yml +5 -5
  5. data/.travis.yml +9 -7
  6. data/{LICENSE.md → LICENSE.txt} +22 -22
  7. data/README.md +126 -241
  8. data/Rakefile +5 -6
  9. data/{RedditKit.LICENSE.md → RedditKit.LICENSE.txt} +13 -13
  10. data/lib/redd.rb +50 -4
  11. data/lib/redd/access.rb +76 -0
  12. data/lib/redd/clients/base.rb +178 -0
  13. data/lib/redd/clients/base/account.rb +20 -0
  14. data/lib/redd/clients/base/identity.rb +22 -0
  15. data/lib/redd/clients/base/none.rb +27 -0
  16. data/lib/redd/clients/base/privatemessages.rb +28 -0
  17. data/lib/redd/clients/base/read.rb +66 -0
  18. data/lib/redd/clients/base/stream.rb +74 -0
  19. data/lib/redd/clients/base/submit.rb +54 -0
  20. data/lib/redd/clients/base/utilities.rb +80 -0
  21. data/lib/redd/clients/base/wikiread.rb +33 -0
  22. data/lib/redd/clients/installed.rb +55 -0
  23. data/lib/redd/clients/script.rb +37 -0
  24. data/lib/redd/clients/userless.rb +31 -0
  25. data/lib/redd/clients/web.rb +57 -0
  26. data/lib/redd/error.rb +138 -153
  27. data/lib/redd/objects/base.rb +36 -0
  28. data/lib/redd/objects/comment.rb +22 -0
  29. data/lib/redd/objects/listing.rb +29 -0
  30. data/lib/redd/objects/more_comments.rb +10 -0
  31. data/lib/redd/objects/private_message.rb +18 -0
  32. data/lib/redd/objects/submission.rb +72 -0
  33. data/lib/redd/objects/subreddit.rb +152 -0
  34. data/lib/redd/objects/thing.rb +33 -0
  35. data/lib/redd/objects/thing/editable.rb +22 -0
  36. data/lib/redd/objects/thing/hideable.rb +18 -0
  37. data/lib/redd/objects/thing/inboxable.rb +25 -0
  38. data/lib/redd/objects/thing/messageable.rb +34 -0
  39. data/lib/redd/objects/thing/moderatable.rb +43 -0
  40. data/lib/redd/objects/thing/refreshable.rb +14 -0
  41. data/lib/redd/objects/thing/saveable.rb +21 -0
  42. data/lib/redd/objects/thing/votable.rb +33 -0
  43. data/lib/redd/objects/user.rb +52 -0
  44. data/lib/redd/objects/wiki_page.rb +15 -0
  45. data/lib/redd/rate_limit.rb +50 -49
  46. data/lib/redd/response/parse_json.rb +17 -33
  47. data/lib/redd/response/raise_error.rb +16 -25
  48. data/lib/redd/version.rb +4 -5
  49. data/redd.gemspec +30 -31
  50. data/spec/redd/objects/base_spec.rb +1 -0
  51. data/spec/redd/rate_limit_spec.rb +29 -29
  52. data/spec/redd/response/parse_json_spec.rb +12 -0
  53. data/spec/redd/response/raise_error_spec.rb +11 -0
  54. data/spec/redd_spec.rb +7 -5
  55. data/spec/spec_helper.rb +69 -50
  56. metadata +73 -136
  57. data/.yardopts +0 -1
  58. data/lib/redd/base.rb +0 -56
  59. data/lib/redd/client/authenticated.rb +0 -83
  60. data/lib/redd/client/authenticated/account.rb +0 -13
  61. data/lib/redd/client/authenticated/apps.rb +0 -13
  62. data/lib/redd/client/authenticated/flair.rb +0 -71
  63. data/lib/redd/client/authenticated/gold.rb +0 -13
  64. data/lib/redd/client/authenticated/links_comments.rb +0 -189
  65. data/lib/redd/client/authenticated/live.rb +0 -13
  66. data/lib/redd/client/authenticated/moderation.rb +0 -126
  67. data/lib/redd/client/authenticated/multis.rb +0 -9
  68. data/lib/redd/client/authenticated/private_messages.rb +0 -73
  69. data/lib/redd/client/authenticated/subreddits.rb +0 -172
  70. data/lib/redd/client/authenticated/users.rb +0 -9
  71. data/lib/redd/client/authenticated/wiki.rb +0 -9
  72. data/lib/redd/client/oauth2.rb +0 -71
  73. data/lib/redd/client/oauth2/authorization.rb +0 -108
  74. data/lib/redd/client/oauth2/identity.rb +0 -16
  75. data/lib/redd/client/oauth2_script.rb +0 -24
  76. data/lib/redd/client/oauth2_script/authorization.rb +0 -21
  77. data/lib/redd/client/unauthenticated.rb +0 -118
  78. data/lib/redd/client/unauthenticated/account.rb +0 -30
  79. data/lib/redd/client/unauthenticated/captcha.rb +0 -27
  80. data/lib/redd/client/unauthenticated/links_comments.rb +0 -60
  81. data/lib/redd/client/unauthenticated/listing.rb +0 -65
  82. data/lib/redd/client/unauthenticated/live.rb +0 -9
  83. data/lib/redd/client/unauthenticated/moderation.rb +0 -26
  84. data/lib/redd/client/unauthenticated/subreddits.rb +0 -49
  85. data/lib/redd/client/unauthenticated/users.rb +0 -67
  86. data/lib/redd/client/unauthenticated/utilities.rb +0 -109
  87. data/lib/redd/client/unauthenticated/wiki.rb +0 -33
  88. data/lib/redd/oauth2_access.rb +0 -70
  89. data/lib/redd/object/comment.rb +0 -74
  90. data/lib/redd/object/listing.rb +0 -29
  91. data/lib/redd/object/more_comments.rb +0 -14
  92. data/lib/redd/object/private_message.rb +0 -35
  93. data/lib/redd/object/submission.rb +0 -94
  94. data/lib/redd/object/subreddit.rb +0 -74
  95. data/lib/redd/object/user.rb +0 -34
  96. data/lib/redd/object/wiki_page.rb +0 -27
  97. data/lib/redd/thing.rb +0 -27
  98. data/lib/redd/thing/commentable.rb +0 -27
  99. data/lib/redd/thing/editable.rb +0 -16
  100. data/lib/redd/thing/hideable.rb +0 -16
  101. data/lib/redd/thing/inboxable.rb +0 -20
  102. data/lib/redd/thing/messageable.rb +0 -12
  103. data/lib/redd/thing/moderatable.rb +0 -32
  104. data/lib/redd/thing/reportable.rb +0 -12
  105. data/lib/redd/thing/saveable.rb +0 -16
  106. data/lib/redd/thing/voteable.rb +0 -22
  107. data/spec/README.md +0 -18
  108. data/spec/redd/base_spec.rb +0 -36
  109. data/spec/redd/client/authenticated/account_spec.rb +0 -5
  110. data/spec/redd/client/authenticated/apps_spec.rb +0 -2
  111. data/spec/redd/client/authenticated/flair_spec.rb +0 -26
  112. data/spec/redd/client/authenticated/gold_spec.rb +0 -2
  113. data/spec/redd/client/authenticated/links_comments_spec.rb +0 -231
  114. data/spec/redd/client/authenticated/live_spec.rb +0 -2
  115. data/spec/redd/client/unauthenticated/account_spec.rb +0 -15
  116. data/spec/redd/client/unauthenticated/captcha_spec.rb +0 -23
  117. data/spec/redd/client/unauthenticated/links_comments_spec.rb +0 -28
  118. data/spec/redd/client/unauthenticated/listing_spec.rb +0 -23
  119. data/spec/redd/client/unauthenticated/live_spec.rb +0 -2
  120. data/spec/redd/client/unauthenticated/moderation_spec.rb +0 -14
  121. data/spec/redd/client/unauthenticated/subreddits_spec.rb +0 -35
  122. data/spec/redd/client/unauthenticated/users_spec.rb +0 -34
  123. data/spec/redd/client/unauthenticated/wiki_spec.rb +0 -18
  124. data/spec/redd/oauth2_access_spec.rb +0 -83
  125. data/spec/redd/thing_spec.rb +0 -22
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4b91c525bcbdf6cce167f9b10b7594ded27166e8
4
- data.tar.gz: 12e3c3465de9fe14cde03786135b2ee2dc23c17f
3
+ metadata.gz: dc908201324aea927202ad7e528f0fbf32ae56be
4
+ data.tar.gz: 92b74d29e18b82bd9f378b5eb44f84bfac519c6f
5
5
  SHA512:
6
- metadata.gz: de0d1c5627d77bc6b22df8f9f85dd93ecda6f6bb1ec17894c21fea24dfa4ab7b04da47014d3eeba0e106ecc6a34d71ae3ef6b5333d0f81671cccb79a8147a59e
7
- data.tar.gz: 7d292d8bb1c6adb22a2b9fc9031cadbd59a9eb8cd2d8b0d8e7d04704052aeee7d38c8cde9e7e42e4c575a081fa92a2285aa65f1a282d1af139f8725c9ffefe14
6
+ metadata.gz: c4c793e73a7d8a65722678da3069cc3affe66d14b19195283dfc9b6fdcc29b183e1e67ea673f0ed5f68b069515a41f7d2fd6e46bb8cb80c36c70a6db225bb9c0
7
+ data.tar.gz: 69ba345a72d015d9e44a44c46de2ae26b57b4ff8474c48d270d4976caf1131f7d458ef184538f1108dd92f6e40547077d6a59bc1c0aec9ca3ef987cd99257639
data/.gitignore CHANGED
@@ -1,33 +1,34 @@
1
- *.gem
2
- *.rbc
3
- /.config
4
- /coverage/
5
- /InstalledFiles
6
- /pkg/
7
- /spec/reports/
8
- /test/tmp/
9
- /test/version_tmp/
10
- /tmp/
11
-
12
- ## Documentation cache and generated files:
13
- /.yardoc/
14
- /_yardoc/
15
- /doc/
16
- /rdoc/
17
-
18
- ## Environment normalisation:
19
- /.bundle/
20
- /lib/bundler/man/
21
-
22
- # for a library or gem, you might want to ignore these files since the code is
23
- # intended to run in multiple environments; otherwise, check them in:
24
- Gemfile.lock
25
- .ruby-version
26
- .ruby-gemset
27
-
28
- # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
29
- .rvmrc
30
-
31
- # Ignore VCR cassettes. It helps for testing quickly, but we want to test
32
- # against the latest API.
33
- /spec/cassettes
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /test/tmp/
9
+ /test/version_tmp/
10
+ /tmp/
11
+
12
+ ## Specific to RubyMotion:
13
+ .dat*
14
+ .repl_history
15
+ build/
16
+
17
+ ## Documentation cache and generated files:
18
+ /.yardoc/
19
+ /_yardoc/
20
+ /doc/
21
+ /rdoc/
22
+
23
+ ## Environment normalisation:
24
+ /.bundle/
25
+ /lib/bundler/man/
26
+
27
+ # for a library or gem, you might want to ignore these files since the code is
28
+ # intended to run in multiple environments; otherwise, check them in:
29
+ Gemfile.lock
30
+ .ruby-version
31
+ .ruby-gemset
32
+
33
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
34
+ .rvmrc
data/.rspec CHANGED
@@ -1,4 +1,3 @@
1
- --color
2
- --format documentation
3
- --no-profile
4
- --require spec_helper
1
+ --require spec_helper
2
+ --format documentation
3
+ --color
data/.rubocop.yml CHANGED
@@ -1,5 +1,5 @@
1
- StringLiterals:
2
- EnforcedStyle: double_quotes
3
-
4
- Style/SpaceInsideHashLiteralBraces:
5
- Enabled: false
1
+ StringLiterals:
2
+ EnforcedStyle: double_quotes
3
+
4
+ Style/SpaceInsideHashLiteralBraces:
5
+ Enabled: false
data/.travis.yml CHANGED
@@ -1,7 +1,9 @@
1
- language: ruby
2
- rvm:
3
- - 2.1.2
4
- - 2.0.0
5
- - 1.9.3
6
- - jruby-1.7
7
- - rbx-2
1
+ language: ruby
2
+ before_install:
3
+ - gem install bundler
4
+ rvm:
5
+ - ruby-2.1.0
6
+ - ruby-2.2.0
7
+ - ruby-2
8
+ - rbx-2
9
+ - jruby-9.0.0.0.pre1
@@ -1,22 +1,22 @@
1
- Copyright (c) 2014 Avinash Dwarapu
2
-
3
- MIT License
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining
6
- a copy of this software and associated documentation files (the
7
- "Software"), to deal in the Software without restriction, including
8
- without limitation the rights to use, copy, modify, merge, publish,
9
- distribute, sublicense, and/or sell copies of the Software, and to
10
- permit persons to whom the Software is furnished to do so, subject to
11
- the following conditions:
12
-
13
- The above copyright notice and this permission notice shall be
14
- included in all copies or substantial portions of the Software.
15
-
16
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1
+ Copyright (c) 2015 Avinash Dwarapu
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md CHANGED
@@ -1,241 +1,126 @@
1
- <p align="center">
2
- <img src="https://i.imgur.com/2JfE4M1.png" alt="redd"><br>
3
- <a href="https://rubygems.org/gems/redd"><img src="http://img.shields.io/gem/v/redd.svg?style=flat-square" alt="Gem Version"></a>
4
- <a href="https://travis-ci.org/avidw/redd"><img src="http://img.shields.io/travis/avidw/redd.svg?style=flat-square" alt="Build Status"></a>
5
- <a href="https://rubygems.org/gems/redd"><img src="http://img.shields.io/badge/license-MIT-blue.svg?style=flat-square" alt="MIT License"></a>
6
- <a href="https://rubygems.org/gems/redd"><img src="http://img.shields.io/gem/dt/redd.svg?style=flat-square" alt="Gem Downloads"></a>
7
- </p>
8
-
9
- **redd** is an API wrapper for [reddit](http://reddit.com/dev/api) written in ruby that focuses on being *simple and extensible*.
10
- **Check out the latest documentation on [RubyDoc](http://rubydoc.info/github/avidw/redd/master/frames).**
11
-
12
- ---
13
-
14
- <p align="center">
15
- <a href="#getting-started">Getting Started</a> |
16
- <a href="#oauth2">OAuth2</a> |
17
- <a href="#extending-redd">Extending Redd</a> |
18
- <a href="#contributing">Contributing</a> |
19
- <a href="#supported-rubies">Supported Rubies</a> |
20
- <a href="#copyright">Copyright</a>
21
- </p>
22
-
23
- ---
24
-
25
- ## Getting Started
26
- 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.*
27
-
28
- 1. **Installing**
29
- You can either install the gem directly by running `gem install redd` or by placing the gem into your `Gemfile` and running `bundle install`.
30
-
31
- ```ruby
32
- source "https://rubygems.org"
33
- gem "redd"
34
-
35
- # or if you're feeling adventurous
36
- gem "redd", github: "avidw/redd"
37
- ```
38
-
39
- 2. **Setting Up**
40
- Let's load up redd and create a client for us to work with. (The username and password aren't real!)
41
-
42
- ```ruby
43
- require "redd"
44
- #=> true
45
-
46
- r = Redd::Client::Authenticated.new_from_credentials "HelloWorldBot", "hunter2", user_agent: "HelloWorldBot v1.0 by /u/you"
47
- # => #<Redd::Client::Authenticated:0xY4D4y4D4y4dA ...
48
- ```
49
-
50
- 3. **Scouting**
51
- 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 less blocking rate limiting class, take a look at [`lib/redd/rate_limit.rb`](https://github.com/avidw/redd/blob/master/lib/redd/rate_limit.rb#L2-L23)
52
-
53
- ```ruby
54
- r.comment_stream "test" do |comment|
55
- comment.reply "World!" if comment.body =~ /^Hello\?$/i
56
- end
57
- ```
58
-
59
- 4. **Just in Case**
60
- It's also a good idea to escape some common errors from reddit in case they happen:
61
-
62
- ```ruby
63
- begin
64
- r.comment_stream "test" do |comment|
65
- comment.reply "World!" if comment.body =~ /^Hello\?$/i
66
- end
67
- rescue Redd::Error::RateLimited => e
68
- time_left = e.time
69
- sleep(time_left)
70
- rescue Redd::Error => e
71
- status = e.code
72
- # 5-something errors are usually errors on reddit's end.
73
- raise e unless (500...600).include?(status)
74
- end
75
- ```
76
-
77
- ## OAuth2
78
- Redd also provides a wrapper to connect to reddit via OAuth2 for both the `authorization_code` and `password` grants with the `Redd::Client::OAuth2` and `Redd::Client::OAuth2Script` clients. The clients' methods are similar to the authenticated client, given that you have the required scopes. Refer to [reddit's api](https://www.reddit.com/dev/api/oauth) for the various scopes. Getting OAuth2 running is easy and can even be used to integrate reddit's features into a [**Rails**](https://github.com/rails/rails) application. Another plus is that logging in via OAuth2 lets you make twice as many requests without hitting a rate limit (1/second). Let's try logging into reddit with [**sinatra**](http://www.sinatrarb.com/).
79
-
80
- *If you're using the `OAuth2Script` client, you don't have to do all this work. For the `password` grant, you can just jump straight to `request_access` without having to get a code, which is pretty sweet.*
81
-
82
- The first thing you need to do is to create an OAuth2 application in reddit [**here**](https://ssl.reddit.com/prefs/apps). For more information, refer to [**"Getting Started"**](https://github.com/reddit/reddit/wiki/OAuth2#getting-started) on reddit's wiki.
83
-
84
- Note: Although I enter the client id and secret in the code directly, I recommend you store them in environment variables or a **`.env`** file and load it with [**dotenv**](https://github.com/bkeepers/dotenv).
85
-
86
- ```ruby
87
- # config.ru
88
-
89
- require "./connect_to_reddit"
90
- run ConnectToReddit
91
- ```
92
-
93
- ```ruby
94
- # connect_to_reddit.rb
95
-
96
- require "sinatra/base"
97
- require "redd"
98
-
99
- class ConnectToReddit < Sinatra::Base
100
- configure do
101
- enable :sessions
102
-
103
- # If you're on Rails, you can replace the fixed url with a named one (e.g. redirect_url).
104
- set :client, Redd::Client::OAuth2.new("sa_xTDcJ3dWz0w", "very-sensitive-secret", "http://localhost:8080/auth/reddit/redirect")
105
- end
106
-
107
- get "/auth/reddit" do
108
- # Make use of the state!
109
- # SecureRandom, which is included in Ruby, helps create a url-safe random string.
110
- state = SecureRandom.urlsafe_base64
111
- session[:state] = state
112
- redirect settings.client.auth_url(["identity"], :temporary, state)
113
- end
114
-
115
- get "/auth/reddit/redirect" do
116
- raise "Your state doesn't match!" unless session[:state] == params[:state]
117
-
118
- # access is a Redd::OAuth2Access object.
119
- access = settings.client.request_access(params[:code])
120
- me = settings.client.with_access(access) { |client| client.me }
121
-
122
- # Now use the Redd::Object::User object to create a user, maybe assign some
123
- # sort of token to remember their session.
124
- redirect to("/success")
125
- end
126
- end
127
- ```
128
-
129
- Now let's run the application:
130
-
131
- ```shell
132
- $ rackup -p 8080
133
- ```
134
-
135
- #### Remember Me
136
- If you want longer control of users' accounts for background tasks like auto-saving to a users' account behind the scenes or not have to ask your user to go through reddit every time, you can choose to have a permanent access by changing the second parameter of `auth_url`.
137
-
138
- ```ruby
139
- client = Redd::Client::OAuth2.new("sa_xTDcJ3dWz0w", "very-sensitive-secret", "http://localhost:8080/auth/reddit/redirect")
140
- state = SecureRandom.urlsafe_base64
141
- auth_url = client.auth_url(["identity"], :permanent, state)
142
- ```
143
-
144
- The access will still only last one hour, but you can refresh the access whenever you want.
145
-
146
- ```ruby
147
- access = client.request_access(params[:code])
148
-
149
- # 1 hour or more later
150
- client.refresh_access(access) if access.expired?
151
- ```
152
-
153
- Now if you are running a web application, you can't just store access tokens in memory. `Redd::OAuth2Access` offers a couple of methods for serializing the access to JSON and retrieving it.
154
-
155
- ```ruby
156
- json = access.to_json
157
- current_user.update!(access: json) # Rails
158
- redis.set("some-token", json) # Redis
159
-
160
- # After some time
161
- access = Redd::OAuth2Access.from_json(current_user.access)
162
- client.with_access(access) do |authenticated_client|
163
- authenticated_client.do_whatever_redd_client_can_do
164
- end
165
- ```
166
-
167
- #### Who, me?
168
- You can also revoke access tokens after the user has logged out to make sure the tokens can't be used for malicious purposes.
169
-
170
- ```ruby
171
- also_revoke_refresh_token = true
172
- client.revoke_access(access, also_revoke_refresh_token)
173
- ```
174
-
175
- ## Extending Redd
176
- 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".
177
-
178
- 1. Let's start by creating a module for the methods to live in.
179
-
180
- ```ruby
181
- module MyGildingExtension
182
- end
183
- ```
184
-
185
- 2. Let's add a method to gild a thing, using the [reddit api](http://www.reddit.com/dev/api#section_gold) and following the conventions.
186
-
187
- ```ruby
188
- module MyGildingExtension
189
- def gild(thing)
190
- # Redd::Client::Unauthenticated::Utilities has some pretty helpful
191
- # methods.
192
- fullname = extract_fullname(thing)
193
-
194
- # We're using post instead of object_from_response, because we don't
195
- # expect any object from the response.
196
- post "/api/v1/gold/gild/#{fullname}"
197
- end
198
- end
199
- ```
200
-
201
- 3. Let's add the method to the Authenticated client. You can also add it to the Unauthenticated client, but since unauthenticated users can't gild, there's no point.
202
-
203
- ```ruby
204
- Redd::Client::Authenticated.include(MyGildingExtension)
205
- ```
206
-
207
- 4. You might also want to add the method to objects to make it easier to access.
208
-
209
- ```ruby
210
- module Gildable
211
- def gild
212
- # Every Redd::Object is instantiated with the client that created
213
- # it, so the method can be called on the client easily, similar to
214
- # praw in python.
215
- client.gild(self)
216
- end
217
- end
218
-
219
- Redd::Object::Submission.include(Gildable)
220
- Redd::Object::Comment.include(Gildable)
221
- ```
222
-
223
- ## Contributing
224
- [Fork the repository](https://github.com/avidw/redd/fork), [create a pull request](https://github.com/avidw/redd/compare) and [go nuts](https://i.imgur.com/lz7hOlC.jpg).
225
-
226
- *You don't have to run the tests, Travis CI will automatically do that with my testing subreddit and account when you submit a pull request.*
227
-
228
- ## Supported Rubies
229
- This gem aims to work on the following rubies:
230
-
231
- MRI: **1.9.3** - **2.1.2**
232
- JRuby: **1.7.x**
233
- Rubinius: **2.x.x**
234
-
235
- ## Copyright
236
- Copyright (c) [Avinash Dwarapu](http://github.com/avidw) under the MIT License. See LICENSE.md for more details.
237
- Some code has been used from [RedditKit.rb](http://github.com/samsymons/RedditKit.rb). See RedditKit.LICENSE.md for more details.
238
-
239
- ---
240
-
241
- Redd not your cup of tea? Check out [RedditKit.rb](http://github.com/samsymons/RedditKit.rb)!
1
+ <p align="center">
2
+ <img src="https://i.imgur.com/2JfE4M1.png" alt="redd"><br>
3
+ <a href="https://rubygems.org/gems/redd"><img src="http://img.shields.io/gem/v/redd.svg?style=flat-square" alt="Gem Version"></a>
4
+ <a href="https://travis-ci.org/avidw/redd"><img src="http://img.shields.io/travis/avidw/redd.svg?style=flat-square" alt="Build Status"></a>
5
+ <a href="https://rubygems.org/gems/redd"><img src="http://img.shields.io/badge/license-MIT-blue.svg?style=flat-square" alt="MIT License"></a>
6
+ </p>
7
+
8
+ **redd** is an API wrapper for [reddit](http://www.reddit.com/dev/api) written in ruby that focuses on being consistent and extensible. **Check out the latest documentation on [RubyDoc](http://www.rubydoc.info/github/avidw/redd/master/frames/Redd.it).**
9
+
10
+ ---
11
+
12
+ #### Gemfile
13
+ ```ruby
14
+ gem "ruby", "~> 0.7.0"
15
+ # if you're feeling adventurous
16
+ # gem "redd", github: "avidw/redd", branch: "oauth2"
17
+
18
+ gem "oj", platforms: :ruby
19
+ ```
20
+
21
+ #### Getting Started
22
+ ```ruby
23
+ # Authorization (Web)
24
+ w = Redd.it(:web, "CLIENT_ID", "SECRET", "REDIRECT_URI", user_agent: "TestSite v1.0.0")
25
+ url = w.auth_url("random_state", ["identity", "read"], :temporary)
26
+ puts "Please go to #{url} and enter the code below:"
27
+ code = gets.chomp
28
+ w.authorize!(code)
29
+
30
+ # Authorization
31
+ r = Redd.it(:script, "CLIENT_ID", "SECRET", "Unidan", "hunter2", user_agent: "TestBot v1.0.0")
32
+ r.authorize!
33
+
34
+ # See documentation for more grants.
35
+ ```
36
+
37
+ ```ruby
38
+ # Access
39
+ get "/redirect" do
40
+ access = w.authorize!(params[:code])
41
+ session[:access] = access.to_json
42
+ redirect to("/name")
43
+ end
44
+
45
+ get "/name" do
46
+ session_access = Access.from_json(session[:access])
47
+ w.with(session_access) do |client|
48
+ client.refresh_access! if session_access.expired?
49
+ "Your username is #{client.me.name}"
50
+ end
51
+ end
52
+
53
+ ```
54
+
55
+ ```ruby
56
+ # Getting a model
57
+ vargas = r.user_from_name("_vargas_")
58
+ puts vargas.keys
59
+ puts vargas.over_18?
60
+
61
+ picturegame = r.subreddit_from_name("picturegame")
62
+ puts picturegame.display_name
63
+ puts picturegame.public_description
64
+ ```
65
+
66
+ ```ruby
67
+ # Listings
68
+ hot = r.get_hot("all")
69
+ hot.each { |link| puts "#{link.title} by /u/#{link.author}" }
70
+ ```
71
+
72
+ ```ruby
73
+ # Streaming
74
+ def stream_all!
75
+ r.stream :get_comments, "all" do |comment|
76
+ comment.reply("World!") if comment.body == "Hello?"
77
+ end
78
+ end
79
+ ```
80
+
81
+ ```ruby
82
+ # Escaping Errors
83
+ begin
84
+ stream_all!
85
+ rescue Redd::Error::RateLimited => error
86
+ sleep(error.time)
87
+ retry
88
+ rescue Redd::Error => error
89
+ # 5-something errors are usually errors on reddit's end.
90
+ raise error unless (500...600).include?(error.code)
91
+ retry
92
+ end
93
+ ```
94
+
95
+ #### Extras
96
+ ```ruby
97
+ # Extending Redd.rb
98
+
99
+ module MyGildingExtension
100
+ def gild!
101
+ # We're using post instead of request_object, because we don't
102
+ # expect any object from the response.
103
+ post("/api/v1/gold/gild/#{fullname}")
104
+ end
105
+ end
106
+
107
+ Redd::Objects::Comment.include(MyGildingExtension)
108
+ Redd::Objects::Submission.include(MyGildingExtension)
109
+ ```
110
+
111
+ ```markdown
112
+ # Contributing
113
+ 1. [Fork](https://github.com/avidw/redd/issues/new)
114
+ 2. [Pull](https://github.com/avidw/redd/compare)
115
+ 3. [Profit](https://www.reddit.com/r/requestabot)
116
+
117
+ # Request a Feature
118
+ 1. [Help](https://github.com/avidw/redd/issues/new)
119
+ ```
120
+
121
+ ```ruby
122
+ # Copyright.rb
123
+ #
124
+ # Copyright (c) Avinash Dwarapu under the MIT License. See LICENSE.txt for more details.
125
+ # Redd::Error has been modified from RedditKit.rb. See RedditKit.LICENSE.txt for its license.
126
+ ```