Skip to content
This repository has been archived by the owner on Oct 29, 2023. It is now read-only.

Commit

Permalink
Add class to mark multiple messages read/unread
Browse files Browse the repository at this point in the history
  • Loading branch information
james cook committed Nov 21, 2010
1 parent 0eef96f commit d28f18f
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.rdoc
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion VERSION
@@ -1 +1 @@
0.2.5
0.2.6
14 changes: 14 additions & 0 deletions features/message_group.feature
@@ -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
30 changes: 30 additions & 0 deletions features/step_definitions/message_group_steps.rb
@@ -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
1 change: 1 addition & 0 deletions lib/ruby_reddit_api.rb
Expand Up @@ -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"

8 changes: 8 additions & 0 deletions lib/ruby_reddit_api/message.rb
Expand Up @@ -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
Expand Down
39 changes: 39 additions & 0 deletions lib/ruby_reddit_api/message_group.rb
@@ -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.