Skip to content

Commit

Permalink
Added find across pages methods for deletions.
Browse files Browse the repository at this point in the history
  • Loading branch information
amartinfraguas committed Feb 8, 2011
1 parent f519b92 commit fc1ac4a
Showing 1 changed file with 37 additions and 1 deletion.
38 changes: 37 additions & 1 deletion lib/highrise/pagination.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,21 @@ def find_all_across_pages_since(time)
find_all_across_pages(:params => { :since => time.utc.strftime("%Y%m%d%H%M%S") })
end

# This is useful only for Company, Person, Note, Comment, Email and Task, but should be safely ignored by other classes
def find_all_deletions_across_pages(options = {})
# point to the global deletions feed
options[:from] = '/deletions.xml'

records = []
each_deletions(options) { |record| records << record }
records
end

# This is useful only for Company, Person, Note, Comment, Email and Task, but should be safely ignored by other classes
def find_all_deletions_across_pages_since(time)
find_all_deletions_across_pages(:params => { :since => time.utc.strftime("%Y%m%d%H%M%S") })
end

private

def each(options = {})
Expand All @@ -31,6 +46,27 @@ def each(options = {})
end
end
end

def each_deletions(options = {})
options[:params] ||= {}
# first index for deletions is 1
options[:params][:n] = 1

loop do
if (records = self.find(:all, options)).try(:any?)
# reject the records whose resource type is different from self
records.reject!{|r| r.type != self.to_s.split('::').last}

records.each{ |record| yield record }

# increment index interval for deletions is 1 per page of 500 resources
options[:params][:n] += 1
else
break # no deletions included on that page, thus no more deletions
end
end
end

end
end
end
end

0 comments on commit fc1ac4a

Please sign in to comment.