Class: LiveJournal::Request::GetEvents

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

Instance Method Summary collapse

Methods inherited from Req

#dryrun!, #dumpresponse, #verbose!

Constructor Details

#initialize(user, opts) ⇒ GetEvents

We support three different types of GetEvents:

  • GetEvents.new(user, :itemid => itemid) (fetch a single item)

  • GetEvents.new(user, :recent => n) (fetch most recent n itemds)

  • GetEvents.new(user, :lastsync => lastsync) (for syncing)

We support one final option called :strict, which requires a bit of explanation.

Whenever LiveJournal adds new metadata to entries (such as the location field, which was introduced in 2006) it also exposes this metadata via the LJ protocol. However, ljrb can’t know about future metadata and doesn’t know how to handle it properly. Some metadata (like the current location) must be sent to the server to publish an entry correctly; others, like the last revision time, must not be.

Normally, when we see a new property we abort with a ProtocolException. If the object is constructed with :strict => false, we’ll skip over any new properties.



313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'lib/livejournal/entry.rb', line 313

def initialize(user, opts)
  super(user, 'getevents')
  @request['lineendings'] = 'unix'

  @strict = true
  @strict = opts[:strict] if opts.has_key? :strict

  if opts.has_key? :itemid
    @request['selecttype'] = 'one'
    @request['itemid'] = opts[:itemid]
  elsif opts.has_key? :recent
    @request['selecttype'] = 'lastn'
    @request['howmany'] = opts[:recent]
  elsif opts.has_key? :lastsync
    @request['selecttype'] = 'syncitems'
    @request['lastsync'] = opts[:lastsync] if opts[:lastsync]
  else
    raise ArgumentError, 'invalid options for GetEvents'
  end
end

Instance Method Details

#runObject

Returns either a single #Entry or a hash of itemid => #Entry, depending on the mode this was constructed with.



336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
# File 'lib/livejournal/entry.rb', line 336

def run
  super

  entries = {}
  each_in_array('events') do |req|
    entry = Entry.new.from_request(req)
    entries[entry.itemid] = entry
  end

  each_in_array('prop') do |prop|
    itemid = prop['itemid'].to_i
    entries[itemid].load_prop(prop['name'], prop['value'], @strict)
  end

  if @request.has_key? 'itemid'
    return entries[@request['itemid']]
  else
    return entries
  end
end