Skip to content

Instantly share code, notes, and snippets.

@drewkerrigan
Created September 5, 2013 00:40
Show Gist options
  • Save drewkerrigan/6444635 to your computer and use it in GitHub Desktop.
Save drewkerrigan/6444635 to your computer and use it in GitHub Desktop.
fetch keys and get objects with pagination
def fetch_with_pagination(keys, start = 1, count = 50)
zombies = []
keys = [] unless keys
result_count = 1
keys.each_with_index do |zombie_key, i|
break if result_count > count
next if (i + 1) < start
data = @client['zombies'].get(zombie_key).data
data["dna"] = data["dna"][0..20] + "..."
zombies << data
result_count+=1
end
pages = 0
current_page = 0
next_index = 0
prev_index = 0
if keys.count > 0
pages = (Float(keys.count) / Float(count)).ceil
current_page = ((Float(start) / Float(keys.count)) * Float(pages)).ceil
next_index = ((start + count) > keys.count) ?
((pages - 1) * count + 1) : (start + count)
prev_index = (start == 1) ? 1 : start - count
end
{
:start => start,
:pages => pages,
:current_page => current_page,
:next_index => next_index,
:prev_index => prev_index,
:increment => count,
:total_count => keys.count,
:zombies => zombies
}
end
def search_index(index, query)
@client['zombies'].get_index(index, query)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment