Skip to content

Instantly share code, notes, and snippets.

@frobichaud
Last active December 7, 2022 18:54
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • 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))
@frobichaud
Copy link
Author

Fixing up HelloSign's suggested Ruby export of all documents:
https://faq.hellosign.com/hc/en-us/articles/360013597231-How-to-download-all-documents

How I use it

  1. Create a Gemfile in a new folder
  2. Add the following to the Gemfile:
source "https://rubygems.org"
gem 'hellosign-ruby-sdk'
  1. run "bundle" in a terminal
  2. run "bundle console"
  3. Replace "YOUR_API_KEY_HERE" with your key
  4. Copy-paste the Gist
  5. All files should be download as PDFs in your current folder

@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