redd 0.1.0 → 0.1.1

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 (36) hide show
  1. checksums.yaml +4 -4
  2. data/{LICENSE.txt → LICENSE.md} +0 -0
  3. data/README.md +99 -1
  4. data/Rakefile +1 -1
  5. data/{lib/redd/RedditKit.LICENSE.md → RedditKit.LICENSE.md} +1 -1
  6. data/TODO.md +45 -0
  7. data/lib/redd/base.rb +22 -5
  8. data/lib/redd/client/authenticated.rb +4 -4
  9. data/lib/redd/client/authenticated/account.rb +8 -3
  10. data/lib/redd/client/authenticated/apps.rb +1 -1
  11. data/lib/redd/client/authenticated/flair.rb +1 -1
  12. data/lib/redd/client/authenticated/gold.rb +1 -1
  13. data/lib/redd/client/authenticated/links_comments.rb +131 -2
  14. data/lib/redd/client/authenticated/live.rb +3 -3
  15. data/lib/redd/client/authenticated/moderation.rb +2 -2
  16. data/lib/redd/client/authenticated/multis.rb +2 -2
  17. data/lib/redd/client/authenticated/private_messages.rb +1 -1
  18. data/lib/redd/client/authenticated/subreddits.rb +31 -2
  19. data/lib/redd/client/authenticated/users.rb +2 -2
  20. data/lib/redd/client/authenticated/wiki.rb +2 -2
  21. data/lib/redd/client/unauthenticated.rb +10 -9
  22. data/lib/redd/client/unauthenticated/links_comments.rb +14 -0
  23. data/lib/redd/client/unauthenticated/live.rb +0 -0
  24. data/lib/redd/client/unauthenticated/subreddits.rb +16 -2
  25. data/lib/redd/client/unauthenticated/utilities.rb +23 -7
  26. data/lib/redd/client/unauthenticated/wiki.rb +2 -2
  27. data/lib/redd/object/comment.rb +39 -24
  28. data/lib/redd/object/listing.rb +9 -4
  29. data/lib/redd/object/private_message.rb +31 -0
  30. data/lib/redd/object/submission.rb +47 -65
  31. data/lib/redd/object/subreddit.rb +61 -0
  32. data/lib/redd/object/user.rb +30 -0
  33. data/lib/redd/rate_limit.rb +5 -4
  34. data/lib/redd/version.rb +1 -1
  35. data/test.rb +6 -0
  36. metadata +9 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: eefcb4605d95ff4dd74d15293cfc83ad525d1c6a
4
- data.tar.gz: f9f7689a053500c5d0021edf35476cbd019b0ca2
3
+ metadata.gz: 546ebcdeb092101a90012a2f2a3d088cdae13259
4
+ data.tar.gz: c0a9b544cd6e0786cf99d0d1fea4336929068ea6
5
5
  SHA512:
6
- metadata.gz: 5b1ce4746c616835b1115000c04685f8f24b009a79e26857f60d0bce2f50c3a53078d0ec5f46dc28819bc92dd46d4d645899901219a80fdf8556e2d03cc376ca
7
- data.tar.gz: 6ab5345939dae63c5cc132a5ca9b91efb331c55c3a7e137659306094ce2ffbe7ceb150df490e688f06484f062e2f614395797384e96cef64473c38099429f700
6
+ metadata.gz: 213c006802d87c57bcdd4236f793c7def0eeadb47d5991e3f7d2894dba3824373b9c7c6fe5ba817a65a9e0de54b12b2047c5a0200bc1487cfc11ef13791f9b34
7
+ data.tar.gz: a6ca604d6a3c55a9dd1dec45edc9537b5a2b5f153447c8d6ce05b49d721f4e12c6204af765cfc8c6d96f4848559757b8a21e38ea7361aef8f5ab5c14831b2ac1
File without changes
data/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  <p align="center">
2
2
  <img src="github/redd.png?raw=true" alt="redd"><br>
3
+ <a href="http://badge.fury.io/rb/redd"><img src="https://badge.fury.io/rb/redd.svg" alt="Gem Version" height="18"></a>
4
+ <a href="https://gemnasium.com/avidw/redd"><img src="https://gemnasium.com/avidw/redd.svg" alt="Dependency Status"></a>
3
5
  </p>
4
6
 
5
7
  **redd** is an API wrapper for [reddit](http://reddit.com/dev/api) written in ruby that focuses on being *simple and extensible*.
@@ -7,4 +9,100 @@
7
9
 
8
10
  ---
9
11
 
10
- ##
12
+ <p align="center">
13
+ <a href="#getting-started">Getting Started</a> |
14
+ <a href="#extending-redd">Extending Redd</a> |
15
+ <a href="#conventions">Conventions</a> |
16
+ <a href="#supported-rubies">Supported Rubies</a> |
17
+ <a href="#copyright">Copyright</a>
18
+ </p>
19
+
20
+ ---
21
+
22
+ ## Getting Started
23
+ TODO: Elaborate.
24
+
25
+ ```ruby
26
+ require "redd"
27
+
28
+ client = Redd.client
29
+ redditdev = client.subreddit("redditdev")
30
+
31
+ latest_post = redditdev.get_new.first
32
+ puts latest_post.title
33
+ ```
34
+
35
+ ## Extending Redd
36
+ 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".
37
+
38
+ 1. Let's start by creating a module for the methods to live in.
39
+ ```ruby
40
+ module MyGildingExtension
41
+ end
42
+ ```
43
+
44
+ 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.
45
+ ```ruby
46
+ module MyGildingExtension
47
+ def gild(thing)
48
+ # Redd::Client::Unauthenticated::Utilities has some pretty helpful
49
+ # methods.
50
+ fullname = extract_fullname(thing)
51
+
52
+ meth = :post
53
+ path = "/api/v1/gold/gild/#{fullname}"
54
+
55
+ # We're using send instead of object_from_response, because we don't
56
+ # expect any object from the response.
57
+ send(meth, path)
58
+ end
59
+ end
60
+ ```
61
+
62
+ 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.
63
+ ```ruby
64
+ Redd::Client::Authenticated.include(MyGildingExtension)
65
+ ```
66
+
67
+ 4. You might also want to add the method to objects to make it easier to access.
68
+ ```ruby
69
+ module Gildable
70
+ def gild
71
+ # Every Redd::Object is instantiated with the client that created
72
+ # it, so the method can be called on the client easily, similar to
73
+ # praw in python.
74
+ client.gild(self)
75
+ end
76
+ end
77
+
78
+ Redd::Object::Submission.include(Gildable)
79
+ Redd::Object::Comment.include(Gildable)
80
+ ```
81
+
82
+ ## Conventions
83
+ ### Method Names
84
+ - 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`
85
+ - 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`.
86
+ - 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.
87
+ - 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`.
88
+
89
+ ### Methods
90
+ - 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
91
+ ```ruby
92
+ def subreddit(thing, options = {})
93
+ fullname = extract_fullname(thing)
94
+
95
+ meth = :get
96
+ path = "/r/ruby/about.json"
97
+ params = options << {additional: "options"}
98
+
99
+ object_from_response(meth, path, params)
100
+ end
101
+ ```
102
+
103
+ ## Supported Rubies
104
+ TODO: Travis CI
105
+
106
+ ## Copyright
107
+ Copyright (c) [Avinash Dwarapu](http://github.com/avidw) under the MIT License. See LICENSE.md for more details.
108
+ Some code has been used from [RedditKit.rb](http://github.com/samsymons/RedditKit.rb). See RedditKit.LICENSE.md for more details.
data/Rakefile CHANGED
@@ -7,4 +7,4 @@ RSpec::Core::RakeTask.new
7
7
  RuboCop::RakeTask.new
8
8
  YARD::Rake::YardocTask.new
9
9
 
10
- task default: [:spec, :rubocop, :yard]
10
+ task default: :spec
@@ -1,4 +1,4 @@
1
- The errors file would have been impossible if it weren't for @samsymons :)
1
+ The error and base files would have been impossible if it weren't for @samsymons :)
2
2
 
3
3
  ---
4
4
 
data/TODO.md ADDED
@@ -0,0 +1,45 @@
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**
@@ -16,11 +16,16 @@ module Redd
16
16
 
17
17
  # Define and memoize the method that returns a key from the
18
18
  # attributes hash.
19
- # @param [Symbol, String] key The key to construct a method out of.
20
- # @param [Symbol] attribute The attribute in the given hash to return.
21
- def self.attr_reader(key, attribute = key)
22
- define_method(key) { @attributes[attribute] }
23
- memoize(key)
19
+ # @param [Symbol, String] attr The attribute to construct a method out of.
20
+ def self.attr_reader(attr)
21
+ define_attribute_method(attr)
22
+ define_predicate_method(attr)
23
+ end
24
+
25
+ def [](method)
26
+ send(method.to_sym)
27
+ rescue NoMethodError
28
+ nil
24
29
  end
25
30
 
26
31
  # @param client The client to use when making requests with this object.
@@ -31,5 +36,17 @@ module Redd
31
36
  @attributes = attributes[:data]
32
37
  @attributes[:kind] = attributes[:kind]
33
38
  end
39
+
40
+ private
41
+
42
+ def self.define_attribute_method(method)
43
+ define_method(method) { @attributes[method] }
44
+ memoize method
45
+ end
46
+
47
+ def self.define_predicate_method(method)
48
+ define_method(:"#{method}?") { !!@attributes[method] }
49
+ memoize :"#{method}?"
50
+ end
34
51
  end
35
52
  end
@@ -30,19 +30,19 @@ module Redd
30
30
  include Redd::Client::Authenticated::Wiki
31
31
 
32
32
  # @!attribute [r] username
33
- # @return [String] The username of the logged-in user.
33
+ # @return [String] The username of the logged-in user.
34
34
  attr_reader :username
35
35
 
36
36
  # @!attribute [r] cookie
37
- # @return [String] The cookie used to store the current session.
37
+ # @return [String] The cookie used to store the current session.
38
38
  attr_reader :cookie
39
39
 
40
40
  # @!attribute [r] modhash
41
- # @return [String] The returned modhash used when making requests.
41
+ # @return [String] The returned modhash used when making requests.
42
42
  attr_reader :modhash
43
43
 
44
44
  # @!attribute [rw] auth_endpoint
45
- # @return [String] The site to connect to authenticate with.
45
+ # @return [String] The site to connect to authenticate with.
46
46
  attr_accessor :auth_endpoint
47
47
 
48
48
  # Set up an authenticated connection to reddit.
@@ -1,8 +1,13 @@
1
1
  module Redd
2
2
  module Client
3
- class Authenticated
4
- module Accounts
5
-
3
+ class Authenticated
4
+ module Account
5
+ def me
6
+ meth = :get
7
+ path = "/api/me.json"
8
+
9
+ object_from_response(meth, path)
10
+ end
6
11
  end
7
12
  end
8
13
  end
@@ -1,6 +1,6 @@
1
1
  module Redd
2
2
  module Client
3
- class Authenticated
3
+ class Authenticated
4
4
  module Apps
5
5
  # Low-Priority
6
6
  #
@@ -1,6 +1,6 @@
1
1
  module Redd
2
2
  module Client
3
- class Authenticated
3
+ class Authenticated
4
4
  module Flair
5
5
 
6
6
  end
@@ -1,6 +1,6 @@
1
1
  module Redd
2
2
  module Client
3
- class Authenticated
3
+ class Authenticated
4
4
  module Gold
5
5
  # Low-Priority
6
6
  #
@@ -1,8 +1,137 @@
1
1
  module Redd
2
2
  module Client
3
- class Authenticated
3
+ class Authenticated
4
4
  module LinksComments
5
-
5
+ def add_comment(thing, text, return_element = true)
6
+ fullname = extract_fullname(thing)
7
+
8
+ meth = :post
9
+ path = "/api/comment"
10
+ params = {api_type: "json", text: text, thing_id: fullname}
11
+
12
+ # TODO: Return the created object.
13
+ send(meth, path, params)
14
+ end
15
+
16
+ alias_method :reply, :add_comment
17
+
18
+ def delete(thing)
19
+ fullname = extract_fullname(thing)
20
+
21
+ meth = :post
22
+ path = "/api/del"
23
+ params = {id: fullname}
24
+
25
+ send(meth, path, params)
26
+ end
27
+
28
+ def edit(thing, text)
29
+ fullname = extract_fullname(thing)
30
+
31
+ meth = :post
32
+ path = "/api/editusertext"
33
+ params = {api_type: "json", text: text, thing_id: fullname}
34
+
35
+ # TODO: Return the edited object.
36
+ send(meth, path, params)
37
+ end
38
+
39
+ def hide(thing)
40
+ fullname = extract_fullname(thing)
41
+
42
+ meth = :post
43
+ path = "/api/hide"
44
+ params = {id: fullname}
45
+
46
+ send(meth, path, params)
47
+ end
48
+
49
+ def unhide(thing)
50
+ fullname = extract_fullname(thing)
51
+
52
+ meth = :post
53
+ path = "/api/unhide"
54
+ params = {id: fullname}
55
+
56
+ send(meth, path, params)
57
+ end
58
+
59
+ def mark_as_nsfw(thing)
60
+ fullname = extract_fullname(thing)
61
+
62
+ meth = :post
63
+ path = "/api/marknsfw"
64
+ params = {id: fullname}
65
+
66
+ send(meth, path, params)
67
+ end
68
+
69
+ def unmark_as_nsfw(thing)
70
+ fullname = extract_fullname(thing)
71
+
72
+ meth = :post
73
+ path = "/api/unmarknsfw"
74
+ params = {id: fullname}
75
+
76
+ send(meth, path, params)
77
+ end
78
+
79
+ alias_method :mark_as_safe, :unmark_as_nsfw
80
+
81
+ def report(thing)
82
+ fullname = extract_fullname(thing)
83
+
84
+ meth = :post
85
+ path = "/api/report"
86
+ params = {id: fullname}
87
+
88
+ send(meth, path, params)
89
+ end
90
+
91
+ def save(thing, category = nil)
92
+ fullname = extract_fullname(thing)
93
+
94
+ meth = :post
95
+ path = "/api/save"
96
+ params = {id: fullname}
97
+ params << {category: category} if category
98
+
99
+ send(meth, path, params)
100
+ end
101
+
102
+ def unsave(thing)
103
+ fullname = extract_fullname(thing)
104
+
105
+ meth = :post
106
+ path = "/api/unsave"
107
+ params = {id: fullname}
108
+
109
+ send(meth, path, params)
110
+ end
111
+
112
+ def upvote(thing)
113
+ vote(thing, 1)
114
+ end
115
+
116
+ def downvote(thing)
117
+ vote(thing, -1)
118
+ end
119
+
120
+ def unvote(thing)
121
+ vote(thing, 0)
122
+ end
123
+
124
+ private
125
+
126
+ def vote(thing, direction)
127
+ fullname = extract_fullname(thing)
128
+
129
+ meth = :post
130
+ path = "/api/vote"
131
+ params = {id: fullname, dir: direction}
132
+
133
+ send(meth, path, params)
134
+ end
6
135
  end
7
136
  end
8
137
  end
@@ -1,9 +1,9 @@
1
1
  module Redd
2
2
  module Client
3
- class Authenticated
3
+ class Authenticated
4
4
  module Live
5
- # Low-priority
6
- #
5
+ # Low-priority
6
+ #
7
7
  # When was this a thing? Let's just hope this doesn't become popular
8
8
  # that quickly before I get to it.
9
9
  end
@@ -1,8 +1,8 @@
1
1
  module Redd
2
2
  module Client
3
- class Authenticated
3
+ class Authenticated
4
4
  module Moderation
5
-
5
+
6
6
  end
7
7
  end
8
8
  end
@@ -1,8 +1,8 @@
1
1
  module Redd
2
2
  module Client
3
- class Authenticated
3
+ class Authenticated
4
4
  module Multis
5
-
5
+
6
6
  end
7
7
  end
8
8
  end
@@ -1,6 +1,6 @@
1
1
  module Redd
2
2
  module Client
3
- class Authenticated
3
+ class Authenticated
4
4
  module PrivateMessages
5
5
  def block_message(message)
6
6
  fullname = extract_fullname(message)
@@ -1,8 +1,37 @@
1
1
  module Redd
2
2
  module Client
3
- class Authenticated
3
+ class Authenticated
4
4
  module Subreddits
5
-
5
+ def subscribe(subreddit)
6
+ edit_subscription(:sub, subreddit)
7
+ end
8
+
9
+ def unsubscribe(subreddit)
10
+ edit_subscription(:unsub, subreddit)
11
+ end
12
+
13
+ def get_subreddits(where = :subscriber, params = {})
14
+ meth = :get
15
+ path =
16
+ if [:popular, :new].include?(where)
17
+ "/subreddits/#{where}.json"
18
+ elsif [:subscriber, :contributor, :moderator].include?(where)
19
+ "/subreddits/mine/#{where}.json"
20
+ end
21
+ object_from_response(meth, path, params)
22
+ end
23
+
24
+ private
25
+
26
+ def edit_subscription(action, subreddit)
27
+ fullname = extract_fullname(subreddit)
28
+
29
+ meth = :post
30
+ path = "/api/subscribe"
31
+ params = {action: action, sr: fullname}
32
+
33
+ send(meth, path, params)
34
+ end
6
35
  end
7
36
  end
8
37
  end
@@ -1,8 +1,8 @@
1
1
  module Redd
2
2
  module Client
3
- class Authenticated
3
+ class Authenticated
4
4
  module Users
5
-
5
+
6
6
  end
7
7
  end
8
8
  end
@@ -1,8 +1,8 @@
1
1
  module Redd
2
2
  module Client
3
- class Authenticated
3
+ class Authenticated
4
4
  module Wiki
5
-
5
+
6
6
  end
7
7
  end
8
8
  end
@@ -3,17 +3,18 @@ require "redd/version"
3
3
  require "redd/rate_limit"
4
4
  require "redd/response/parse_json"
5
5
  require "redd/response/raise_error"
6
- require "redd/client/unauthenticated/account"
7
- require "redd/client/unauthenticated/links_comments"
8
- require "redd/client/unauthenticated/listing"
9
- require "redd/client/unauthenticated/subreddits"
10
- require "redd/client/unauthenticated/utilities"
11
- require "redd/client/unauthenticated/wiki"
12
6
 
13
7
  module Redd
14
8
  module Client
15
9
  # The Client used to connect without needing login credentials.
16
10
  class Unauthenticated
11
+ require "redd/client/unauthenticated/account"
12
+ require "redd/client/unauthenticated/links_comments"
13
+ require "redd/client/unauthenticated/listing"
14
+ require "redd/client/unauthenticated/subreddits"
15
+ require "redd/client/unauthenticated/utilities"
16
+ require "redd/client/unauthenticated/wiki"
17
+
17
18
  include Redd::Client::Unauthenticated::Account
18
19
  include Redd::Client::Unauthenticated::LinksComments
19
20
  include Redd::Client::Unauthenticated::Listing
@@ -22,15 +23,15 @@ module Redd
22
23
  include Redd::Client::Unauthenticated::Wiki
23
24
 
24
25
  # @!attribute [r] api_endpoint
25
- # @return [String] The site to connect to.
26
+ # @return [String] The site to connect to.
26
27
  attr_accessor :api_endpoint
27
28
 
28
29
  # @!attribute [r] user_agent
29
- # @return [String] The user-agent used to communicate with reddit.
30
+ # @return [String] The user-agent used to communicate with reddit.
30
31
  attr_accessor :user_agent
31
32
 
32
33
  # @!attribute [r] rate_limit
33
- # @return [#after_limit] The handler that takes care of rate limiting.
34
+ # @return [#after_limit] The handler that takes care of rate limiting.
34
35
  attr_accessor :rate_limit
35
36
 
36
37
  # Set up an unauthenticated connection to reddit.
@@ -9,6 +9,20 @@ module Redd
9
9
  path = "/api/info.json"
10
10
  object_from_response(meth, path, params)
11
11
  end
12
+
13
+ def get_comments(submission)
14
+ id = extract_id(submission)
15
+
16
+ meth = :get
17
+ path = "/comments/#{id}.json"
18
+ comments_from_response(meth, path)
19
+ end
20
+
21
+ def get_replies(comment)
22
+ replies = comment.attributes[:replies]
23
+ return [] unless replies.is_a?(Hash) and replies.has_key?(:kind)
24
+ object_from_body(replies)
25
+ end
12
26
  end
13
27
  end
14
28
  end
@@ -2,11 +2,25 @@ module Redd
2
2
  module Client
3
3
  class Unauthenticated
4
4
  module Subreddits
5
- def subreddit(name)
5
+ def subreddit(title)
6
6
  meth = :get
7
- path = "/r/#{name}/about.json"
7
+ path = "/r/#{title}/about.json"
8
8
  object_from_response(meth, path)
9
9
  end
10
+
11
+ def get_subreddits(where = :popular, params = {})
12
+ meth = :get
13
+ path = "/subreddits/#{where}.json"
14
+ object_from_response(meth, path, params)
15
+ end
16
+
17
+ def search_subreddits(query, params = {})
18
+ meth = :get
19
+ path = "/subreddits/search.json"
20
+ params << {q: query}
21
+
22
+ object_from_response(meth, path, params)
23
+ end
10
24
  end
11
25
  end
12
26
  end
@@ -1,8 +1,10 @@
1
1
  require "redd/thing"
2
2
  require "redd/object/listing"
3
3
  require "redd/object/comment"
4
+ require "redd/object/private_message"
4
5
  require "redd/object/submission"
5
6
  require "redd/object/subreddit"
7
+ require "redd/object/user"
6
8
 
7
9
  module Redd
8
10
  module Client
@@ -10,12 +12,16 @@ module Redd
10
12
  module Utilities
11
13
  private
12
14
 
15
+ def extract_attribute(object, attribute)
16
+ object.send(attribute) if object.respond_to?(attribute)
17
+ end
18
+
13
19
  def extract_fullname(object)
14
20
  object.is_a?(String) ? object : extract_attribute(object, :fullname)
15
21
  end
16
22
 
17
- def extract_attribute(object, attribute)
18
- object.send(attribute) if object.respond_to?(attribute)
23
+ def extract_id(object)
24
+ object.is_a?(String) ? object : extract_attribute(object, :id)
19
25
  end
20
26
 
21
27
  # @todo "more"
@@ -25,8 +31,12 @@ module Redd
25
31
  Redd::Object::Listing
26
32
  when "t1"
27
33
  Redd::Object::Comment
34
+ when "t2"
35
+ Redd::Object::User
28
36
  when "t3"
29
37
  Redd::Object::Submission
38
+ when "t4"
39
+ Redd::Object::PrivateMessage
30
40
  when "t5"
31
41
  Redd::Object::Subreddit
32
42
  else
@@ -36,24 +46,30 @@ module Redd
36
46
 
37
47
  def objects_from_listing(thing)
38
48
  thing[:data][:children].map do |child|
39
- get_object_from_body(child)
49
+ object_from_body(child)
40
50
  end
41
51
  end
42
52
 
43
- def get_object_from_body(body)
53
+ def object_from_body(body)
54
+ return nil unless body.is_a?(Hash) && body.has_key?(:kind)
44
55
  object = object_from_kind(body[:kind])
45
56
 
46
57
  if object == Redd::Object::Listing
47
- things = objects_from_listing(body)
48
- object.new(things)
58
+ body[:data][:children] = objects_from_listing(body)
59
+ object.new(body)
49
60
  else
50
61
  object.new(self, body)
51
62
  end
52
63
  end
53
64
 
65
+ def comments_from_response(*args)
66
+ body = request(*args).body[1]
67
+ object_from_body(body)
68
+ end
69
+
54
70
  def object_from_response(*args)
55
71
  body = request(*args).body
56
- get_object_from_body(body)
72
+ object_from_body(body)
57
73
  end
58
74
  end
59
75
  end
@@ -1,8 +1,8 @@
1
1
  module Redd
2
2
  module Client
3
- class Unauthenticated
3
+ class Unauthenticated
4
4
  module Wiki
5
-
5
+
6
6
  end
7
7
  end
8
8
  end
@@ -4,37 +4,52 @@ module Redd
4
4
  module Object
5
5
  # A comment made on links.
6
6
  class Comment < Redd::Thing
7
- # @!attribute [r] subreddit
8
- # @return [String] The name of the subreddit this comment belongs to.
9
- # @todo Convert to a Subreddit object?
10
- attr_reader :subreddit
11
-
12
- # @!attribute [r] parent_id
13
- # @return [String] The id of the parent comment.
14
- # @todo parent - get the parent comment directly.
15
- attr_reader :parent_id
7
+ attr_reader :created_utc
8
+ attr_reader :author
16
9
 
17
- # @!attribute [r] body
18
- # @return [String] The text of the comment in markdown.
19
- attr_reader :body
10
+ attr_reader :edited
11
+ attr_reader :saved
12
+ attr_reader :gilded
20
13
 
21
- # @!attribute [r] body_html
22
- # @return [String] The text of the comment in html.
23
- # @note Be warned: this isn't actual html, but escaped html. So all the
24
- # <'s and >'s are converted to &lt;'s and &gt;'s.
25
- attr_reader :body_html
14
+ attr_reader :ups
15
+ attr_reader :downs
16
+ attr_reader :score
17
+ attr_reader :likes
18
+ attr_reader :controversiality
26
19
 
27
- # @!attribute [r] author_flair_text
28
- # @return [String] The user's flair.
29
- attr_reader :author_flair_text
20
+ attr_reader :banned_by
21
+ attr_reader :approved_by
22
+ attr_reader :score_hidden
23
+ attr_reader :distinguished
24
+ attr_reader :num_reports
30
25
 
31
- # @!attribute [r] author_flair_css_class
32
- # @return [String] The CSS class of the user's flair.
26
+ attr_reader :parent_id
27
+ attr_reader :link_id
28
+ attr_reader :body
29
+ attr_reader :body_html
30
+ attr_reader :author_flair_text
33
31
  attr_reader :author_flair_css_class
34
32
 
35
- # @return [Boolean] Whether the comment is a root rather than a reply.
33
+ alias_method :reports_count, :num_reports
34
+
35
+ def replies
36
+ @replies ||= client.get_replies(self)
37
+ end
38
+
39
+ def subreddit
40
+ @subreddit ||= client.subreddit(@attributes[:subreddit])
41
+ end
42
+
43
+ def created
44
+ @created ||= Time.at(@attributes[:created])
45
+ end
46
+
36
47
  def root?
37
- !parent_id || parent_id == fullname
48
+ !parent_id || parent_id == link_id
49
+ end
50
+
51
+ def gilded?
52
+ gilded > 0
38
53
  end
39
54
  end
40
55
  end
@@ -14,10 +14,15 @@ module Redd
14
14
  # @return [Array] A list of things in the listing.
15
15
  attr_reader :things
16
16
 
17
- # Create a new listing with the given things.
18
- # @param [Array] things A list of things to generate a Listing out of.
19
- def initialize(things)
20
- @things = things
17
+ attr_reader :kind
18
+ attr_reader :after
19
+ attr_reader :before
20
+
21
+ def initialize(listing)
22
+ @kind = listing[:kind]
23
+ @things = listing[:data][:children]
24
+ @after = listing[:data][:after]
25
+ @before = listing[:data][:before]
21
26
  end
22
27
  end
23
28
  end
@@ -0,0 +1,31 @@
1
+ require "redd/thing"
2
+
3
+ module Redd
4
+ module Object
5
+ class PrivateMessage < Redd::Thing
6
+ attr_reader :created_utc
7
+ attr_reader :body
8
+ attr_reader :body_html
9
+ attr_reader :subreddit
10
+ attr_reader :parent_id
11
+ attr_reader :distinguished
12
+ attr_reader :was_comment
13
+ attr_reader :first_message_name
14
+ attr_reader :context
15
+
16
+ attr_reader :dest
17
+ attr_reader :author
18
+
19
+ alias_method :from, :author
20
+ alias_method :to, :author
21
+
22
+ def created
23
+ @created ||= Time.at(@attributes[:created])
24
+ end
25
+
26
+ def replies
27
+ @replies ||= client.objects_from_listing(@attributes[:replies])
28
+ end
29
+ end
30
+ end
31
+ end
@@ -4,91 +4,73 @@ module Redd
4
4
  module Object
5
5
  # A submission made in a subreddit.
6
6
  class Submission < Redd::Thing
7
- # @!attribute [r] title
8
- # @return [String] The title of the submission.
9
- attr_reader :title
10
-
11
- # @!attribute [r] url
12
- # @return [String] The url the submission links to.
13
- attr_reader :url
14
-
15
- # @!attribute [r] num_comments
16
- # @return [Integer] The number of comments the submission has.
17
- attr_reader :num_comments
18
-
19
- # @!attribute [r] domain
20
- # @return [String] The domain the url belongs to.
21
- attr_reader :domain
22
-
23
- # @!attribute [r] subreddit
24
- # @return [String] The name of the subreddit this comment belongs to.
25
- # @todo Convert to a Subreddit object?
26
- attr_reader :subreddit
27
-
28
- # @!attribute [r] is_self?
29
- # @return [Boolean] Whether the submission is a self submission.
30
- attr_reader :self?, :is_self
7
+ attr_reader :created_utc
8
+ attr_reader :author
31
9
 
32
- # @!attribute [r] thumbnail
33
- # @return [String] The url for the thumbnail of the submission.
34
- attr_reader :thumbnail
35
- alias_method :thumbnail_url, :thumbnail
10
+ attr_reader :edited
11
+ attr_reader :saved
12
+ attr_reader :gilded
13
+ attr_reader :clicked
14
+ attr_reader :visited
15
+ attr_reader :stickied
16
+ attr_reader :hidden
36
17
 
37
- # @!attribute [r] selftext
38
- # @return [String] The text of the submission in markdown.
39
- # @note This returns "" if the submission is not a self submission.
40
- attr_reader :selftext
18
+ attr_reader :ups
19
+ attr_reader :downs
20
+ attr_reader :score
21
+ attr_reader :likes
41
22
 
42
- # @!attribute [r] selftext_html
43
- # @return [String] The text of the submission in html.
44
- # @note This returns nil if the submission is not a self submission.
45
- # @note Be warned: this isn't actual html, but escaped html. So all the
46
- # <'s and >'s are converted to &lt;'s and &gt;'s.
47
- attr_reader :selftext_html
23
+ attr_reader :banned_by
24
+ attr_reader :approved_by
25
+ attr_reader :distinguished
26
+ attr_reader :num_reports
48
27
 
49
- # @!attribute [r] link_flair_text
50
- # @return [String] The submission's flair.
51
28
  attr_reader :link_flair_text
52
-
53
- # @!attribute [r] link_flair_css_class
54
- # @return [String] The CSS class of the submission's flair.
55
29
  attr_reader :link_flair_css_class
56
-
57
- # @!attribute [r] author_flair_text
58
- # @return [String] The user's flair.
30
+ attr_reader :author_flair_css_class
59
31
  attr_reader :author_flair_text
60
32
 
61
- # @!attribute [r] author_flair_css_class
62
- # @return [String] The CSS class of the user's flair.
63
- attr_reader :author_flair_css_class
33
+ attr_reader :domain
34
+ attr_reader :media
35
+ attr_reader :media_embed
36
+ attr_reader :selftext
37
+ attr_reader :selftext_html
38
+ attr_reader :secure_media
39
+ attr_reader :secure_media_embed
40
+ attr_reader :over_18
41
+ attr_reader :thumbnail
42
+ attr_reader :is_self
43
+ attr_reader :url
44
+ attr_reader :title
45
+ attr_reader :num_comments
64
46
 
65
- # @!attribute [r] clicked
66
- # @return [Boolean] Whether the user already clicked on the link before.
67
- # @note This only works for users with reddit gold.
68
- attr_reader :clicked
47
+ alias_method :nsfw?, :over_18
48
+ alias_method :self?, :is_self
49
+ alias_method :comments_count, :num_comments
69
50
 
70
- # @!attribute [r] visited
71
- # @return [Boolean] Whether the user already visited on the link before.
72
- # @note I have no idea what the difference between this and {#clicked} is
73
- attr_reader :visited
51
+ def subreddit
52
+ @subreddit ||= client.subreddit(@attributes[:subreddit])
53
+ end
74
54
 
75
- # @!attribute [r] stickied
76
- # @return [Boolean] Whether the submission was stickied in the subreddit.
77
- attr_reader :stickied
55
+ def comments
56
+ @comments ||= client.get_comments(id)
57
+ end
78
58
 
79
- # @!attribute [r] nsfw?
80
- # @return [Boolean] Whether the post is marked as NSFW.
81
- attr_reader :nsfw?, :over_18
59
+ def created
60
+ @created ||= Time.at(@attributes[:created])
61
+ end
82
62
 
83
- # @return [String] The full url to the post on reddit.com.
84
63
  def permalink
85
64
  "http://www.reddit.com" + attributes[:permalink]
86
65
  end
87
66
 
88
- # @return [String] The short url on redd.it.
89
67
  def short_url
90
68
  "http://redd.it/" + id
91
69
  end
70
+
71
+ def gilded?
72
+ gilded > 0
73
+ end
92
74
  end
93
75
  end
94
76
  end
@@ -5,6 +5,67 @@ module Redd
5
5
  # A comment made on links.
6
6
  # @note This model can sure benefit from some lazy-loading...
7
7
  class Subreddit < Redd::Thing
8
+ attr_reader :display_name
9
+ attr_reader :title
10
+
11
+ attr_reader :description
12
+ attr_reader :description_html
13
+
14
+ attr_reader :header_img
15
+ attr_reader :header_title
16
+ attr_reader :header_size
17
+
18
+ attr_reader :user_is_banned
19
+ attr_reader :user_is_contributor
20
+ attr_reader :user_is_moderator
21
+ attr_reader :user_is_subscriber
22
+
23
+ attr_reader :submit_text
24
+ attr_reader :submit_text_html
25
+ attr_reader :submit_link_label
26
+ attr_reader :submit_text_label
27
+
28
+ attr_reader :over18
29
+ attr_reader :accounts_active
30
+ attr_reader :public_traffic
31
+ attr_reader :subscribers
32
+ attr_reader :created_utc
33
+ attr_reader :comment_score_hide_mins
34
+ attr_reader :subreddit_type
35
+ attr_reader :submission_type
36
+
37
+ alias_method :header_image, :header_img
38
+ alias_method :nsfw?, :over18
39
+ alias_method :users_online, :accounts_active
40
+ alias_method :type, :subreddit_type
41
+
42
+ def created
43
+ @created ||= Time.at(@attributes[:created])
44
+ end
45
+
46
+ def url
47
+ "http://reddit.com" + @attributes[:url]
48
+ end
49
+
50
+ def get_hot(*args)
51
+ client.get_hot(display_name, *args)
52
+ end
53
+
54
+ def get_new(*args)
55
+ client.get_new(display_name, *args)
56
+ end
57
+
58
+ def get_random(*args)
59
+ client.get_random(display_name, *args)
60
+ end
61
+
62
+ def get_top(*args)
63
+ client.get_top(display_name, *args)
64
+ end
65
+
66
+ def get_controversial(*args)
67
+ client.get_controversial(display_name, *args)
68
+ end
8
69
  end
9
70
  end
10
71
  end
@@ -0,0 +1,30 @@
1
+ require "redd/thing"
2
+
3
+ module Redd
4
+ module Object
5
+ class User < Redd::Thing
6
+ attr_reader :created_utc
7
+ attr_reader :name
8
+ attr_reader :is_friend
9
+ attr_reader :link_karma
10
+ attr_reader :comment_karma
11
+ attr_reader :gold_creddits
12
+ attr_reader :gold_expiration
13
+ attr_reader :is_gold
14
+ attr_reader :is_mod
15
+ attr_reader :has_verified_email
16
+ attr_reader :has_mod_mail
17
+ attr_reader :has_mail
18
+
19
+ alias_method :has_gold, :is_gold
20
+
21
+ def created
22
+ @created ||= Time.at(@attributes[:created])
23
+ end
24
+
25
+ def fullname
26
+ @fullname ||= "#{kind}_#{id}"
27
+ end
28
+ end
29
+ end
30
+ end
@@ -26,20 +26,21 @@ module Redd
26
26
  # @return [String] The time when the last request took place.
27
27
  attr_reader :last_request_time
28
28
 
29
- def initialize
29
+ # @param [Float, Integer] gap The minimum time between each request.
30
+ def initialize(gap = 2)
30
31
  # Some time ages ago, because we never made a request.
31
32
  @last_request_time = Time.at(0)
33
+ @gap = gap
32
34
  end
33
35
 
34
36
  # Sleep until 2 seconds have passed since the last request and perform the
35
37
  # given request.
36
38
  #
37
- # @param [Float, Integer] gap The minimum time between each request.
38
39
  # @yield A block.
39
40
  # @return The return value of the block.
40
- def after_limit(gap = 2)
41
+ def after_limit
41
42
  seconds_passed = Time.now - @last_request_time
42
- wait_time = gap - seconds_passed
43
+ wait_time = @gap - seconds_passed
43
44
  sleep(wait_time) if wait_time > 0
44
45
  @last_request_time = Time.now
45
46
  yield
@@ -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.0"
4
+ VERSION = "0.1.1"
5
5
  end
data/test.rb ADDED
@@ -0,0 +1,6 @@
1
+ require "redd"
2
+
3
+ c = Redd.client
4
+ s = c.get_info(id: "t3_2cfy08")
5
+
6
+ puts c.get_comments(s)
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.0
4
+ version: 0.1.1
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-07-30 00:00:00.000000000 Z
11
+ date: 2014-08-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -163,12 +163,13 @@ files:
163
163
  - .travis.yml
164
164
  - .yardopts
165
165
  - Gemfile
166
- - LICENSE.txt
166
+ - LICENSE.md
167
167
  - README.md
168
168
  - Rakefile
169
+ - RedditKit.LICENSE.md
170
+ - TODO.md
169
171
  - github/redd.png
170
172
  - lib/redd.rb
171
- - lib/redd/RedditKit.LICENSE.md
172
173
  - lib/redd/base.rb
173
174
  - lib/redd/client/authenticated.rb
174
175
  - lib/redd/client/authenticated/account.rb
@@ -188,14 +189,17 @@ files:
188
189
  - lib/redd/client/unauthenticated/account.rb
189
190
  - lib/redd/client/unauthenticated/links_comments.rb
190
191
  - lib/redd/client/unauthenticated/listing.rb
192
+ - lib/redd/client/unauthenticated/live.rb
191
193
  - lib/redd/client/unauthenticated/subreddits.rb
192
194
  - lib/redd/client/unauthenticated/utilities.rb
193
195
  - lib/redd/client/unauthenticated/wiki.rb
194
196
  - lib/redd/error.rb
195
197
  - lib/redd/object/comment.rb
196
198
  - lib/redd/object/listing.rb
199
+ - lib/redd/object/private_message.rb
197
200
  - lib/redd/object/submission.rb
198
201
  - lib/redd/object/subreddit.rb
202
+ - lib/redd/object/user.rb
199
203
  - lib/redd/rate_limit.rb
200
204
  - lib/redd/response/parse_json.rb
201
205
  - lib/redd/response/raise_error.rb
@@ -204,6 +208,7 @@ files:
204
208
  - redd.gemspec
205
209
  - spec/redd_spec.rb
206
210
  - spec/spec_helper.rb
211
+ - test.rb
207
212
  homepage: ''
208
213
  licenses:
209
214
  - MIT