Skip to content

Instantly share code, notes, and snippets.

@frobichaud
Last active December 7, 2022 18:54
Show Gist options
  • Save frobichaud/2357b52c564459e4ea57e95c0474fa42 to your computer and use it in GitHub Desktop.
Save frobichaud/2357b52c564459e4ea57e95c0474fa42 to your computer and use it in GitHub Desktop.
HelloSign - Download all documents
API_KEY = 'YOUR_API_KEY_HERE'
def get_total_pages(client)
list = client.get_signature_requests(page_size: 100)
list.data["list_info"]["num_pages"]
end
def get_all_requests(client)
signature_requests = []
page = 1
total_pages = get_total_pages(client)
while page < total_pages + 1
req = client.get_signature_requests(page_size: 100, page: page)
signature_requests << req.data["signature_requests"]
page += 1
end
signature_requests.flatten
end
def download_requests(client, requests)
requests.each do |req|
begin
puts "-- Downloading #{req['final_copy_uri']}"
download = client.get(
req["final_copy_uri"].gsub('/v3', '')
)
rescue StandardError => e
puts "-- Failed #{e.message}"
next
end
fname = clear_title(req["title"])
File.open("HelloSign-#{fname}", "wb") do |file|
file.write(download[:body])
end
end
end
def clear_title(str)
str = str.gsub(/[^\w]/, '_')
str += '.pdf'
end
client = HelloSign::Client.new(api_key: API_KEY)
download_requests(client, get_all_requests(client))
@MokhiLaxmidevi
Copy link

instead of doing gsub, file write and clear title, we can use req["files_url"] rather than req["final_copy_uri"]

@la-ruby
Copy link

la-ruby commented May 4, 2022

I did some tweaks after having trouble running this.
What I'm doing

  • Using ruby version 3 nowadays
  • Storing intermediate files to make it less of a memory hog, and also
    that makes the entire job "resumable"
    My job required downloading lots of pdfs, so the ability to resume was crucial.
  • Created a GitHub repo for it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment