Class: LiveJournal::Entry

Inherits:
Object
  • Object
show all
Defined in:
lib/livejournal/entry.rb,
lib/livejournal/database.rb

Constant Summary collapse

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

%w{admin_content_flag adult_content commentalter
current_coords personifi_lang personifi_tags personifi_word_count
qotdid revnum revtime sms_msgid statusvis syn_id syn_link unknown8bit
unsuspend_supportid used_rte useragent verticals_list poster_ip uniq}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeEntry

Returns a new instance of Entry.



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# 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

Instance Attribute Details

#allowmaskObject

Returns the value of attribute allowmask.



54
55
56
# File 'lib/livejournal/entry.rb', line 54

def allowmask
  @allowmask
end

#anumObject

Returns the value of attribute anum.



48
49
50
# File 'lib/livejournal/entry.rb', line 48

def anum
  @anum
end

#backdatedObject

Returns the value of attribute backdated.



50
51
52
# File 'lib/livejournal/entry.rb', line 50

def backdated
  @backdated
end

#commentsObject

values: :none, :noemail



51
52
53
# File 'lib/livejournal/entry.rb', line 51

def comments
  @comments
end

#eventObject

Returns the value of attribute event.



48
49
50
# File 'lib/livejournal/entry.rb', line 48

def event
  @event
end

#give_featuresObject

Returns the value of attribute give_features.



57
58
59
# File 'lib/livejournal/entry.rb', line 57

def give_features
  @give_features
end

#interfaceObject

values …



56
57
58
# File 'lib/livejournal/entry.rb', line 56

def interface
  @interface
end

#itemidObject

Returns the value of attribute itemid.



48
49
50
# File 'lib/livejournal/entry.rb', line 48

def itemid
  @itemid
end

#locationObject

Returns the value of attribute location.



49
50
51
# File 'lib/livejournal/entry.rb', line 49

def location
  @location
end

#moodObject

Returns the value of attribute mood.



48
49
50
# File 'lib/livejournal/entry.rb', line 48

def mood
  @mood
end

#moodidObject

Returns the value of attribute moodid.



48
49
50
# File 'lib/livejournal/entry.rb', line 48

def moodid
  @moodid
end

#musicObject

Returns the value of attribute music.



49
50
51
# File 'lib/livejournal/entry.rb', line 49

def music
  @music
end

#pickeywordObject

Returns the value of attribute pickeyword.



49
50
51
# File 'lib/livejournal/entry.rb', line 49

def pickeyword
  @pickeyword
end

#preformattedObject

Returns the value of attribute preformatted.



50
51
52
# File 'lib/livejournal/entry.rb', line 50

def preformatted
  @preformatted
end

#propsObject

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.)



62
63
64
# File 'lib/livejournal/entry.rb', line 62

def props
  @props
end

#screeningObject

values :all, :anonymous, :nonfriends, :none



55
56
57
# File 'lib/livejournal/entry.rb', line 55

def screening
  @screening
end

#securityObject

values: :private, :friends, :custom



53
54
55
# File 'lib/livejournal/entry.rb', line 53

def security
  @security
end

#subjectObject

Returns the value of attribute subject.



48
49
50
# File 'lib/livejournal/entry.rb', line 48

def subject
  @subject
end

#taglistObject

Returns the value of attribute taglist.



49
50
51
# File 'lib/livejournal/entry.rb', line 49

def taglist
  @taglist
end

#timeObject

a Ruby Time object



52
53
54
# File 'lib/livejournal/entry.rb', line 52

def time
  @time
end

Instance Method Details

#==(other) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# 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) ⇒ Object



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/livejournal/entry.rb', line 221

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_itemidObject

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



193
194
195
# File 'lib/livejournal/entry.rb', line 193

def display_itemid
  (@itemid << 8) + @anum
end

#event_as_html(server = LiveJournal::DEFAULT_SERVER) ⇒ Object

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.)



207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/livejournal/entry.rb', line 207

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) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# 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) ⇒ Object

Parse an entry from a row from the database.



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# 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

#load_prop(name, value, strict = false) ⇒ Object

:nodoc:#



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/livejournal/entry.rb', line 137

def load_prop(name, value, strict=false) #:nodoc:#
  case name
  when 'current_location'
    @location = value
  when 'current_mood'
    @mood = value
  when 'current_moodid'
    @moodid = value.to_i
  when 'current_music'
    @music = value
  when 'hasscreened'
    @screened = value == '1'
  when 'interface'
    @interface = value.intern
  when 'opt_backdated'
    @backdated = value == '1'
  when 'opt_nocomments'
    @comments = :none
  when 'opt_noemail'
    @comments = :noemail
  when 'opt_preformatted'
    @preformatted = value == '1'
  when 'opt_screening'
    case value
    when 'A'; @screening = :all
    when 'R'; @screening = :anonymous
    when 'F'; @screening = :nonfriends
    when 'N'; @screening = :none
    else
      raise LiveJournalException,
        "unknown opt_screening value #{value.inspect}"
    end
  when 'picture_keyword'
    @pickeyword = value
  when 'taglist'
    @taglist = value.split(/,\s/).sort
  when 'give_features'
      @give_features = value == '1'
  else
    # LJ keeps adding props, so we store all leftovers in a hash.
    # Unfortunately, we don't know which of these need to be passed
    # on to new entries.  This may mean we drop some data when we
    # round-trip.
    #
    # Some we've seen so far:
    #   revnum, revtime, commentalter, unknown8bit, useragent
    @props[name] = value

    unless KNOWN_EXTRA_PROPS.include? name or not strict
      raise Request::ProtocolException, "unknown prop (#{name}, #{value})"
    end
  end
end

#to_database_rowObject



273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# 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) ⇒ Object

Raises:

  • (UnimplementedError)


197
198
199
200
201
# File 'lib/livejournal/entry.rb', line 197

def url(user)
  raise UnimplementedError, "only works for lj.com" unless user.server == LiveJournal::DEFAULT_SERVER
  journal = user.journal.gsub(/_/, '-')
  "http://#{journal}.livejournal.com/#{display_itemid}.html"
end