Permalink
Browse files

Add class to mark multiple messages read/unread

  • Loading branch information...
1 parent 0eef96f commit d28f18f1f7646b44762829e535bbfaf9756d67a0 james cook committed Nov 21, 2010
View
@@ -1,4 +1,4 @@
-== Ruby Reddit Client v0.2.5
+== Ruby Reddit Client v0.2.6
Tested with ruby 1.8.7 & 1.9.2
== Usage
View
@@ -1 +1 @@
-0.2.5
+0.2.6
@@ -0,0 +1,14 @@
+Feature: Message Group
+ I want to be able to mark multiple messages as read or unread
+
+ Scenario: Mark multiple messages read
+ Given I am logged in
+ And I have unread messages
+ When I mark all unread messages read
+ Then I should have no unread messages
+
+ Scenario: Mark multiple messages unread
+ Given I am logged in
+ And I have unread messages
+ When I mark all read messages unread
+ Then I should have no read messages
@@ -0,0 +1,30 @@
+Before do
+ load_server_config
+ Reddit::Api.base_uri @address
+ Reddit::MessageGroup.base_uri @address
+ Reddit::Base.instance_variable_set("@throttle_duration", 0)
+ @api = Reddit::Api.new @user, @pass
+ @api.login
+end
+
+When /^I mark all unread messages read$/ do
+ group = Reddit::MessageGroup.new
+ messages = @api.unread_messages
+ result = group.mark_read messages
+ result.should be true
+end
+
+When /^I mark all read messages unread$/ do
+ group = Reddit::MessageGroup.new
+ messages = @api.received_messages
+ result = group.mark_unread messages
+ result.should be true
+end
+
+Then /^I should have no unread messages$/ do
+ @api.unread_messages.should == []
+end
+
+Then /^I should have no read messages$/ do
+ @api.received_messages.select{|m| !m.was_comment}.map(&:read?).uniq.should == [false]
+end
View
@@ -16,4 +16,5 @@ module Reddit
require "ruby_reddit_api/submission"
require "ruby_reddit_api/comment"
require "ruby_reddit_api/message"
+require "ruby_reddit_api/message_group"
@@ -15,6 +15,14 @@ def id
"#{kind}_#{@id}"
end
+ def unread?
+ @new == true
+ end
+
+ def read?
+ @new == false
+ end
+
# The author of the message. The data is lazy-loaded and cached on the message
# @return [Reddit::User]
def author
@@ -0,0 +1,39 @@
+# @author James Cook
+module Reddit
+ class MessageGroup < Thing
+ include JsonListing
+ attr_reader :debug
+
+ def initialize
+ @debug = StringIO.new
+ end
+
+ def mark_read(messages)
+ mark messages, "read"
+ end
+
+ def mark_unread(messages)
+ mark messages, "unread"
+ end
+
+ protected
+ def mark(messages, action)
+ ids = ids(messages)
+ action = action == "read" ? "read_message" : "unread_message"
+ resp = self.class.post("/api/#{action}", {:body => {:id => ids.join(','), :uh => modhash }, :headers => base_headers, :debug_output => @debug })
+ resp.code == 200
+ end
+
+ def ids(messages)
+ if messages.is_a?(Array)
+ if messages.first.is_a?(Reddit::Message)
+ return messages.map{|m| m.id }
+ else
+ return messages # assume array of String
+ end
+ else
+ return [ messages.id ]
+ end
+ end
+ end
+end

0 comments on commit d28f18f

Please sign in to comment.