Class: Reddit::Message

Inherits:
Thing show all
Includes:
JsonListing
Defined in:
lib/ruby_reddit_api/message.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#last_action

Instance Method Summary collapse

Methods included from JsonListing

included

Methods inherited from Thing

#report, #toggle

Methods inherited from Base

base_headers, #base_headers, #cookie, #logged_in?, #login, #logout, #modhash, #read, #user, #user_agent, user_agent, #user_id

Constructor Details

#initialize(json) ⇒ Message

Returns a new instance of Message.



7
8
9
10
# File 'lib/ruby_reddit_api/message.rb', line 7

def initialize(json)
  parse(json)
  @debug    = StringIO.new
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



6
7
8
# File 'lib/ruby_reddit_api/message.rb', line 6

def body
  @body
end

#body_htmlObject (readonly)

Returns the value of attribute body_html.



6
7
8
# File 'lib/ruby_reddit_api/message.rb', line 6

def body_html
  @body_html
end

#contextObject (readonly)

Returns the value of attribute context.



6
7
8
# File 'lib/ruby_reddit_api/message.rb', line 6

def context
  @context
end

#createdObject (readonly)

Returns the value of attribute created.



6
7
8
# File 'lib/ruby_reddit_api/message.rb', line 6

def created
  @created
end

#created_utcObject (readonly)

Returns the value of attribute created_utc.



6
7
8
# File 'lib/ruby_reddit_api/message.rb', line 6

def created_utc
  @created_utc
end

#debugObject (readonly)

Returns the value of attribute debug.



6
7
8
# File 'lib/ruby_reddit_api/message.rb', line 6

def debug
  @debug
end

#destObject (readonly)

Returns the value of attribute dest.



6
7
8
# File 'lib/ruby_reddit_api/message.rb', line 6

def dest
  @dest
end

#first_messageObject (readonly)

Returns the value of attribute first_message.



6
7
8
# File 'lib/ruby_reddit_api/message.rb', line 6

def first_message
  @first_message
end

#kindObject (readonly)

Returns the value of attribute kind.



6
7
8
# File 'lib/ruby_reddit_api/message.rb', line 6

def kind
  @kind
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/ruby_reddit_api/message.rb', line 6

def name
  @name
end

#parent_idObject (readonly)

Returns the value of attribute parent_id.



6
7
8
# File 'lib/ruby_reddit_api/message.rb', line 6

def parent_id
  @parent_id
end

#repliesObject (readonly)

Returns the value of attribute replies.



6
7
8
# File 'lib/ruby_reddit_api/message.rb', line 6

def replies
  @replies
end

#subjectObject (readonly)

Returns the value of attribute subject.



6
7
8
# File 'lib/ruby_reddit_api/message.rb', line 6

def subject
  @subject
end

#subredditObject (readonly)

Returns the value of attribute subreddit.



6
7
8
# File 'lib/ruby_reddit_api/message.rb', line 6

def subreddit
  @subreddit
end

#was_commentObject (readonly)

Returns the value of attribute was_comment.



6
7
8
# File 'lib/ruby_reddit_api/message.rb', line 6

def was_comment
  @was_comment
end

Instance Method Details

#authorReddit::User

The author of the message. The data is lazy-loaded and cached on the message

Returns:



28
29
30
# File 'lib/ruby_reddit_api/message.rb', line 28

def author
  @author_data ||= read("/user/#{@author}/about.json", :handler => "User")
end

#idString

The reddit ID of this message

Returns:

  • (String)


14
15
16
# File 'lib/ruby_reddit_api/message.rb', line 14

def id
  "#{kind}_#{@id}"
end

#inspectObject



49
50
51
# File 'lib/ruby_reddit_api/message.rb', line 49

def inspect
  "<Reddit::Message '#{short_body}'>"
end

#mark_readObject



44
45
46
47
# File 'lib/ruby_reddit_api/message.rb', line 44

def mark_read
  resp = self.class.post("/api/read_message", {:body => {:id => id, :uh => modhash }, :headers => base_headers, :debug_output => @debug })
  resp.code == 200
end

#mark_unreadObject



39
40
41
42
# File 'lib/ruby_reddit_api/message.rb', line 39

def mark_unread
  resp = self.class.post("/api/unread_message", {:body => {:id => id, :uh => modhash}, :headers => base_headers, :debug_output => @debug })
  resp.code == 200
end

#read?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/ruby_reddit_api/message.rb', line 22

def read?
  @new == false
end

#reply(text) ⇒ true, false

Reply to this message

Returns:

  • (true, false)


34
35
36
37
# File 'lib/ruby_reddit_api/message.rb', line 34

def reply(text)
  resp = self.class.post("/api/comment", {:body => {:thing_id => id, :text => text, :uh => modhash, :r => subreddit }, :headers => base_headers, :debug_output => @debug })
  resp.code == 200
end

#short_bodyString

Trimmed comment body suitable for #inspect

Returns:

  • (String)


55
56
57
58
59
60
61
# File 'lib/ruby_reddit_api/message.rb', line 55

def short_body
  if body.size > 15
    body[0..15]
  else
    body
  end
end

#unread?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/ruby_reddit_api/message.rb', line 18

def unread?
  @new == true
end