Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@tybro0103
Created November 26, 2012 18:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tybro0103/4149781 to your computer and use it in GitHub Desktop.
Save tybro0103/4149781 to your computer and use it in GitHub Desktop.
Twilio API Paging Bug
require 'json'
require 'rest_client'
ACCOUNT_SID = "XXX"
AUTH_TOKEN = "YYY"
API_BASE = "https://#{ACCOUNT_SID}:#{AUTH_TOKEN}@api.twilio.com"
TEST_DATE = "2012-10-27"
def fetch_call_sids_from_uri(uri, sids=[])
# fetch and parse data
json = RestClient.get "#{API_BASE}#{uri}"
data = JSON.parse json
# put returned sids in sids array
data['calls'].each { |call| sids.push(call['sid']) }
# recursively repeat if next_page_uri exists; return sids
next_page_uri = data['next_page_uri']
next_page_uri ? fetch_call_sids_from_uri(next_page_uri, sids) : sids
end
non_paged_sids = fetch_call_sids_from_uri "/2010-04-01/Accounts/#{ACCOUNT_SID}/Calls.json?StartTime=#{TEST_DATE}&PageSize=1000"
paged_sids = fetch_call_sids_from_uri "/2010-04-01/Accounts/#{ACCOUNT_SID}/Calls.json?StartTime=#{TEST_DATE}&PageSize=50"
puts "Test Date: " << TEST_DATE
puts "# when not paged: " << non_paged_sids.length.to_s
puts "# when paged: " << paged_sids.length.to_s
puts "missing sids:\n" << (non_paged_sids - paged_sids).to_s
# SAMPLE OUTPUT
# Test Date: 2012-10-27
# # when not paged: 258
# # when paged: 252
# missing sids:
# ["CAxxx", "CAxxx", "CAxxx", "CAxxx", "CAxxx", "CAxxx", "CAxxx", "CAxxx", "CAxxx"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment