Class: LiveJournal::Sync::Entries

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

Overview

To run a sync, create a Sync::Entries object, then call Entries#run_syncitems to fetch the sync metadata, then call Entries#run_sync to get the actual entries.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user, lastsync = nil) ⇒ Entries

Returns a new instance of Entries.



96
97
98
99
100
# File 'lib/livejournal/sync.rb', line 96

def initialize(user, lastsync=nil)
  @user = user
  @logitems = {}
  @lastsync = lastsync
end

Instance Attribute Details

#lastsyncObject (readonly)

To resume from a previous sync, pass in its lastsync value.



94
95
96
# File 'lib/livejournal/sync.rb', line 94

def lastsync
  @lastsync
end

Instance Method Details

#run_syncObject

:yields: entries_hash, lastsync, remaining_count



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/livejournal/sync.rb', line 117

def run_sync  # :yields: entries_hash, lastsync, remaining_count
  return if @logitems.empty?

  lastsync = @lastsync
  while @logitems.size > 0
    req = Request::GetEvents.new(@user, :lastsync => lastsync)
    entries = req.run
    # pop off all items that we now have entries for
    entries.each do |itemid, entry|
      time = @logitems.delete itemid
      lastsync = time if lastsync.nil? or time > lastsync
    end
    yield entries, lastsync, @logitems.size
  end
end

#run_syncitemsObject

:yields: cur, total



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/livejournal/sync.rb', line 101

def run_syncitems  # :yields: cur, total
  cur = 0
  total = nil
  items = {}
  lastsync = @lastsync
  while total.nil? or cur < total
    req = Request::SyncItems.new(@user, items, lastsync)
    lastsync = req.run
    cur += req.fetched
    total = req.total unless total
    yield cur, total if block_given?
  end
  @logitems = Request::SyncItems::subset_items(items, 'L')
  return (not @logitems.empty?)
end