Skip to content

Instantly share code, notes, and snippets.

@dinjas
Forked from rutvij-pandya/echo_sign_api
Created April 28, 2014 20:55
Show Gist options
  • Save dinjas/11383725 to your computer and use it in GitHub Desktop.
Save dinjas/11383725 to your computer and use it in GitHub Desktop.
### Core library for EchoSign APIs
### Use a wrapper file for validations & response handling
### Get a Developer Edition account & replace "YOUR_ACCOUNT_API_KEY" with API key of your account.
### With Rails, use Savon gem for SOAP based requests.
module EchoSignApi
API_URI = 'https://secure.echosign.com/services/EchoSignDocumentService15?wsdl'
API_KEY = YOUR_ACCOUNT_API_KEY
SOAP_CLIENT = Savon.client(wsdl: API_URI, log: true, pretty_print_xml: true, env_namespace: 'soap')
CALLBACK_SECURITY_KEY = "SOME_SECURITY_KEY_FOR_ECHOSIGN_CALL_BACK_URL"
CALLBACK_URL = 'https://YOUR_DOMAIN.com/echo-sign/CALL_BACK_PATH/'+CALLBACK_SECURITY_KEY
class EchoSignClient
def initialize(api_key =nil)
@api_key = api_key || API_KEY
end
# sends document(s) for signing & returns document_key
# returns : document_key
#
def send_document(sender, document)
# set default values for user info
sender_info = sender[:user_key].present? ? sender : nil
document[:signature_flow] = 'SENDER_SIGNATURE_NOT_REQUIRED'
document[:reminder_frequency] = document[:reminder_frequency] || 'WEEKLY_UNTIL_SIGNED'
document[:signature_type] = 'ESIGN'
document[:callback_info] = {signed_document_url: Cms::EchoSign::EchoSignApi::CALLBACK_URL} if Rails.env != "development"
# API call
call_api(:send_document, {sender_info: sender_info, document_creation_info: document})
end
# cancel document
# returns : CancelDocumentResult - CANCELLED, ALREADY_SIGNED, ...
#
def cancel_document(document_key, comment="", notify_signer=true)
# call cancelDocument API
call_api(:cancel_document, {document_key: document_key, comment: comment, notify_signer: notify_signer})
end
# get details of all users in account
# returns : company, email, full-name_or_email, user_key, group_key for all users
#
def get_users_in_account
# call getUsersInAccount API
call_api(:get_users_in_account)
end
# add a user in account
# returns : user_key
#
def create_user(user_info)
# call createUser API
call_api(:create_user, {user_info: user_info})
end
# makes a call to given API with required params
# returns : API response HASH
#
def call_api(api_method, api_params={})
begin
Timeout::timeout(100) do
response = SOAP_CLIENT.call(api_method.to_sym, :message => {api_key: @api_key}.merge(api_params)).body["#{api_method}_response".to_sym]
return {success:true, response:response, exception:""}
end
rescue Timeout::Error
return {success:false, response:{}, exception:"EXCEPTION::#{api_method}-TimeOut"}
rescue Savon::SOAPFault => e
return {success:false, response:{}, exception:"SAVON EXCEPTION::#{api_method}-#{e.to_hash[:fault][:faultstring].to_s}"}
rescue Exception => e
return {success:false, response:{}, exception:"EXCEPTION::#{api_method}-#{e.message.inspect}"}
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment