redd 0.1.1 → 0.1.2

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: 546ebcdeb092101a90012a2f2a3d088cdae13259
4
- data.tar.gz: c0a9b544cd6e0786cf99d0d1fea4336929068ea6
3
+ metadata.gz: 737c5bb6b96b0507169cd88c0d2b6d7117241dc6
4
+ data.tar.gz: 277672cec830f047ffe1b17082325da5bf88fc46
5
5
  SHA512:
6
- metadata.gz: 213c006802d87c57bcdd4236f793c7def0eeadb47d5991e3f7d2894dba3824373b9c7c6fe5ba817a65a9e0de54b12b2047c5a0200bc1487cfc11ef13791f9b34
7
- data.tar.gz: a6ca604d6a3c55a9dd1dec45edc9537b5a2b5f153447c8d6ce05b49d721f4e12c6204af765cfc8c6d96f4848559757b8a21e38ea7361aef8f5ab5c14831b2ac1
6
+ metadata.gz: ae62116a74fcb27b218e55024133082861d63ecc5b8cba99b1a46d86d94cdb9350e59104fef2ed2963d13f31d5e7dfcf5a83f4d501adb643bb86e245b37a554b
7
+ data.tar.gz: 3ae8c2e8870bcdcc0e2b3705073e1b4ae46aacc735ae662d9fedcdd07618351aa53982e911f371b5432e186dadfdab388867f248d798a57a2aa5c5ec11c6a174
@@ -2,7 +2,23 @@ module Redd
2
2
  module Client
3
3
  class Authenticated
4
4
  module Flair
5
-
5
+ def get_flair_list(subreddit = nil, params = {})
6
+ name = extract_attribute(subreddit, :display_name) if subreddit
7
+
8
+ path = "/api/flairlist.json"
9
+ path = path.prepend("/r/#{name}") if name
10
+
11
+ get(path, params)[:users]
12
+ end
13
+
14
+ # @see http://ruby-doc.org/core-1.9.3/String.html#method-i-casecmp
15
+ def get_flair(user, subreddit = nil)
16
+ username = extract_attribute(user, :name)
17
+ options = {name: username}
18
+
19
+ flair = get_flair_list(subreddit, options).first
20
+ flair if flair[:user].casecmp(username.downcase) == 0
21
+ end
6
22
  end
7
23
  end
8
24
  end
@@ -2,111 +2,63 @@ module Redd
2
2
  module Client
3
3
  class Authenticated
4
4
  module LinksComments
5
+ # @todo return the edited object.
5
6
  def add_comment(thing, text, return_element = true)
6
7
  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)
8
+ post "/api/comment", api_type: "json", text: text, thing_id: fullname
14
9
  end
15
10
 
16
11
  alias_method :reply, :add_comment
17
12
 
18
13
  def delete(thing)
19
14
  fullname = extract_fullname(thing)
20
-
21
- meth = :post
22
- path = "/api/del"
23
- params = {id: fullname}
24
-
25
- send(meth, path, params)
15
+ post "/api/del", id: fullname
26
16
  end
27
17
 
18
+ # @todo return the edited object.
28
19
  def edit(thing, text)
29
20
  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)
21
+ post "/api/editusertext",
22
+ api_type: "json", text: text, thing_id: fullname
37
23
  end
38
24
 
39
25
  def hide(thing)
40
26
  fullname = extract_fullname(thing)
41
-
42
- meth = :post
43
- path = "/api/hide"
44
- params = {id: fullname}
45
-
46
- send(meth, path, params)
47
- end
27
+ post "/api/hide", id: fullname
28
+ end
48
29
 
49
30
  def unhide(thing)
50
31
  fullname = extract_fullname(thing)
51
-
52
- meth = :post
53
- path = "/api/unhide"
54
- params = {id: fullname}
55
-
56
- send(meth, path, params)
32
+ post "/api/unhide", id: fullname
57
33
  end
58
34
 
59
35
  def mark_as_nsfw(thing)
60
36
  fullname = extract_fullname(thing)
61
-
62
- meth = :post
63
- path = "/api/marknsfw"
64
- params = {id: fullname}
65
-
66
- send(meth, path, params)
37
+ post "/api/marknsfw", id: fullname
67
38
  end
68
39
 
69
40
  def unmark_as_nsfw(thing)
70
41
  fullname = extract_fullname(thing)
71
-
72
- meth = :post
73
- path = "/api/unmarknsfw"
74
- params = {id: fullname}
75
-
76
- send(meth, path, params)
42
+ post "/api/unmarknsfw", id: fullname
77
43
  end
78
44
 
79
45
  alias_method :mark_as_safe, :unmark_as_nsfw
80
46
 
81
47
  def report(thing)
82
48
  fullname = extract_fullname(thing)
83
-
84
- meth = :post
85
- path = "/api/report"
86
- params = {id: fullname}
87
-
88
- send(meth, path, params)
49
+ post "/api/report", id: fullname
89
50
  end
90
51
 
91
52
  def save(thing, category = nil)
92
53
  fullname = extract_fullname(thing)
93
-
94
- meth = :post
95
- path = "/api/save"
96
54
  params = {id: fullname}
97
55
  params << {category: category} if category
98
-
99
- send(meth, path, params)
56
+ post "/api/save", params
100
57
  end
101
58
 
102
59
  def unsave(thing)
103
60
  fullname = extract_fullname(thing)
104
-
105
- meth = :post
106
- path = "/api/unsave"
107
- params = {id: fullname}
108
-
109
- send(meth, path, params)
61
+ post "/api/unsave", id: fullname
110
62
  end
111
63
 
112
64
  def upvote(thing)
@@ -125,12 +77,7 @@ module Redd
125
77
 
126
78
  def vote(thing, direction)
127
79
  fullname = extract_fullname(thing)
128
-
129
- meth = :post
130
- path = "/api/vote"
131
- params = {id: fullname, dir: direction}
132
-
133
- send(meth, path, params)
80
+ post "/api/vote", id: fullname, dir: direction
134
81
  end
135
82
  end
136
83
  end
@@ -2,7 +2,75 @@ module Redd
2
2
  module Client
3
3
  class Authenticated
4
4
  module Moderation
5
-
5
+ def approve(thing)
6
+ fullname = extract_fullname(thing)
7
+ post "/api/approve", id: fullname
8
+ end
9
+
10
+ def remove(thing)
11
+ fullname = extract_fullname(thing)
12
+ post "/api/remove", id: fullname
13
+ end
14
+
15
+ def distinguish(thing, how = :yes)
16
+ fullname = extract_fullname(thing)
17
+ post "/api/distinguish", api_type: "json", id: fullname, how: how
18
+ end
19
+
20
+ def undistinguish(thing)
21
+ distinguish(thing, :no)
22
+ end
23
+
24
+ def accept_moderator_invite(subreddit)
25
+ name = extract_attribute(subreddit, :display_name)
26
+ post, "/r/#{name}/api/accept_moderator_invite", api_type: "json"
27
+ end
28
+
29
+ def leave_contributor_status(subreddit)
30
+ fullname = extract_fullname(subreddit)
31
+ post "/api/leavecontributor", id: fullname
32
+ end
33
+
34
+ def leave_moderator_status(subreddit)
35
+ fullname = extract_fullname(subreddit)
36
+ post "/api/leavemoderator", id: fullname
37
+ end
38
+
39
+ def ignore_reports(thing)
40
+ fullname = extract_fullname(subreddit)
41
+ post "/api/ignore_reports", id: fullname
42
+ end
43
+
44
+ def unignore_reports(thing)
45
+ fullname = extract_fullname(subreddit)
46
+ post "/api/unignore_reports", id: fullname
47
+ end
48
+
49
+ def get_reports(*args)
50
+ get_submissions(:reports, *args)
51
+ end
52
+
53
+ def get_spam(*args)
54
+ get_submissions(:spam, *args)
55
+ end
56
+
57
+ def get_modqueue(*args)
58
+ get_submissions(:modqueue, *args)
59
+ end
60
+
61
+ def get_unmoderated(*args)
62
+ get_submissions(:unmoderated, *args)
63
+ end
64
+
65
+ private
66
+
67
+ def get_submissions(type, subreddit = nil, params = {})
68
+ subreddit_name = extract_attribute(subreddit, :display_name)
69
+ path = "/about/#{type}.json"
70
+ path = path.prepend("/r/#{subreddit_name}") if subreddit_name
71
+
72
+ object_from_response :get, path, params
73
+ end
6
74
  end
7
75
  end
8
76
  end
@@ -4,35 +4,30 @@ module Redd
4
4
  module PrivateMessages
5
5
  def block_message(message)
6
6
  fullname = extract_fullname(message)
7
- send(:post, "/api/block", id: fullname)
7
+ post "/api/block", id: fullname
8
8
  end
9
9
 
10
10
  def compose_message(to, subject, text, captcha = nil, identifier = nil)
11
- meth = :post
12
- path = "/api/compose"
13
11
  params = {
14
12
  api_type: "json", to: to.to_s, subject: subject, text: text
15
13
  }
16
14
  params << {captcha: captcha, iden: identifier} if captcha
17
15
 
18
- send(meth, path, params)
16
+ post "/api/compose", params
19
17
  end
20
18
 
21
19
  def mark_as_read(message)
22
20
  fullname = extract_fullname(message)
23
- send(:post, "/api/read_message", id: fullname)
21
+ post "/api/read_message", id: fullname
24
22
  end
25
23
 
26
24
  def mark_as_unread(message)
27
25
  fullname = extract_fullname(message)
28
- send(:post, "/api/unread_message", id: fullname)
26
+ post "/api/unread_message", id: fullname
29
27
  end
30
28
 
31
29
  def messages(category = "inbox", params = {})
32
- meth = :get
33
- path = "/message/#{category}.json"
34
-
35
- object_from_response(meth, path, params)
30
+ object_from_response :get, "/message/#{category}.json", params
36
31
  end
37
32
  end
38
33
  end
@@ -11,26 +11,20 @@ module Redd
11
11
  end
12
12
 
13
13
  def get_subreddits(where = :subscriber, params = {})
14
- meth = :get
15
14
  path =
16
15
  if [:popular, :new].include?(where)
17
16
  "/subreddits/#{where}.json"
18
17
  elsif [:subscriber, :contributor, :moderator].include?(where)
19
18
  "/subreddits/mine/#{where}.json"
20
19
  end
21
- object_from_response(meth, path, params)
20
+ object_from_response(:get, path, params)
22
21
  end
23
22
 
24
23
  private
25
24
 
26
25
  def edit_subscription(action, subreddit)
27
26
  fullname = extract_fullname(subreddit)
28
-
29
- meth = :post
30
- path = "/api/subscribe"
31
- params = {action: action, sr: fullname}
32
-
33
- send(meth, path, params)
27
+ post "/api/subscribe", action: action, sr: fullname
34
28
  end
35
29
  end
36
30
  end
@@ -9,15 +9,21 @@ module Redd
9
9
  # The Client used to connect without needing login credentials.
10
10
  class Unauthenticated
11
11
  require "redd/client/unauthenticated/account"
12
+ require "redd/client/unauthenticated/captcha"
12
13
  require "redd/client/unauthenticated/links_comments"
13
14
  require "redd/client/unauthenticated/listing"
15
+ require "redd/client/unauthenticated/live"
16
+ require "redd/client/unauthenticated/moderation"
14
17
  require "redd/client/unauthenticated/subreddits"
15
18
  require "redd/client/unauthenticated/utilities"
16
19
  require "redd/client/unauthenticated/wiki"
17
20
 
18
21
  include Redd::Client::Unauthenticated::Account
22
+ include Redd::Client::Unauthenticated::Captcha
19
23
  include Redd::Client::Unauthenticated::LinksComments
20
24
  include Redd::Client::Unauthenticated::Listing
25
+ include Redd::Client::Unauthenticated::Live
26
+ include Redd::Client::Unauthenticated::Moderation
21
27
  include Redd::Client::Unauthenticated::Subreddits
22
28
  include Redd::Client::Unauthenticated::Utilities
23
29
  include Redd::Client::Unauthenticated::Wiki
@@ -70,10 +76,10 @@ module Redd
70
76
  # @param [#to_sym] method The HTTP verb to use.
71
77
  # @param [String] path The path under the api endpoint to request from.
72
78
  # @param [Hash] params The additional parameters to send (defualt: {}).
73
- # @return [Faraday::Response] A Faraday Response.
79
+ # @return [String] The response body.
74
80
  def request(method, path, params = {})
75
81
  rate_limit.after_limit do
76
- connection.send(method.to_sym, path, params)
82
+ connection.send(method.to_sym, path, params).body
77
83
  end
78
84
  end
79
85
 
@@ -0,0 +1,20 @@
1
+ module Redd
2
+ module Client
3
+ class Unauthenticated
4
+ module Captcha
5
+ def needs_captcha?
6
+ get "/api/needs_captcha.json"
7
+ end
8
+
9
+ def new_captcha
10
+ response = get "/api/new_captcha", api_type: "json"
11
+ response[:json][:data][:iden]
12
+ end
13
+
14
+ def captcha_url(identifier)
15
+ "http://www.reddit.com/captcha/#{identifier}.png"
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -5,17 +5,12 @@ module Redd
5
5
  # @note Reddit does accept a subreddit, but with fullnames and urls, I
6
6
  # assumed that was unnecessary.
7
7
  def get_info(params = {})
8
- meth = :get
9
- path = "/api/info.json"
10
- object_from_response(meth, path, params)
8
+ object_from_response :get, "/api/info.json"
11
9
  end
12
10
 
13
11
  def get_comments(submission)
14
12
  id = extract_id(submission)
15
-
16
- meth = :get
17
- path = "/comments/#{id}.json"
18
- comments_from_response(meth, path)
13
+ comments_from_response :get, "/comments/#{id}.json"
19
14
  end
20
15
 
21
16
  def get_replies(comment)
@@ -23,6 +18,26 @@ module Redd
23
18
  return [] unless replies.is_a?(Hash) and replies.has_key?(:kind)
24
19
  object_from_body(replies)
25
20
  end
21
+
22
+ # FIX THIS ASAP
23
+ def replace_morecomments(morecomments)
24
+ parent_id = morecomments.parent_id
25
+ link_id =
26
+ if parent_id.start_with?("t1_")
27
+ get_info(id: parent_id).first.link_id
28
+ elsif parent_id.start_with?("t3_")
29
+ parent_id
30
+ end
31
+
32
+ meth = :post
33
+ path = "/api/morechildren"
34
+ params = {
35
+ api_type: "json", link_id: link_id,
36
+ children: morecomments.children.join(",")
37
+ }
38
+
39
+ send(meth, path, params)
40
+ end
26
41
  end
27
42
  end
28
43
  end
@@ -4,10 +4,7 @@ module Redd
4
4
  module Listing
5
5
  def by_id(*fullnames)
6
6
  names = fullnames.join(",")
7
-
8
- meth = :get
9
- path = "/by_id/#{names}.json"
10
- object_from_response(meth, path)
7
+ object_from_response :get, "/by_id/#{names}.json"
11
8
  end
12
9
 
13
10
  def get_hot(*args)
@@ -33,10 +30,12 @@ module Redd
33
30
  private
34
31
 
35
32
  def get_listing(type, subreddit = nil, params = {})
36
- meth = :get
33
+ name = extract_attribute(subreddit, :display_name) if subreddit
34
+
37
35
  path = "/#{type}.json"
38
- path = path.prepend("/r/#{subreddit}") if subreddit
39
- object_from_response(meth, path, params)
36
+ path = path.prepend("/r/#{name}") if name
37
+
38
+ object_from_response :get, path, params
40
39
  end
41
40
  end
42
41
  end
@@ -0,0 +1,8 @@
1
+ module Redd
2
+ module Client
3
+ class Unauthenticated
4
+ module Live
5
+ end
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,18 @@
1
+ module Redd
2
+ module Client
3
+ class Unauthenticated
4
+ module Moderation
5
+ def stylesheet_url(subreddit = nil)
6
+ path = "/stylesheet"
7
+ path = path.prepend("/r/#{subreddit}") if subreddit
8
+
9
+ get(path).headers[:location]
10
+ end
11
+
12
+ def stylesheet(subreddit = nil)
13
+ get stylesheet_url(subreddit)
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -3,23 +3,16 @@ module Redd
3
3
  class Unauthenticated
4
4
  module Subreddits
5
5
  def subreddit(title)
6
- meth = :get
7
- path = "/r/#{title}/about.json"
8
- object_from_response(meth, path)
6
+ object_from_response :get, "/r/#{title}/about.json"
9
7
  end
10
8
 
11
9
  def get_subreddits(where = :popular, params = {})
12
- meth = :get
13
- path = "/subreddits/#{where}.json"
14
- object_from_response(meth, path, params)
10
+ object_from_response :get, "/subreddits/#{where}.json", params
15
11
  end
16
12
 
17
13
  def search_subreddits(query, params = {})
18
- meth = :get
19
- path = "/subreddits/search.json"
20
14
  params << {q: query}
21
-
22
- object_from_response(meth, path, params)
15
+ object_from_response :get, "/subreddits/search.json", params
23
16
  end
24
17
  end
25
18
  end
@@ -1,6 +1,7 @@
1
1
  require "redd/thing"
2
2
  require "redd/object/listing"
3
3
  require "redd/object/comment"
4
+ require "redd/object/more_comments"
4
5
  require "redd/object/private_message"
5
6
  require "redd/object/submission"
6
7
  require "redd/object/subreddit"
@@ -13,22 +14,28 @@ module Redd
13
14
  private
14
15
 
15
16
  def extract_attribute(object, attribute)
16
- object.send(attribute) if object.respond_to?(attribute)
17
+ case object
18
+ when ::String
19
+ object
20
+ else
21
+ object.send(attribute)
22
+ end
17
23
  end
18
24
 
19
25
  def extract_fullname(object)
20
- object.is_a?(String) ? object : extract_attribute(object, :fullname)
26
+ extract_attribute(object, :fullname)
21
27
  end
22
28
 
23
29
  def extract_id(object)
24
- object.is_a?(String) ? object : extract_attribute(object, :id)
30
+ extract_attribute(object, :id)
25
31
  end
26
32
 
27
- # @todo "more"
28
33
  def object_from_kind(kind) # rubocop:disable Style/MethodLength
29
34
  case kind
30
35
  when "Listing"
31
36
  Redd::Object::Listing
37
+ when "more"
38
+ Redd::Object::MoreComments
32
39
  when "t1"
33
40
  Redd::Object::Comment
34
41
  when "t2"
@@ -63,12 +70,12 @@ module Redd
63
70
  end
64
71
 
65
72
  def comments_from_response(*args)
66
- body = request(*args).body[1]
73
+ body = request(*args)[1]
67
74
  object_from_body(body)
68
75
  end
69
76
 
70
77
  def object_from_response(*args)
71
- body = request(*args).body
78
+ body = request(*args)
72
79
  object_from_body(body)
73
80
  end
74
81
  end
@@ -0,0 +1,9 @@
1
+ module Redd
2
+ module Object
3
+ class MoreComments < Redd::Base
4
+ attr_reader :count
5
+ attr_reader :parent_id
6
+ attr_reader :children
7
+ end
8
+ end
9
+ end
@@ -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.1"
4
+ VERSION = "0.1.2"
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.1
4
+ version: 0.1.2
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-03 00:00:00.000000000 Z
11
+ date: 2014-08-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -187,15 +187,18 @@ files:
187
187
  - lib/redd/client/oauth2.rb
188
188
  - lib/redd/client/unauthenticated.rb
189
189
  - lib/redd/client/unauthenticated/account.rb
190
+ - lib/redd/client/unauthenticated/captcha.rb
190
191
  - lib/redd/client/unauthenticated/links_comments.rb
191
192
  - lib/redd/client/unauthenticated/listing.rb
192
193
  - lib/redd/client/unauthenticated/live.rb
194
+ - lib/redd/client/unauthenticated/moderation.rb
193
195
  - lib/redd/client/unauthenticated/subreddits.rb
194
196
  - lib/redd/client/unauthenticated/utilities.rb
195
197
  - lib/redd/client/unauthenticated/wiki.rb
196
198
  - lib/redd/error.rb
197
199
  - lib/redd/object/comment.rb
198
200
  - lib/redd/object/listing.rb
201
+ - lib/redd/object/more_comments.rb
199
202
  - lib/redd/object/private_message.rb
200
203
  - lib/redd/object/submission.rb
201
204
  - lib/redd/object/subreddit.rb