class LiveJournal::Entry

Constants

KNOWN_EXTRA_PROPS

A list of extra properties we're aware of but don't wrap explicitly. Upon retrieval stored in the props hash. See: www.livejournal.com/doc/server/ljp.csp.proplist.html

Attributes

allowmask[RW]
anum[RW]
backdated[RW]
comments[RW]
event[RW]
give_features[RW]
interface[RW]
itemid[RW]
location[RW]
mood[RW]
moodid[RW]
music[RW]
pickeyword[RW]
preformatted[RW]
props[RW]

A hash of any leftover properties (including those in KNOWN_EXTRA_PROPS) that aren't explicitly supported by ljrb. (See the Request::GetEvents#new for details.)

screening[RW]
security[RW]
subject[RW]
taglist[RW]
time[R]

Public Class Methods

new() click to toggle source
# File lib/livejournal/entry.rb, line 71
def initialize
  @subject = nil
  @event = nil
  @moodid = nil
  @mood = nil
  @music = nil
  @location = nil
  @taglist = []
  @pickeyword = nil
  @preformatted = false
  @backdated = false
  @comments = :normal
  @security = :public
  @allowmask = nil
  @screening = :default
  @give_features = nil
  @props = {}
end

Public Instance Methods

==(other) click to toggle source
# File lib/livejournal/entry.rb, line 90
def ==(other)
  return false if self.class != other.class
  
  [:subject, :event, :moodid,
   :mood, :music, :location, :taglist, :pickeyword,
   :preformatted, :backdated, :comments, :security, :allowmask,
   :screening, :props].each do |attr|
    return false if send(attr) != other.send(attr)
  end
  # compare time fields one-by-one because livejournal ignores the
  # "seconds" field.
  [:year, :mon, :day, :hour, :min, :zone].each do |attr|
    return false if @time.send(attr) != other.time.send(attr)
  end
  return true
end
add_to_request(req) click to toggle source
# File lib/livejournal/entry.rb, line 220
def add_to_request req
  req['event'] = self.event
  req['lineendings'] = 'unix'
  req['subject'] = self.subject

  case self.security
  when :public
    req['security'] = 'public'
  when :friends
    req['security'] = 'usemask'
    req['allowmask'] = 1
  when :private
    req['security'] = 'private'
  when :custom
    req['security'] = 'usemask'
    req['allowmask'] = self.allowmask
  end

  req['give_features']  = self.give_features ? 1 : 0

  if req['mode'] == 'postevent' && self.time.nil?
    self.time = Time.now.gmtime
  end

  if self.time
    req['year'], req['mon'], req['day'] =
        self.time.year, self.time.mon, self.time.day
    req['hour'], req['min'] = self.time.hour, self.time.min
  end

  { 'current_mood' => self.mood,
    'current_moodid' => self.moodid,
    'current_music' => self.music,
    'current_location' => self.location,
    'picture_keyword' => self.pickeyword,
    'taglist' => self.taglist.join(', '),
    'opt_preformatted' => self.preformatted ? 1 : 0,
    'opt_nocomments' => self.comments == :none ? 1 : 0,
    'opt_noemail' => self.comments == :noemail ? 1 : 0,
    'opt_backdated' => self.backdated ? 1 : 0,
    'opt_screening' =>
      case self.screening
      when :all; 'A'
      when :anonymous; 'R'
      when :nonfriends; 'F'
      when :none; 'N'
      when :default; ''
      end
  }.each do |name, value|
    req["prop_#{name}"] = value
  end
end
display_itemid() click to toggle source

Get the numeric id used in URLs (it's a function of the itemid and the anum).

# File lib/livejournal/entry.rb, line 193
def display_itemid
  (@itemid << 8) + @anum
end
event_as_html(server=LiveJournal::DEFAULT_SERVER) click to toggle source

Render LJ markup to an HTML simulation of what is displayed on LJ itself. (XXX this needs some work: polls, better preformatting, etc.)

(The server to use is necessary for rendering links to other LJ users.)

# File lib/livejournal/entry.rb, line 206
def event_as_html server=LiveJournal::DEFAULT_SERVER
  # I'd like to use REXML but the content isn't XML, so REs it is!
  html = @event.dup
  html.gsub!(/\n/, "<br/>\n") unless @preformatted
  html.gsub!(%r{< \s* lj \s+ user \s* = \s*
                ['"]? ([^\s'"]+) ['"]?
                \s* /? \s* >}ix) do
    user = $1
    url = "#{server.url}/~#{user}/"
    "<a href='#{url}'><b>#{user}</b></a>"
  end
  html
end
from_request(req) click to toggle source
# File lib/livejournal/entry.rb, line 112
def from_request(req)
  @itemid, @anum = req['itemid'].to_i, req['anum'].to_i
  @subject, @event = req['subject'], CGI.unescape(req['event'])

  case req['security']
  when 'public'
    @security = :public
  when 'private'
    @security = :private
  when 'usemask'
    if req['allowmask'] == '1'
      @security = :friends
    else
      @security = :custom
      @allowmask = req['allowmask'].to_i
    end
  end

  @time = LiveJournal::Request::ljtime_to_time req['eventtime']

  # further metadata is loaded via #load_prop

  self
end
load_from_database_row(row) click to toggle source

Parse an entry from a row from the database.

# File lib/livejournal/database.rb, line 243
def load_from_database_row row
  @itemid, @anum = row[0].to_i, row[1].to_i
  @subject, @event = row[2], row[3]
  @moodid, @mood = row[4].nil? ? nil : row[4].to_i, row[5]
  @music, @location, @taglist, @pickeyword = row[6], row[7], row[8], row[9]
  @taglist = if @taglist then @taglist.split(/, /) else [] end
  @preformatted, @backdated = !row[10].nil?, !row[11].nil?
  @comments = case Database::optional_to_i(row[12])
    when nil; :normal
    when 1; :none
    when 2; :noemail
    else raise Database::Error, "Bad comments value: #{row[12].inspect}"
  end

  @time = Time.at(row[16].to_i).utc

  case Database::optional_to_i(row[17])
  when nil
    @security = :public
  when 0
    @security = :private
  when 1
    @security = :friends
  else
    @security = :custom
    @allowmask = row[17]
  end

  self
end
time=(time) click to toggle source
# File lib/livejournal/entry.rb, line 107
def time=(time)
  raise RuntimeError, "Must use GMT times everywhere to reduce confusion.  See LiveJournal::coerce_gmt for details." unless time.gmt?
  @time = time
end
to_database_row() click to toggle source
# File lib/livejournal/database.rb, line 273
def to_database_row
  comments = case @comments
    when :normal; nil
    when :none; 1
    when :noemail; 2
  end
  security = case @security
    when :public; nil
    when :private; 0
    when :friends; 1
    when :custom; @allowmask
  end
  [@itemid, @anum, @subject, @event,
   @moodid, @mood, @music, @location, @taglist.join(', '), @pickeyword,
   @preformatted ? 1 : nil, @backdated ? 1 : nil, comments,
   @time.year, @time.mon, @time.day, @time.to_i, security]
end
url(user) click to toggle source
# File lib/livejournal/entry.rb, line 197
def url(user)
  #raise NotImplementedError, "only works for lj.com" unless user.server == LiveJournal::DEFAULT_SERVER
  "#{user.journal_url}/#{display_itemid}.html"
end