ruby_reddit_api 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
data/README CHANGED
@@ -1,4 +1,4 @@
1
- Ruby Reddit Client v0.2.1
1
+ Ruby Reddit Client v0.2.2
2
2
  ==================
3
3
  Tested with ruby 1.9.2
4
4
 
@@ -112,6 +112,7 @@ Then /^I should be able to indistinguish the submission$/ do
112
112
  end
113
113
 
114
114
  Then /^I should be able to see the comments$/ do
115
+ @submission.instance_variable_set("@last_action", Time.now - 60) if @submission
115
116
  comments = @submission.comments
116
117
  comments.size.should > 0
117
118
  end
@@ -3,14 +3,28 @@ module Reddit
3
3
  class Comment < Thing
4
4
 
5
5
  include JsonListing
6
- attr_reader :body, :subreddit_id, :name, :created, :downs, :author, :created_utc, :body_html, :link_id, :parent_id, :likes, :num_comments, :subreddit, :ups, :debug, :kind
7
- def initialize(json)
8
- parse(json)
6
+ attr_reader :body, :subreddit_id, :name, :created, :downs, :author, :created_utc, :body_html, :link_id, :parent_id, :likes, :num_comments, :subreddit, :ups, :debug, :kind, :replies
7
+ def initialize(json, options={})
8
+ mode = options.fetch(:mode){ :json }
9
+ if mode == :json
10
+ parse(json)
11
+ end
12
+
13
+ if mode == :replies
14
+ json.keys.each do |key|
15
+ instance_variable_set("@#{key}", json[key])
16
+ end
17
+ end
18
+
19
+ if replies.is_a?(Hash)
20
+ handle_replies(replies)
21
+ end
22
+
9
23
  @debug = StringIO.new
10
24
  end
11
25
 
12
26
  def inspect
13
- "<Reddit::Comment author='#{@author}' body='#{short_body}'>"
27
+ "<Reddit::Comment author='#{@author}' body='#{short_body}' replies='#{replies.size}' ups='#{ups}' downs='#{downs}'>"
14
28
  end
15
29
 
16
30
  # @return [String]
@@ -79,7 +93,7 @@ module Reddit
79
93
  # Trimmed comment body suitable for #inspect
80
94
  # @return [String]
81
95
  def short_body
82
- str = body.to_s
96
+ str = body.to_s.strip
83
97
  if str.size > 15
84
98
  str[0..15] + "..."
85
99
  else
@@ -89,8 +103,20 @@ module Reddit
89
103
 
90
104
  def add_distinction(verb)
91
105
  resp=self.class.post("/api/distinguish/#{verb}", {:body => {:id => id, :uh => modhash, :r => subreddit, :executed => "distinguishing..."}, :headers => base_headers, :debug_output => @debug })
92
- puts resp.headers
93
106
  resp.code == 200
94
107
  end
108
+
109
+ # Parse nested comment hashes into a Comment
110
+ # @param [Hash] JSON containing reply data
111
+ # @return [true]
112
+ def handle_replies(data)
113
+ dup = data.dup
114
+ _kind, data = dup["kind"], dup["data"]
115
+ modhash, children = data["modhash"], data["children"]
116
+ if children.is_a?(Array)
117
+ @replies = children.map{|reply| Comment.new(reply["data"], :mode => :replies) }
118
+ end
119
+ true
120
+ end
95
121
  end
96
122
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 2
8
- - 1
9
- version: 0.2.1
8
+ - 2
9
+ version: 0.2.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - James Cook
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-10-06 00:00:00 -04:00
17
+ date: 2010-10-10 00:00:00 -04:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency